简介
本文将用Docker的方式来构建一个应用APP。
过去,如果要开发一个Python应用APP,所需做的第一件事就是在开发机上安装Python运行时环境。在这种情形下,开发机的环境必须与APP所要求的环境一致,同时还需要与生产环境相匹配。
通过使用Docker,可以将一个可移植的Python运行时环境作为一个image获取,而无需安装。然后,就可以基于Python运行时环境image,将APP代码及其依赖库合并构建,从而简化了应用APP的部署难度。
用Dockerfile定义容器Container
Dockerfile是由一系列命令和参数构成的脚本,这些命令应用于基础镜像并最终创建一个新的镜像。它简化了业务部署的流程,大大提高了业务的部署速度。Dockerfile的产出为一个新的可以用于创建容器的镜像。
Dockerfile语法由两部分构成,分别是“注释”和“命令+参数”。
# Line blocks used for commenting
COMMAND argument1 argument2 ...
对于Dockerfile,我们首先创建一个空目录,然后cd到该目录,并创建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
ADD . /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"]
其中,app.py和requirements.txt都位于与Dockerfile相同的目录下,具体如下:
zjl@ubuntu:~/docker/pyapp$ ls
app.py Dockerfile requirements.txt
对于requirements.txt,其内容如下:
Flask
Redis
对于app.py,其内容如下:
from flask import Flask
from redis import Redis, RedisError
import os
import socket
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)
现在,我们知道,pip install -r requirements.txt
为Python安装了Flask和Redis库,同时APP会打印出环境变量NAME
,同时将socket.gethostname()
打印出来。
构建新的image镜像
下面,我们进行构建,具体如下:
zjl@ubuntu:~/docker/pyapp$ ls
app.py Dockerfile requirements.txt
zjl@ubuntu:~/docker/pyapp$ sudo docker build -t fhello .
Sending build context to Docker daemon 4.608kB
Step 1/7 : FROM python:2.7-slim
2.7-slim: Pulling from library/python
d2ca7eff5948: Pull complete
cef69dd0e5b9: Pull complete
50e1d7e4f3c6: Pull complete
861e9de5333f: Pull complete
Digest: sha256:e9baca9b405d3bbba71d4c3c4ce8a461e4937413b8b910cb1801dfac0a2423aa
Status: Downloaded newer image for python:2.7-slim
---> 52ad41c7aea4
Step 2/7 : WORKDIR /app
...
Step 3/7 : ADD . /app
...
Step 4/7 : RUN pip install --trusted-host pypi.python.org -r requirements.txt
...
Step 5/7 : EXPOSE 80
...
Step 6/7 : ENV NAME World
...
Step 7/7 : CMD ["python", "app.py"]
...
Successfully built d3fafd68e807
Successfully tagged fhello:latest
zjl@ubuntu:~/docker/pyapp$ sudo docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
fhello latest 12d000cd7a1b 12 minutes ago 148MB
运行新的image镜像
$ sudo docker run -p 4000:80 fhello
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
# 访问该网站
$ curl localhost:4000
<h3>Hello World!</h3><b>Hostname:</b> f4e37f061593<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>
$ sudo docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4a150bdc67cd fhello "python app.py" 10 seconds ago Up 9 seconds 0.0.0.0:4000->80/tcp upbeat_austin
关闭容器,命令如下:
$ sudo docker container stop 4a150bdc67cd
4a150bdc67cd
给镜像打上标签
syntax: -->> docker tag image username/repository:tag
$ sudo docker tag fhello zhjl/getstarted:alpha-1
$ sudo docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
fhello latest 12d000cd7a1b 18 minutes ago 148MB
zhjl/getstarted alpha-1 12d000cd7a1b 18 minutes ago 148MB
$ docker run -p 4000:80 zhjl/getstarted:alpha-1
Recap and cheat sheet
# Create image using this directory's Dockerfile
docker build -t fhello .
# Run "friendlyname" mapping port 4000 to 80
docker run -p 4000:80 fhello
# Same thing, but in detached mode
docker run -d -p 4000:80 fhello
# List all running containers
docker container ls
# List all containers, even those not running
docker container ls -a
# Gracefully stop the specified container
docker container stop <hash>
# Force shutdown of the specified container
docker container kill <hash>
# Remove specified container from this machine
docker container rm <hash>
# Remove all containers
docker container rm $(docker container ls -a -q)
# List all images on this machine
docker image ls -a
# Remove specified image from this machine
docker image rm <image id>
# Remove all images from this machine
docker image rm $(docker image ls -a -q)
# Log in this CLI session using your Docker credential
docker login
# Tag <image> for upload to registry
docker tag <image> username/repository:tag
# Upload tagged image to registry
docker push username/repository:tag
# Run image from a registry
docker run username/repository:tag