In the past, if you were to start writing a Python app, your first order of business was to install a Python runtime onto your machine. But, that creates a situation where the environment on your machine has to be just so in order for your app to run as expected; ditto for the server that runs your app.
With Docker, you can just grab a portable Python runtime as an image, no installation necessary. Then, your build can include the base Python image right alongside your app code, ensuring that your app, its dependencies, and the runtime, all travel together.
过去写一个Python程序,需要在本机安装Python运行环境,本机必须有序的安装程序,才能按期望运行程序。服务器也同理。
但是用Docker,可以获取一个Python运行环境镜像,无需安装,应用代码与镜像一起构建,以确保应用的运行环境及依赖一起运行。
1 创建容器,新建一个工作目录,加入3个文件
1.1 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 -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"]
如果有代理,需配置代理
# Set proxy server, replace host:port with values for your servers
ENV http_proxy host:port
ENV https_proxy host:port
1.2 requirements.txt
Flask
Redis
1.3 app.py
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)
1.4 执行以下命令,等待下载及安装
docker build -t friendlyhello .
控制台输出:
Sending build context to Docker daemon 11.78kB
Step 1/7 : FROM python:2.7-slim
2.7-slim: Pulling from library/python
d13d02fa248d: Pull complete
5875fae15e49: Pull complete
19a68c2b3f2d: Pull complete
6a420196b3d3: Pull complete
Digest: sha256:7a64f01690266b9c7b505c6fbe7153cd01c46de6798eeba58b1afa10a0efa228
Status: Downloaded newer image for python:2.7-slim
---> e9adbdab327d
Step 2/7 : WORKDIR /app
---> 6c8be0209b9b
Removing intermediate container 79518c2c25af
Step 3/7 : ADD . /app
---> f8ad81dc489b
Step 4/7 : RUN pip install -r requirements.txt
---> Running in 6fd799d76cec
Collecting Flask (from -r requirements.txt (line 1))
Downloading Flask-0.12.2-py2.py3-none-any.whl (83kB)
Collecting Redis (from -r requirements.txt (line 2))
Downloading redis-2.10.6-py2.py3-none-any.whl (64kB)
Collecting itsdangerous>=0.21 (from Flask->-r requirements.txt (line 1))
Downloading itsdangerous-0.24.tar.gz (46kB)
Collecting Jinja2>=2.4 (from Flask->-r requirements.txt (line 1))
Downloading Jinja2-2.9.6-py2.py3-none-any.whl (340kB)
Collecting Werkzeug>=0.7 (from Flask->-r requirements.txt (line 1))
Downloading Werkzeug-0.12.2-py2.py3-none-any.whl (312kB)
Collecting click>=2.0 (from Flask->-r requirements.txt (line 1))
Downloading click-6.7-py2.py3-none-any.whl (71kB)
Collecting MarkupSafe>=0.23 (from Jinja2>=2.4->Flask->-r requirements.txt (line 1))
Downloading MarkupSafe-1.0.tar.gz
Building wheels for collected packages: itsdangerous, MarkupSafe
Running setup.py bdist_wheel for itsdangerous: started
Running setup.py bdist_wheel for itsdangerous: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/fc/a8/66/24d655233c757e178d45dea2de22a04c6d92766abfb741129a
Running setup.py bdist_wheel for MarkupSafe: started
Running setup.py bdist_wheel for MarkupSafe: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/88/a7/30/e39a54a87bcbe25308fa3ca64e8ddc75d9b3e5afa21ee32d57
Successfully built itsdangerous MarkupSafe
Installing collected packages: itsdangerous, MarkupSafe, Jinja2, Werkzeug, click, Flask, Redis
Successfully installed Flask-0.12.2 Jinja2-2.9.6 MarkupSafe-1.0 Redis-2.10.6 Werkzeug-0.12.2 click-6.7 itsdangerous-0.24
---> f51df28a7a50
Removing intermediate container 6fd799d76cec
Step 5/7 : EXPOSE 80
---> Running in 586b51058063
---> fbbd919d52c1
Removing intermediate container 586b51058063
Step 6/7 : ENV NAME World
---> Running in 20ba1eeb0e0b
---> d6a7bfb8a00a
Removing intermediate container 20ba1eeb0e0b
Step 7/7 : CMD python app.py
---> Running in 544d1d800091
---> b9da56efd7dc
Removing intermediate container 544d1d800091
Successfully built b9da56efd7dc
Successfully tagged friendlyhello:latest
1.5 查看镜像
docker image ls 或 docker images
1.6 运行image 将docker的80端口映射为宿主机的4000端口
docker run -p 4000:80 friendlyhello
浏览器请求localhost:4000
docker run -d -p 4000:80 friendlyhello 可以后台运行
docker container ls 查看容器ID
docker container stop 3ccdc83fda2d 使用容器ID结束
2 共享镜像(操作方式类似于git)
2.1 首先再在https://cloud.docker.com网站注册用户
命令行使用用户名密码登录
docker login
控制台输出:
Login Succeeded
2.2 上传镜像
样例 docker tag image username/repository:tag
docker tag friendlyhello gaojingyuan/testrepo:v1
2.3 查看镜像
docker image ls
控制台输出:
REPOSITORY TAG IMAGE ID CREATED SIZE
gaojingyuan/testrepo v1 b9da56efd7dc 28 minutes ago 150MB
friendlyhello latest b9da56efd7dc 28 minutes ago 150MB
python 2.7-slim e9adbdab327d 6 days ago 138MB
hello-world latest 05a3bd381fc2 6 weeks ago 1.84kB
2.4 发布镜像
docker push gaojingyuan/testrepo:v1
控制台输出:
The push refers to a repository [docker.io/gaojingyuan/testrepo]
03209beada39: Pushed
afa3600fb996: Pushed
f02dd5f87330: Pushed
267e945e8138: Mounted from library/python
227cf6fd7a76: Mounted from library/python
60ed3196351d: Mounted from library/python
29d71372a492: Mounted from library/python
v1: digest: sha256:32f4cbc9b9528c43b5b0f00ba1a3c4c4efb82f31b39c63fa809c695ff918972e size: 1788
2.5 执行镜像,如果本地没有会从repository下载
docker run -p 4000:80 gaojingyuan/testrepo:v1
控制台输出:
Unable to find image 'gaojingyuan/testrepo:v1' locally
part2: Pulling from gaojingyuan/testrepo:v1
备注:
docker build -t friendlyname . # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyname # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyname # Same thing, but in detached mode
docker container ls # List all running containers
docker container ls -a # List all containers, even those not running
docker container stop <hash> # Gracefully stop the specified container
docker container kill <hash> # Force shutdown of the specified container
docker container rm <hash> # Remove specified container from this machine
docker container rm $(docker container ls -a -q) # Remove all containers
docker image ls -a # List all images on this machine
docker image rm <image id> # Remove specified image from this machine
docker image rm $(docker image ls -a -q) # Remove all images from this machine
docker login # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag # Tag <image> for upload to registry
docker push username/repository:tag # Upload tagged image to registry
docker run username/repository:tag # Run image from a registry