导出依赖
正常情况
1
| conda activate target_detect
|
1 2 3 4
| pip list --format=freeze > requirements.txt
pip freeze > requirements.txt
|
异常情况
- 大量出现依赖冲突时,可以在linux中用个docker实例来让项目能在虚拟环境中运行,再按正常情况导出依赖
1 2 3 4 5 6
| docker search anaconda
docker pull continuumio/anaconda3
docker images
|
1 2 3 4 5 6 7 8
|
docker run --name target_detect_xlh -idt continuumio/anaconda3 /bin/bash
docker exec -it target_detect_xlh /bin/bash
|
部署
文件准备
- 项目源文件:将开发的项目代码放大linux服务器上,如
/home/xlh/wrj
requirements.txt
:由上文方法可得,将该文件放到python项目的目录下,如/home/xlh/wrj/requirements.txt
- Dockerfile文件:创建Dockerfile文件(即Dockerfile,无后缀),放到项目目录下,如
/home/xlh/wrj/Dockerfile
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 29 30 31 32 33 34
|
FROM python:3.8
RUN mkdir /app
WORKDIR /app
COPY requirements.txt requirements.txt COPY Arial.ttf Arial.ttf COPY Arial.Unicode.ttf /root/.config/Ultralytics/
RUN apt-get update && apt-get install -y libgl1-mesa-glx
RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
COPY . .
ENV FLASK_APP app.py
CMD ["flask","run","-h","0.0.0.0","-p","5005"]
|
编译镜像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| cd /home/xlh/wrj
docker build -t xlh/target_detect .
docker images
docker run -d --name target_detect --restart=always -p 5030:5005 xlh/target_detect
docker ps
docker logs -f target_detect
docker exec -it target_detect /bin/bash
|
如何修改代码
将容器发布后,若想修改运行中的代码,则需要如下操作:
停止容器
1 2
| docker stop ffd56492d9d4
|
拷贝代码
1 2 3 4
| pwd
docker cp /home/xlh/wrj/app.py ffd56492d9d4:/app
|
启动容器
1 2 3 4
| docker start ffd56492d9d4
docker logs -f target_detect
|
防火墙问题
1
| iptables -A INPUT -p tcp --dport 7687 -j ACCEPT
|