docker 镜像制作–python程序
一、第1步:编辑Dockerfile
[root@docker-three ~]# mkdir /mydocker/
[root@docker-three ~]# cd /mydocker/
[root@docker-three mydocker]#
[root@docker-three mydocker]# vim Dockerfile
FROM python:2.7-slim
WORKDIR /app
ADD . /app
RUN pip install --trusted-host pypi.python.org -r requirements.txt
EXPOSE 80
ENV NAME World
ENV AUTHOR cali
CMD ["python","app.py"]
FROM python:2.7-slim #导入基础镜像,镜像里已经安装了python 2.7
WORKDIR /app #进入到容器后,会进入的文件夹
ADD . /app #复制linux系统当前目录下的内容到容器的/app目录下 类似于执行docker cp
RUN pip install --trusted-host pypi.python.org -r requirements.txt #在容器内部执行的命令
EXPOSE 80 #容器暴露80端口,监听80端口
ENV NAME World #定义了环境变量NAME赋值world
ENV AUTHOR cali #定义了环境变量AUTHOR赋值cali
CMD [“python”,“app.py”] #容器启动的时候执行命令 python app.py
二、第2步:编辑requirements.txt文件
[root@docker-three mydocker]# vim requirements.txt
Flask
Redis
三、第3步:编辑app.py文件,程序文件
[root@docker-three mydocker]# vim 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)
四、第4步:生成镜像文件
[root@docker-three mydocker]# docker build -t sc_friendlyhello_1 .
Sending build context to Docker daemon 4.608kB
Step 1/8 : FROM python:2.7-slim
2.7-slim: Pulling from library/python
123275d6e508: Pull complete
dd1cd6637523: Pull complete
0c4e6d630f2c: Pull complete
13e9cd8f0ea1: Pull complete
Digest: sha256:6c1ffdff499e29ea663e6e67c9b6b9a3b401d554d2c9f061f9a45344e3992363
Status: Downloaded newer image for python:2.7-slim
---> eeb27ee6b893
Step 2/8 : WORKDIR /app
---> Running in ad134fad2143
Removing intermediate container ad134fad2143
---> a56e0732d466
Step 3/8 : ADD . /app
---> 197f82e55c98
Step 4/8 : RUN pip install --trusted-host pypi.python.org -r requirements.txt
---> Running in 937b101042e1
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting Flask
Downloading Flask-1.1.4-py2.py3-none-any.whl (94 kB)
Collecting Redis
Downloading redis-3.5.3-py2.py3-none-any.whl (72 kB)
Collecting click<8.0,>=5.1
Downloading click-7.1.2-py2.py3-none-any.whl (82 kB)
Collecting Werkzeug<2.0,>=0.15
Downloading Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB)
Collecting Jinja2<3.0,>=2.10.1
Downloading Jinja2-2.11.3-py2.py3-none-any.whl (125 kB)
Collecting itsdangerous<2.0,>=0.24
Downloading itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB)
Collecting MarkupSafe>=0.23
Downloading MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl (24 kB)
Installing collected packages: click, Werkzeug, MarkupSafe, Jinja2, itsdangerous, Flask, Redis
Successfully installed Flask-1.1.4 Jinja2-2.11.3 MarkupSafe-1.1.1 Redis-3.5.3 Werkzeug-1.0.1 click-7.1.2 itsdangerous-1.1.0
WARNING: You are using pip version 20.0.2; however, version 20.3.4 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
Removing intermediate container 937b101042e1
---> 341c8f7d0e86
Step 5/8 : EXPOSE 80
---> Running in e2ef7e0e133d
Removing intermediate container e2ef7e0e133d
---> 0bc3ddbfe8ee
Step 6/8 : ENV NAME World
---> Running in b02c273bc86d
Removing intermediate container b02c273bc86d
---> 65c0874fa8ec
Step 7/8 : ENV AUTHOR cali
---> Running in f31bf49e3e78
Removing intermediate container f31bf49e3e78
---> d147e39a30e1
Step 8/8 : CMD ["python","app.py"]
---> Running in 97c7f95e007d
Removing intermediate container 97c7f95e007d
---> 7e98a2564967
Successfully built 7e98a2564967
Successfully tagged sc_friendlyhello_1:latest
[root@docker-three mydocker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sc_friendlyhello_1 latest 7e98a2564967 35 seconds ago 159MB
五、第5步:使用镜像,启动容器
[root@docker-three mydocker]# docker run -d -p 5080:80 --name xiaowei-hello-1 sc_friendlyhello_1
df00992ea1b7d8d3300e90e95710e4e68d4baa55346367de0461bcdddb587f62
[root@docker-three mydocker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
df00992ea1b7 sc_friendlyhello_1 "python app.py" 7 seconds ago Up 6 seconds 0.0.0.0:5080->80/tcp, :::5080->80/tcp xiaowei-hello-1
六、第6步:访问容器的web服务
curl或者chrome浏览器访问
宿主机ip:5080
因为redis数据库容器没有启动,flask web服务不能连接到redis数据库
七、第7步: 启动redis容器
[root@docker-three mydocker]# docker run -d -p 6379:6379 --name sc-redis-1 redis
Unable to find image 'redis:latest' locally
latest: Pulling from library/redis
a603fa5e3b41: Already exists
77631c3ef092: Pull complete
ed3847cf62b8: Pull complete
261a8b530567: Pull complete
7d9005a8af6d: Pull complete
828da1afb5be: Pull complete
Digest: sha256:1e3207c292225b6dd21cb74d59255748a50e8f739dd983040df38fa913927cf1
Status: Downloaded newer image for redis:latest
96ad406d892da84a3f3aad922f74116a9625363a43a03e4b6cd30f37af8c9b76
[root@docker-three mydocker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
96ad406d892d redis "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp sc-redis-1
df00992ea1b7 sc_friendlyhello_1 "python app.py" 4 minutes ago Up 4 minutes 0.0.0.0:5080->80/tcp, :::5080->80/tcp xiaowei-hello-1
cce420e5de69 nginx "/docker-entrypoint.…" 36 minutes ago Up 26 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp xiaowei-nginx-1
八、第8步: 再次启动一个自己制作镜像的容器,链接到redis容器
[root@docker-three mydocker]# docker run -d -p 5081:80 --link sc-redis-1:redis --name xiaowei-hello-2 sc_friendlyhello_1
21737a0e156536d8e3d39b6f73741711bf4188b2a6055e2cddb9271730b4e06c
[root@docker-three mydocker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
21737a0e1565 sc_friendlyhello_1 "python app.py" 2 seconds ago Up 2 seconds 0.0.0.0:5081->80/tcp, :::5081->80/tcp xiaowei-hello-2
96ad406d892d redis "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp sc-redis-1
df00992ea1b7 sc_friendlyhello_1 "python app.py" 6 minutes ago Up 6 minutes 0.0.0.0:5080->80/tcp, :::5080->80/tcp xiaowei-hello-1
cce420e5de69 nginx "/docker-entrypoint.…" 37 minutes ago Up 28 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp xiaowei-nginx-1
[root@docker-three mydocker]#
九、第9步:访问容器的web服务
curl或者chrome浏览器访问
宿主机ip:5081