mission details
Due to the limited computing power of the notebook, the python project needs to be deployed on the server.
After consulting a lot of information and stepping on countless pits, I will record the whole process today, hoping to help everyone.
configuration requirements
- The python project that needs to be deployed
- virtual machine/linus
- docker installation
- server port
general flow
1.Docker packaging
The final structure of the project:
docker_test
├── Dockerfile
├── pythonproject
│││ └── tset.py
│││ └── other configuration files
└── requirements.txt
Generate requirements.txt
cd into the main.py directory
or execute directly under the Terminal of pycharm
pipenv lock --requirements > requirements.txt
//或者
pip freeze >> requirements.txt
Get the requirement file
Writing of dockFile
It is recommended to write directly under linus
# 将官方 Python 运行时用作父镜像
FROM python:3.8.2
# 将工作目录设置为 /pythonproject
WORKDIR ./pythonproject
# 将当前目录内容复制到位于 /pythonproject 中的容器中
ADD . .
# 安装 requirements.txt 中指定的任何所需软件包
RUN pip install -r requirements.txt
# 在容器启动时运行 tset.py
CMD ["python3", "./pythonproject/tset.py"]
mirror generation
Under linus, cd to the Dockerfile file
docker build -t cowsay .
详细信息:
docker build -t imagename Dockerfilepath
# imagename:镜像名称,自定义
# Dockerfilepath:Dockerfile 所在文件夹名称,当前名录为 “.”
generate mirror
docker build -t imagename Dockerfilepath
run mirror
docker run --rm cowsay
--rm : 跑完就删除(因为有时候container比较占空间)
Here is a summary of some docker commands
docker image ls to list images
docker ps to list containers (running) (you can add -a to list all)
docker rm containerID delete container
docker rmi ImageID delete image
docker exec -it containerID bash into the container
Generation of IMAGE.tar (Image packaging)
docker save f660ca2347c0 > image.tar
//输出镜像文件到tar文件中
660ca2347c0是Image的ID号,可以通过 docker image ls 查看
So far, we have got IMAGE.tar
Next, upload it to the server and run
2. Server part
transfer files (non-ssh)
scp IMAGE.tar hostname@服务器地址: path
Explanation of the scp command
login server
ssh 账号@服务器地址
docker load tar file
docker image load <IMAGE.tar
docker run! (It's almost over!)
After decompression, you can see the docker image! Then execute the docker run operation normally!
(Still post the command and take a look)
docker run --rm cowsay
//cowsay只是一个自定义名字
You can refer to docker run