当前位置:   article > 正文

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

docker 启动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
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

大小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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

大小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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

大小1.5G

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

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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

docker stop s1
docker start s1
  • 1
  • 2

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

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2.2 gunicorn_myapp.conf

使用gunicorn代理myapp。

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

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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

2.4 supervisord_nginx.conf

supervisord代理nginx。

[supervisord]
nodaemon=true

[program:nginx]
command=/usr/sbin/nginx
  • 1
  • 2
  • 3
  • 4
  • 5

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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"]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/54936
推荐阅读
相关标签
  

闽ICP备14008679号