当前位置:   article > 正文

dockerfile从零到一打包运行python程序_python 文件怎么dockerfile 运行(1)_docker 运行程序

docker 运行程序

└── src
├── config.py
├── demo.py
├── main.py
└── requirements.txt


**需要记住定义的目录框架**,会在dockerfile文件引用,不对会出现 error:no such file or directory



  • 1
  • 2
  • 3
  • 4
  • 5

[root@i-9d1eq6no login]# cat > Dockerfile << EOF
FROM centos-py3:v1
WORKDIR /opt/login
ADD . .
RUN pip3 install -r ./src/requirements.txt
EXPOSE 8000
CMD [“python3”,“./src/main.py”]
EOF


1、pip需用centos-py3 images中安装的pip3,python亦是。


2、EXPOSE 暴露的端口,需要跟main程序内port值一致,,host需要定义为正确的地址,也可以不做限制(0.0.0.0:port)防止访问出现502报错。



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

if __name__==“__main__”:
uvicorn.run(app=“main:app”,host=“0.0.0.0”,port=9990,reload=True)


**docker打包login镜像**



  • 1
  • 2
  • 3
  • 4
  • 5

[root@i-9d1eq6no login]# docker build -t login:v1 .
[+] Building 82.0s (9/9) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 177B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos-py3:v1 0.0s
=> [1/4] FROM docker.io/library/centos-py3:v1 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 273B 0.0s
=> [2/4] WORKDIR /opt/login 0.0s
=> [3/4] ADD . . 0.1s
=> [4/4] RUN pip3 install -r ./src/requirements.txt 81.6s
=> exporting to image 0.3s
=> => exporting layers 0.3s
=> => writing image sha256:79e45ab2730ddb8c6f3ce23c655cb258773ba7a17a780f97d1ee1936080e2690 0.0s
=> => naming to docker.io/library/login:v1 0.0s
[root@i-9d1eq6no login]# docker images | grep login
login v1 79e45ab2730d 45 seconds ago 825MB


**docker运行程序**



  • 1
  • 2
  • 3
  • 4
  • 5

[root@i-9d1eq6no login]# docker run -itd -p 9999:8000 --name login 79e45ab2730d
[root@i-9d1eq6no login]# docker ps | grep login
4092aa912662 79e45ab2730d “python3 ./src/main.…” 1 hours ago Up 5 hours 0.0.0.0:9999->8000/tcp, :::9999->8000/tcp login


**注意点:**  
 expose的端口值,只在容器层做了放开,运行的时候默认是bridge网络模式,宿主机也需要指定一个段=端口去docker-proxy main的程序。我在程序内指定是9999端口。



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

[root@i-9d1eq6no login]# netstat -anptl | grep 9999
tcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN 30928/docker-proxy
tcp6 0 0 :::9999 ::

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/632185
推荐阅读
相关标签