Package Python projects using Docker

1. Obtain the Python image file

Project encapsulation can be based on an existing image, which is available in Ubuntu

Docker pull Python :3.6.8-slim-stretch #Copy the code

Obtaining the Python image

2. Configure the Dockerfile file

Create a new Dockerfile in the root directory of your Python project. The Dockerfile name must be this or an error will be reported.

FROM python:3.6.8-slim-stretch WORKDIR /usr/src/app COPY requirements.txt./ RUN PIP install -- no-cache-dir-r requirements.txt COPY . . ENTRYPOINT ["python"] CMD ["./run.py"]Copy the code

3. Create a mirror

To create a mirror, run the following command:

Docker build-t python-project:1.0.Copy the code

Note the last “. Can’t omit, “. Represents the current directory

4. Run the image

To run the image, run the following command:

Docker run --rm -p 55:5000 --name pyProject python-project:1.0Copy the code

The parameters in this command have the following meanings

  • – rm: indicates that when the cli window is closed, the process running the image will be closed at the same time.
  • -p: used to set port mapping, that is, to map the port of the host computer to the port in the Docker container. How to encapsulate a Web service, so port mapping must be set;
  • – name: set an alias for the running image for later invocation

In addition, parameters that may be used frequently include:

  • -e: adds environment variables for the mirror
  • -d: Keeps the Web running in the background

5. Summary of common commands

Docker build -t pflask: 1.0 create mirror docker images | grep python view mirror docker run - rm - 5000 p: 5000 pflask: 1.0 running mirror docker Run --rm -p 55:5000 -e env1=evn1 pflask:1.0 Add the environment variable docker run -d pflask:1.0 Run the docker daemon docker ps -a check all the images that are currently running Docker stop IMAGE_ID Stops the image process based on the IMAGE_ID docker rm -f IMAGE_ID Deletes the image process based on the IMAGE_ID docker rmi IMAGE_ID Deletes the image based on the IMAGE_ID Docker logs -f tname according to mnemonic view log ss - anp | grep 5000 filter out from all ports 5000 curl -v "http://192.168.195.100:5000" test connection cat A256sum Hello. py Hash algorithm Dockerfile contents FROM python:3.6.8-slim-stretch WORKDIR /usr/src/app COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt COPY . . ENTRYPOINT ["python"] CMD ["/run.py"]Copy the code

 

See information:

1, blog.csdn.net/weixin_4290…

2, www.cnblogs.com/dhName/p/12…

3, www.cnblogs.com/sammy1989/p…

4, www.cnblogs.com/shoufu/p/12…