The flask service is packaged into a docker image

Table of contents

1. Generate a configuration file in pycharm

1. Dockerfile

2. The requirements.txt file and its generation method

2. Execute the docker command on the command line to generate the image


1. Generate a configuration file in pycharm

1. Dockerfile

Dockerfile is a text file that contains instructions (Instruction) for building images. Each instruction builds a layer of image, so the content of each instruction is to describe how the layer image should be constructed. 

  • dockerfile is used to instruct the docker image build command to automatically build the source code of the Image
  • is a plain text file
  • The FROM command specifies the base container image on which the new image will be built.
  • WORKDIR is the working directory set in the container, and RUN CMD ENTRYPOINT is performed in this directory
  • ADD ./app copies all files in the directory where the Dockerfile is located to the /app directory in the container (COPY is also available)
  • The RUN command is executed to install project dependencies
  • EXPOSE exposes the ports in the container
  • CMD is the operation performed in the container when the container starts

Simple configuration:

 2. requirements.txt file and generation method

The requirements.txt file is an information list of the project's dependent packages and their corresponding version numbers, which records the dependencies installed by your project.

1, pip freeze method 

Terminal input: pip freeze > requirements.txt

If the project you are writing is written in a virtual environment, you can use this method, because this method will generate all the packages in your entire Python environment. If you are not using a virtual environment, use this method, you will find that the generated There are many packages that you don't need in it, so when docker installs dependent packages, many unnecessary packages will be installed.

2. pipreqs third-party library 

Install third-party libraries:

pip install pipreqs

Execute under the current project path:

pipreqs ./ --encoding=utf8 --force

Use pipreqs to automatically retrieve all components and their versions under the current project, and generate a requirements.txt file

--encoding=utf8 : to use utf8 encoding

--force : Mandatory execution, overwrite when requirements.txt exists in the generated directory 

./  : Which file generates the requirements.txt file

2. Execute the docker command on the command line to generate the image

000: First switch to the directory where the Dockerfile is located

build image

001: docker build -f Dockerfile -t image name .

boot image

002: docker run -p 8700:8700 image name

When the browser opens locahost:8700 and the correct content appears, it means success

003: Package the image into a .tar file

docker save -o name.tar image name

Other machines load and run this image

004: docker load -i name.tar   

Then docker run -p 8700:8700 image name

Guess you like

Origin blog.csdn.net/weixin_57742734/article/details/127033739