Docker和云计算学习

Docker -自底向上构建容器

2019-05-19  本文已影响0人  山中散人的博客

在过去,如果要构建一个应用程序,例如,利用Python写一个文本正则表达式匹配搜索工具,第一件要做的事就是安装Python解释器和运行环境(当然一般是自带的)。开发者可能还需要测试运行环境是否符合期望,然后编译安装应用程序,再进行测试。

现在,我们可以选择一个Python镜像,依次为基础,构建一个包含应用程序的镜像,同时将其运行环境打包。

构建镜像

Dockerfile defines what goes on in the environment inside your container.

  • Access to virtualized resources like networking interfaces and disk drives
  • Map ports to the outside world
  • Specific about what files you want to “copy in” to that image

Dockerfile定义了构建镜像的每一步,如何访问网络和硬盘,拷贝哪些文件到镜像中,将哪些端口暴露到镜像之外。

使用Dockerfile时,创建一个空目录(镜像目录)和文件Dockerfile,

# Use an official Python runtime as a parent image
FROM python:2.7-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

创建文件requirements.txt,指定镜像依赖的Python包

Flask
Redis

在文件app.py中,完成应用程序的逻辑,一个hello world Web程序,通过HTML文档返回本地主机名和主页访问次数。

from flask import Flask
from redis import Redis, RedisError
import os
import socket

# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)

app = Flask(__name__)

@app.route("/")
def hello():
    try:
        visits = redis.incr("counter")
    except RedisError:
        visits = "<i>cannot connect to Redis, counter disabled</i>"

    html = "<h3>Hello {name}!</h3>" \
           "<b>Hostname:</b> {hostname}<br/>" \
           "<b>Visits:</b> {visits}"
    return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)

运行Dockerfile构建镜像,指定tag名。

docker build --tag=friendlyhello:v0.0.1 .

构建成功后,得到一个镜像,

docker image ls

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        4 months ago        1.84kB

#delete the newly built image
docker container stop fce289e99eb9
docker image rm fce289e99eb9

运行容器

构建成功后,通过容器运行应用程序,将本地主机的端口4000映射到容器端口80上。

docker run -p 4000:80 friendlyhello

利用浏览器访问[http://localhost:4000]

访问hello-world 页面

在Shell中访问

curl http://localhost:4000

发布镜像

let’s upload our built image and run it somewhere else. Push to registries when you want to deploy containers to production.

Docker通过DockerHub提供了类似GitHub的版本控制,镜像分发服务。

#login docker hub
docker login

Authenticating with existing credentials...
Login Succeeded

首先登录DockerHub,在Mac OS上可以通过Docker Desktop完成

Docker Desktop
#download nginx image from Docker hub
docker pull nginx

#tag local image
docker tag nginx:latest kevinacoder/my_nginx

#publish the image
docker push kevinacoder/my_nginx

#get the image
docker pull kevinacoder/my_nginx

通过DockerHub发布,获取镜像的方法和GitHub类似。

原文:[https://docs.docker.com/get-started/part2/]

上一篇 下一篇

猜你喜欢

热点阅读