Detailed explanation of the process of docker packaging Python environment

Detailed explanation of the process of docker packaging Python environment

The steps of docker packaging Python environment are as follows:

1 Export the dependency packages under pip list

pip freeze > requirements.txt

Put the exported requirements.txt in the docker_test directory

docker_test is the code source folder to be packaged. The docker_test folder of this demo should have requirements.txt and app.py files

2. Write the Dockfile

Create a Dockerfile in the same directory as docker_test. The format is as follows:

#Base image based on FROM python:3.9.6

# Maintainer information MAINTAINER name [email protected]

# Add the code in the docker_test directory to the code folder in the image (there is a space between the two directory parameters)

ADD ./docker_test /code

# Set the code folder to be the working directory WORKDIR /code

# Installation support RUN pip install -r requirements.txt

#Run the app.py file when docker is runningCMD ["python","/code/app.py"]

3. Create an image

docker build -t imagename Dockerfilepath 

# Dockerfilepath: The name of the folder where the Dockerfile is located. The current directory is "." 

docker build -t d_t .

4 Run the image

docker run -t -i d_t

5 Operation Results

ps: Let's take a look at the docker packaged Python program

Steps: Prepare the environment - Copy the Python program - Start the program

For simplicity, our Python program is a Python file app.py

app.py

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
    return "hello docker"
if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5000)

Dockerfile

FROM python:2.7
LABEL maintainer="Peng Xiao<[email protected]>"
RUN pip install flask
COPY app.py /app/
WORKDIR /app
EXPOSE 5000
CMD ["python", "app.py"]

Build image and start container

docker build -t soymilk/python-test .
docker run -d soymilk/python-test

This is the end of this article about the process of packaging Python environment in docker. For more information about the process of packaging Python environment in docker, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to implement packaging in docker python
  • Detailed explanation of Docker package python commands
  • This article teaches you how to use Docker to package the Python runtime environment

<<:  How to solve the problem of absolutely positioned elements mysteriously disappearing or being blocked in IE6/7

>>:  The images in HTML are directly replaced by base64 encoded strings

Recommend

Detailed explanation of the use of CSS3 rgb and rgba (transparent color)

I believe everyone is very sensitive to colors. C...

Learning Vue instructions

Table of contents 1. v-text (v-instruction name =...

CSS3 property line-clamp controls the use of text lines

Description: Limit the number of lines of text di...

Node script realizes automatic sign-in and lottery function

Table of contents 1. Introduction 2. Preparation ...

Explanation of the working mechanism of namenode and secondarynamenode in Hadoop

1) Process 2) FSImage and Edits Nodenode is the b...

Detailed steps for installing Tomcat, MySQL and Redis with Docker

Table of contents Install Tomcat with Docker Use ...

Detailed graphic explanation of sqlmap injection

Table of contents 1. We found that this website m...

MySQL optimization tutorial: large paging query

Table of contents background LIMIT Optimization O...

How to use the Fuser command in Linux system

What is Fuser Command? The fuser command is a ver...

WeChat applet implements SMS login in action

Table of contents 1. Interface effect preview 2.u...

Learning about UDP in Linux

Table of contents 1. Introduction to UDP and Linu...

Summary of the use of TypeScript in React projects

Preface This article will focus on the use of Typ...

Solutions to MySQL batch insert and unique index problems

MySQL batch insert problem When developing a proj...

How to import Chinese data into csv in Navicat for SQLite

This article shares with you the specific method ...