如何從一個python程序打包成為image,然后上傳到dockerhub,最后下載image,生成對應(yīng)的container容器。走咱們一起走個流程下來。
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 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
運行containerdocker run zhugeaming/flask-hello-world
#后臺運行
docker run -d zhugeaming/flask-hello-world
容器有2個一個是剛才執(zhí)行,一個后臺執(zhí)行的,后臺直接執(zhí)行。不加入-d的已經(jīng)退出了。
PS:本次就是做了個實戰(zhàn),通過四步 1. 代碼編寫好 2. 環(huán)境python環(huán)境 3. 第三方依賴 4. 執(zhí)行命令
|