手记

『中级篇』Dockerfile实战(19)

>如何从一个python程序打包成为image,然后上传到dockerhub,最后下载image,生成对应的container容器。走咱们一起走个流程下来。

python程序

  • 编写python程序
    mkdir flask-hello-world
    cd flask-hello-world/
    vim app.py

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

#安装flask插件
sudo yum -y install epel-release
sudo yum -y install python-pip
pip install flask
python app.py

编写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"]

docker image ls

运行container

docker run zhugeaming/flask-hello-world

#后台运行
docker run -d zhugeaming/flask-hello-world

>容器有2个一个是刚才执行,一个后台执行的,后台直接执行。不加入-d的已经退出了。

PS:本次就是做了个实战,通过四步

  1. 代码编写好
  2. 环境python环境
  3. 第三方依赖
  4. 执行命令
0人推荐
随时随地看视频
慕课网APP