1. Create your own docker python container environment; support adding python packages and updating containers; offline packaging and image loading

1. Create your own docker python container environment

Reference: https://blog.csdn.net/weixin_42357472/article/details/118991485

First write the Dockfile, be careful not to have the suffix
Dockfile such as txt.

# 使用 Python 3.9 镜像作为基础
FROM python:3.9

# 设置工作目录
WORKDIR /app

# 复制当前目录中的 requirements.txt 文件到容器中
COPY requirements.txt requirements.txt

# 在容器中安装所需的包
RUN pip install -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com --no-cache-dir -r requirements.txt

# 复制当前目录中的所有文件到容器中
COPY . .

# 设置容器的默认命令
CMD ["/bin/bash"]

bulid:
The build time may be very long. You can view it in docker images after it is created.

docker build -t mypythonimage .

run:
test, CMD ["/bin/bash"] Because these images are interactive, the run will end immediately, and you can directly enter the container to view

docker run -it mypythonimage /bin/bash

Insert image description here

2. Support new python packages and update containers

For example, add other packages to image, faiss;
1) First enter the container

docker run -it mypythonimage /bin/bash

2) Container class installation

pip install faiss-cpu -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

3) Do not exit after installation. Change cmd to repackage and update the image.
ad6e1d2c5869 is the container ID running above, and mynewpythonimage is the name of the new image.

docker commit ad6e1d2c5869 mynewpythonimage

4) After the package is updated, you can re-enter the new mynewpythonimage to view. At this time, aiss-cpu is in the image that comes with it.
Insert image description here

3. Offline packaging box loading image

1) Save the image as a file on the source computer. Run the following command in the terminal:

docker save -o image.tar <镜像名称>

Replace <image name> with the name of the image you want to save. This command will save the image as a file named image.tar.

Transfer the saved image file to the target computer. You can use various methods such as using an external storage device, transferring over the network, or using a file sharing service.
Insert image description here

2) Load the image file on the target computer. After copying the image file to the appropriate location on the target computer, run the following command in the terminal:

docker load -i image.tar

This will load the image and make it available on the target computer.

Insert image description here

3) Confirm that the image has been loaded successfully. Run the following command in the terminal:

docker images

All available images will be listed, confirm that the image you saved previously has been successfully loaded on the target computer.

Guess you like

Origin blog.csdn.net/weixin_42357472/article/details/131953866