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.
過去寫一個(gè)Python程序,需要在本機(jī)安裝Python運(yùn)行環(huán)境鳄炉,本機(jī)必須有序的安裝程序,才能按期望運(yùn)行程序。服務(wù)器也同理。
但是用Docker葵第,可以獲取一個(gè)Python運(yùn)行環(huán)境鏡像鳍咱,無需安裝,應(yīng)用代碼與鏡像一起構(gòu)建馅笙,以確保應(yīng)用的運(yùn)行環(huán)境及依賴一起運(yùn)行。
1 創(chuàng)建容器厉亏,新建一個(gè)工作目錄董习,加入3個(gè)文件
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 執(zhí)行以下命令爱只,等待下載及安裝
docker build -t friendlyhello .
控制臺(tái)輸出:
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 運(yùn)行image 將docker的80端口映射為宿主機(jī)的4000端口
docker run -p 4000:80 friendlyhello
瀏覽器請(qǐng)求localhost:4000
docker run -d -p 4000:80 friendlyhello 可以后臺(tái)運(yùn)行
docker container ls 查看容器ID
docker container stop 3ccdc83fda2d 使用容器ID結(jié)束
2 共享鏡像(操作方式類似于git)
2.1 首先再在https://cloud.docker.com網(wǎng)站注冊(cè)用戶
命令行使用用戶名密碼登錄
docker login
控制臺(tái)輸出:
Login Succeeded
2.2 上傳鏡像
樣例 docker tag image username/repository:tag
docker tag friendlyhello gaojingyuan/testrepo:v1
2.3 查看鏡像
docker image ls
控制臺(tái)輸出:
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 發(fā)布鏡像
docker push gaojingyuan/testrepo:v1
控制臺(tái)輸出:
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 執(zhí)行鏡像,如果本地沒有會(huì)從repository下載
docker run -p 4000:80 gaojingyuan/testrepo:v1
控制臺(tái)輸出:
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