Docker deployment process of python

Keywords: Python Docker Ubuntu

The process of deploying python using Docker:

  • 1. The Docker hub pulls the python image.
  • 2. Create a container and add third-party libraries required for the project in the container
  • 3. Make the container in step 2 as a mirror
  • 4. Use images for intranet environments.

1. Pull the python basic image:

xiaoyuanzi@xiaoyuanzi-virtual-machine:~$ docker pull python:3.6

Use docker images to check whether the image we just pulled exists in the image warehouse.

xiaoyuanzi@xiaoyuanzi-virtual-machine:~$ docker images

2. Create a container and add the third-party library required by the project in the container

xiaoyuanzi@xiaoyuanzi-virtual-machine:~$ docker run -it python:3.6 /bin/bash
root@e2d28fdbcd53:/# python
Python 3.6.15 (default, Oct 13 2021, 09:43:57) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
  • -i: Interactive operation
  • -t: Terminal
  • python:3.6 : REPOSITORY:TAG
  • /bin/bash: Here we want an interactive Shell, so we use / bin/bash

Install relevant third-party packages:
Enter quit() to exit the Python terminal. In order to make the installation process faster, we use the domestic image to install pandas and opencv Python.

pip install pandas -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
pip install opencv-python -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

3. Create container

At this time, we need to save the container and make it into an image to facilitate us to import it into the Intranet environment.
Do not close the shell in step 2. Reopen a shell window for image creation.
Enter the following command to view the container ID and make the image according to the container ID: docker ps -a

xiaoyuanzi@xiaoyuanzi-virtual-machine:~$ docker ps -a
CONTAINER ID   IMAGE        COMMAND       CREATED          STATUS          PORTS     NAMES
fa5bc4eaf4de   python:3.6   "/bin/bash"   54 seconds ago   Up 53 seconds             gifted_brown

commit is followed by the ID corresponding to the opencv container where pandas was just installed, New_python and 3.6 correspond to REPOSITORY and TAG respectively, which can be named at will.

xiaoyuanzi@xiaoyuanzi-virtual-machine:~$ docker commit fa5bc4eaf4de new_python:3.6
sha256:a87c809c1c3a3fcb2fb655ca6d1f0e1b9ceae63c3036608f5e7f971529c53758

Image creation succeeded. Enter docker images to check whether it already exists:

xiaoyuanzi@xiaoyuanzi-virtual-machine:~$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
new_python   3.6       a87c809c1c3a   28 seconds ago   1.01GB
python       3.6       1498723101b5   5 days ago       902MB

At this time, you can close the shell window that cannot be closed as prompted above.
According to New_python, the image boot container, tests whether Python and installed pandas and pymysql can be used normally.

xiaoyuanzi@xiaoyuanzi-virtual-machine:~$ docker run -it new_python:3.6 /bin/bash
root@be029dd048d2:/# 

In order to use this image in Intranet and offline environments, package the image as a file (which needs to be packaged as a file ending in. tar suffix or. tar.gz) and migrate it to a specific project environment.
The format of the image packaging command is: docker save Repository: tag | gzip > image name

xiaoyuanzi@xiaoyuanzi-virtual-machine:~$ docker save new_python:3.6 |gzip > python_images.tar.gz

Python was generated under the current path_ Images.tar.gz, the file size is 370M, and the image file is made here. We only need to add python_images.tar.gz is used in offline environment or Intranet environment.

4. Use the image in the Intranet environment

By means of xshell or ftp, python_ Upload images.tar.gz to the target server. (for demonstration purposes, delete the original python related image)
After deleting the original Python image, view the image: docker images

xiaoyuanzi@xiaoyuanzi-virtual-machine:~$ docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

Now it's based on python_ Import the image from the imges.tar.gz image file. The command format is: docker load < tar package path, here is docker load < / home / Xiaoyuanzi / python_ images.tar.gz

xiaoyuanzi@xiaoyuanzi-virtual-machine:~$ docker load < /home/xiaoyuanzi/python_images.tar.gz
xiaoyuanzi@xiaoyuanzi-virtual-machine:~$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
new_python   3.6       a87c809c1c3a   14 minutes ago   1.01GB

You can see that the image has been successfully imported, and the REPOSITORY is consistent with the original TAG. Here we use the image to start the container and test Python and pandas third-party packages.

Test:

xiaoyuanzi@xiaoyuanzi-virtual-machine:~$ docker run -it new_python:3.6 /bin/bash
root@d10f5d34c765:/# python
Python 3.6.15 (default, Oct 13 2021, 09:43:57) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
>>> 

Test successful!

5. Problem summary

Problem 1: docker cannot delete the image

Error response from daemon: conflict: unable to remove repository reference "training/webapp" (must force) - container 2588f7bc4a2b is using its referenced image 6fae60ef3446

Solution 1:

https://blog.csdn.net/yyj108317/article/details/105875836?

Question 2: libGL.so.1 problem in container

ImportError: libGL.so.1: cannot open shared object file: No such file or directory

Solution 1:

root@e2d28fdbcd53:/# apt-get update
root@e2d28fdbcd53:/# Apt get install sudo / / the first two operations are only necessary when there is no sudo in the container
root@e2d28fdbcd53:/# sudo apt update
root@e2d28fdbcd53:/# sudo apt install libgl1-mesa-glx

Problem 3: pip version is too low

WARNING: You are using pip version 21.2.4; however, version 21.3 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.

solve:

root@e2d28fdbcd53:/# python -m pip install --upgrade pip

reference resources:

1. https://blog.csdn.net/gf19960103/article/details/109489632

Posted by plastik77 on Mon, 18 Oct 2021 20:04:09 -0700