赞
踩
└── src
├── config.py
├── demo.py
├── main.py
└── requirements.txt
**需要记住定义的目录框架**,会在dockerfile文件引用,不对会出现 error:no such file or directory
[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报错。
if __name__==“__main__”:
uvicorn.run(app=“main:app”,host=“0.0.0.0”,port=9990,reload=True)
**docker打包login镜像**
[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运行程序**
[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端口。
[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 ::
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。