docker-16-安装python环境并从内部启动容器内的程序

1 制作镜像一

目录层级
在这里插入图片描述

1.1 myapp.py

from flask import Flask
app = Flask(__name__)

@app.route("/")
def test_link():
    return "the link is very good12"

if __name__=="__main__":
    app.run(host="0.0.0.0",port=5000,debug=False)

1.2 Dockerfile_py

(1)基础环境

FROM python:3.8
MAINTAINER bingbing <1978654@qq.com>
RUN pip3 install pip -U
RUN pip3 config set global.index-url http://mirrors.aliyun.com/pypi/simple
RUN pip3 config set install.trusted-host mirrors.aliyun.com
RUN pip3 install flask

WORKDIR /

ADD deploy/* deploy/
CMD python deploy/myapp.py

大小919M。
还可以安装更多的包
(2)基础+依赖包

FROM python:3.8
MAINTAINER bingbing <1978654@qq.com>
RUN pip3 install pip -U
RUN pip3 config set global.index-url http://mirrors.aliyun.com/pypi/simple
RUN pip3 config set install.trusted-host mirrors.aliyun.com
RUN pip3 install flask
RUN pip3 install scikit-learn==1.1.2
RUN pip3 install pandas==1.4.4
RUN pip3 install numpy==1.23.2
RUN pip3 install scipy==1.9.1
RUN pip3 install redis==4.3.4
RUN pip3 install pymysql==1.0.2
RUN pip3 install clickhouse-driver==0.2.4
RUN pip3 install apache-iotdb==0.13.1
RUN pip3 install func_timeout==4.3.5
WORKDIR /

ADD deploy/* deploy/
CMD python deploy/myapp.py

大小1.69G。
更改文件格式
(3)RUN指令合并

FROM python:3.8
MAINTAINER bingbing <1978654@qq.com>
RUN pip3 install pip -U && \
    pip3 config set global.index-url http://mirrors.aliyun.com/pypi/simple && \
    pip3 config set install.trusted-host mirrors.aliyun.com && \
    pip3 install flask && \
    pip3 install scikit-learn==1.1.2 && \
    pip3 install pandas==1.4.4 && \
    pip3 install numpy==1.23.2 && \
    pip3 install scipy==1.9.1 && \
    pip3 install redis==4.3.4 && \
    pip3 install pymysql==1.0.2 && \
    pip3 install clickhouse-driver==0.2.4 && \
    pip3 install apache-iotdb==0.13.1 && \
    pip3 install func_timeout==4.3.5
WORKDIR /

ADD deploy/* deploy/
CMD python deploy/myapp.py

大小1.5G。

基础环境		919M
基础+包		1.69G
RUN指令合并	1.5G

1.3 命令

# 制作镜像
docker build -f Dockerfile_py -t myservice:1.0 .
# 启动容器,映射目录
docker run -id --name=s1 -p 5000:5000 -v /root/servicetest/deploy:/deploy myservice:1.0 
# 访问服务       
curl http://192.168.1.9:5000/
# 进入容器
docker exec -it s1 /bin/bash

在宿主机/root/servicetest/deploy修改文件后,不会立刻生效,需要重启容器后生效。

docker stop s1
docker start s1

查看安装的包版本
在这里插入图片描述

2 制作镜像二

2.1 myapp.py

from flask import Flask
app = Flask(__name__)

@app.route("/")
def test_link():
    return "the link is very good12"

if __name__=="__main__":
    app.run(host="0.0.0.0",port=5000,debug=False)

2.2 gunicorn_myapp.conf

使用gunicorn代理myapp。

[program:gunicorn]
directory=/deploy
command=gunicorn --workers=2 -b 0.0.0.0:5000 myapp:app

2.3 nginx_gunicorn.conf

nginx代理gunicorn。

nginx_gunicorn.conf
server {
    listen      8087;

    location / {
        proxy_pass http://0.0.0.0:5000/;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

2.4 supervisord_nginx.conf

supervisord代理nginx。

[supervisord]
nodaemon=true

[program:nginx]
command=/usr/sbin/nginx

2.5 sources.list

更新apt源

deb http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb http://mirrors.aliyun.com/debian-security buster/updates main
deb-src http://mirrors.aliyun.com/debian-security buster/updates main
deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib

2.6 Dockerfile_py_apt

FROM python:3.8
MAINTAINER bingbing <1978654@qq.com>
RUN pip3 install pip -U
RUN pip3 config set global.index-url http://mirrors.aliyun.com/pypi/simple
RUN pip3 config set install.trusted-host mirrors.aliyun.com
RUN pip3 install flask
RUN pip3 install gunicorn

RUN mv /etc/apt/sources.list /etc/apt/sources.list.back
ADD sources.list /etc/apt

RUN apt-get update
RUN apt-get install -y supervisor
RUN apt-get install -y nginx
RUN apt-get clean

WORKDIR /

ADD deploy/* deploy/
ADD gunicorn_myapp.conf /etc/supervisor/conf.d/
ADD supervisord_nginx.conf /etc/supervisor/conf.d/

RUN rm /etc/nginx/sites-enabled/default
COPY nginx_gunicorn.conf /etc/nginx/sites-available/
RUN ln -s /etc/nginx/sites-available/nginx_gunicorn.conf /etc/nginx/sites-enabled/nginx_gunicorn.conf
RUN echo "daemon off;" >> /etc/nginx/nginx.conf

CMD ["/usr/bin/supervisord"]

2.7 命令

# 制作镜像
docker build -f Dockerfile_py_apt -t myservice:4.0 .
# 启动容器,映射目录
docker run -id --name=s4 -p 8087:8087 -v /root/servicetest/deploy:/deploy myservice:4.0 
# 访问服务       
curl http://192.168.1.9:8087/
# 进入容器
docker exec -it s4 /bin/bash
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮皮冰燃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值