Install and run Python files on docker

Table of contents

1. Install python in docker

1.1 Enter the image pull command

1.2 View mirror image

1.3 run

1.4 Check whether it is successful

1.5 View python version

Two, run the py file

2.1 Prepare the required files for operation

2.2 Prepare Folder

2.3 Probably looks like this

2.4 Package and upload to the server

 2.5 Build an image example

2.6 View mirror image

2.7 Optimize the size of the image.

2.8 Running the program


1. Install python in docker

1.1 Enter the image pull command

Note that we are using the alpine version here, which is not much different from the commonly used version. The main advantage is that it is small in size! !

docker pull python:3.8-alpine

1.2 View mirror image

1.3 run

docker run -itd python:3.8-alpine /bin/sh

1.4 Check whether it is successful

1.5 View python version

 docker exec -it py容器的id /bin/sh

exit ctrl+D

Two, run the py file

2.1 Prepare the required files for operation

Dockerfile and requirement.txt files required for packaging into images

The Dockerfile file reference is as follows

#基于的基础镜像
FROM python:3.8.16

# 设置code文件夹是工作目录
WORKDIR /codepy1

# 复制该文件到工作目录中,./requirements.txt表示当前目录下的requirements.txt文件。这里      # 的/code/requirements.txt是docker内部自动生成的工作目录,不用自己创建的,请注意。
COPY ./requirements.txt /codepy1/requirements.txt
 
# 禁用缓存并批量安装包(后面的链接是利用豆瓣源安装,速度会加快)
RUN pip install --no-cache-dir --upgrade -r /codepy1/requirements.txt -i https://pypi.douban.com/simple/
 
# 复制代码到工作目录
COPY ./app /codepy1/app

#运行
CMD ["python", "/app/main.py"]

How to generate request.txt please see this article:

Py automatically generates the requirements.txt file - Gui Tingting's Blog - CSDN Blog

2.2 Prepare Folder

Put the pre-prepared files into the newly created app folder, and the Dockerfile can be written when it is uploaded to the server.

2.3 Probably looks like this

2.4 Package and upload to the server

The suffix of Dockerfile is not, not txt, if there is a suffix, it will report an error, please pay attention

 2.5 Build an image example

docker build -t viewadd .

2.6 View mirror image

2.7 Optimize the size of the image.

We changed our original Dockerfile, the main modification is the version number

#基于的基础镜像
FROM python:3.8-alpine

# 设置code文件夹是工作目录
WORKDIR /codepy1

# 复制该文件到工作目录中,./requirements.txt表示当前目录下的requirements.txt文件。这里      # 的/code/requirements.txt是docker内部自动生成的工作目录,不用自己创建的,请注意。
COPY ./requirements.txt /codepy1/requirements.txt
 
# 禁用缓存并批量安装包(后面的链接是利用豆瓣源安装,速度会加快)
RUN pip install --no-cache-dir --upgrade -r /codepy1/requirements.txt -i https://pypi.douban.com/simple/
 
# 复制代码到工作目录
COPY ./app /codepy1/app

#运行
CMD ["python", "/app/main.py"]

after deleting the original

repackage

docker build -t viewadd .

2.8 Running the program

An error may be reported as follows.

fix bug

Modify the Dockerfile, notice what we changed?

Answer: The path where cmd runs, the following is an example

#基于的基础镜像
FROM python:3.8-alpine

# 设置code文件夹是工作目录
WORKDIR /codepy1

# 复制该文件到工作目录中,./requirements.txt表示当前目录下的requirements.txt文件。这里      # 的/code/requirements.txt是docker内部自动生成的工作目录,不用自己创建的,请注意。
COPY ./requirements.txt /codepy1/requirements.txt
 
# 禁用缓存并批量安装包(后面的链接是利用豆瓣源安装,速度会加快)
RUN pip install --no-cache-dir --upgrade -r /codepy1/requirements.txt -i https://pypi.douban.com/simple/
 
# 复制代码到工作目录
COPY ./app /codepy1/app

#运行
CMD python /codepy1/app/main.py 

Guess you like

Origin blog.csdn.net/qq_53679247/article/details/130552615