原创文章,欢迎转载。转载请注明:转载自 IT人故事会,谢谢!
原文链接地址: 『中级篇』Dockerfile实战(19)
如何从一个python程序打包成为image,然后上传到dockerhub,最后下载image,生成对应的container容器。走咱们一起走个流程下来。
python程序
编写python程序
mkdir flask-hello-worldcd flask-hello-world/ vim app.py
image.png
from flask import Flask app = Flask(__name__)@app.route('/')def hell(): return "hello docker public attention:idig88"if __name__=='__main__': app.run(port=8888)
运行python程序
python app.py
image.png
#安装flask插件sudo yum -y install epel-release sudo yum -y install python-pip pip install flask python app.py
image.png
编写Dockerfile文件
vim Dockerfile
FROM python:2.7 LABEL maintainer="zhugeaming"RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak RUN echo "deb http://mirrors.163.com/debian/ jessie main non-free contrib" >/etc/apt/sources.list RUN echo "deb http://mirrors.163.com/debian/ jessie-proposed-updates main non-free contrib" >>/etc/apt/sources.list RUN echo "deb-src http://mirrors.163.com/debian/ jessie main non-free contrib" >>/etc/apt/sources.list RUN echo "deb-src http://mirrors.163.com/debian/ jessie-proposed-updates main non-free contrib" >>/etc/apt/sources.list RUN apt-get update RUN pip install flask COPY app.py /app/ WORKDIR /app EXPOSE 8888 CMD ["python","app.py"]
image.png
docker image ls
运行container
docker run zhugeaming/flask-hello-world
#后台运行docker run -d zhugeaming/flask-hello-world
容器有2个一个是刚才执行,一个后台执行的,后台直接执行。不加入-d的已经退出了。
PS:本次就是做了个实战,通过四步
代码编写好
环境python环境
第三方依赖
执行命令
回贴