第一步:编辑Dockerfile
[root@mxy-docker ~]# mkdir /mydocker
[root@mxy-docker ~]# cd /mydocker
[root@mxy-docker mydocker]#
[root@mxy-docker 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"]
第二步::编辑requirements.txt文件
[root@mxy-docker mydocker]# vim requirements.txt
Flask
Redis
第三步::编辑app.py文件,即程序文件
[root@mxy-docker 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)
第四步:生成镜像文件
[root@mxy-docker mydocker]# docker build -t sc_friendlyhello_1 .
Sending build context to Docker daemon 4.608kB
Step 1/9 : FROM python:2.7-slim
---> eeb27ee6b893
Step 2/9 : WORKDIR /app
---> Using cache
---> ed3a5d79cc04
Step 3/9 : ADD . /app
---> Using cache
---> 4f36b79e3431
Step 4/9 : VOLUME ["/data_flask"]
---> Using cache
---> c3dcb8190ab3
Step 5/9 : RUN pip install --trusted-host pypi.python.org -r requirements.txt
---> Running in 928613862e21
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 928613862e21
---> ff8adc66922e
Step 6/9 : EXPOSE 80
---> Running in b98dc3e45560
Removing intermediate container b98dc3e45560
---> 9f02633087c8
Step 7/9 : ENV NAME World
---> Running in 432fdd440b70
Removing intermediate container 432fdd440b70
---> da5a8e496578
Step 8/9 : ENV AUTHOR mxy
---> Running in c67016331ae3
Removing intermediate container c67016331ae3
---> 47452cbb6e66
Step 9/9 : CMD ["python","app.py"]
---> Running in 87440506cd07
Removing intermediate container 87440506cd07
---> a24400c200c3
Successfully built a24400c200c3
Successfully tagged sc_friendlyhello_1:latest
[root@mxy-docker mydocker]#
此处可能会出现如下错误,原因是容器不能上网,可以使用service docker restart重启容器来解决
查看一下完成的镜像
[root@mxy-docker mydocker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sc_friendlyhello_1 latest a24400c200c3 5 hours ago 159MB
第五步:使用镜像,启动容器
[root@mxy-docker mydocker]# docker run -d -p 5080:80 --name sc-hello-1 sc_friendlyhello_1
第六步:访问容器的web服务 ? ? ? ? ? ? ? 使用curl或者chrome浏览器访问 ? ? ? ? ? ? ? 宿主机ip:5080
?因为redis数据库容器没有启动,flask web服务不能连接到redis数据库
第7步: 启动redis容器
[root@mxy-docker mydocker]# docker run -d -p 6379:6379 --name sc-redis-1 redis
第八步: 再次启动一个自己制作镜像的容器,链接到redis容器
[root@mxy-docker mydocker]# docker run -d --name sc-hello-2 -p 5081:80 --link sc-redis-1:redis sc_friendlyhello_1
第九步:再次访问容器的web服务 ? ? ? ? ? ? ? curl或者chrome浏览器访问 ? ? ? ? ? ? ? 宿主机ip:5081
?这样我们就完成了一个镜像的制作!
|