Analysis of the process of deploying Python applications in Docker containers

Analysis of the process of deploying Python applications in Docker containers

Simple application deployment

1. Directory structure:

└── Pythonpro #Directory└── test.py #File└── requirements.txt #File└── Dockerfile #File

2. Write the Dockerfile file

# Based on the image
FROM python:3.6.4
# Create a code folder working directory/code
RUN mkdir /code
#Copy the current code file to the container/code
COPY ./code
# Install required packages
RUN pip install -r /code/requirements.txt -i https://pypi.douban.com/simple
# Specify the working directory of cmd/code
WORKDIR /code
#Commands executed when the container starts
CMD ["python","test.py"]

3. Create a container image

docker build -t test .

4. Run the container

docker run -it --name test --restart always --privileged=true python-test
--name: specifies the name of the container as python-test, where test is the image just built.

--restart: always The container is always restarted when it exits.

--privileged=true: The permissions required to execute files in the container.

Django application containerization

1. Directory structure, I assume that this directory exists in /home/Pythonpro.

└── Pythonpro #Directory└── manage.py #File└── Main Project #Directory└── apps #Directory└── requirements.txt #File└── Dockerfile #File└── run.sh #File

run.sh script

python /code/manage.py runserver 0.0.0.0:8000

2. Write the Dockerfile file

FROM python:3.6.4
RUN mkdir /code \
&&apt-get update \
&&apt-get -y install freetds-dev \
&&apt-get -y install unixodbc-dev
COPY ./code 
RUN pip install -r /code/requirements.txt -i https://pypi.douban.com/simple
WORKDIR /code
CMD ["/bin/bash","run.sh"]

3. Build an Image

docker build -t webtest .

4. Run the container

docker run -it -p 6500:8000 -v /home/Pythonpro:/code --name web --restart always --privileged=true webtest

-p: Map the container's port 8000 to the host's port 6500

-v: The host directory /home/Pythonprot is mapped to the container directory /code

--name: specifies the name of the container as web, the image just built by webtest

--restart: always The container is always restarted when it exits

--privileged=true: Permissions required to execute files in the container

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • About Python connecting to Cassandra container for query
  • How to build a deep learning environment running Python in Docker container
  • Detailed explanation of Python basic syntax containers
  • Python statistics hashable objects container Counter detailed explanation
  • Python container summary
  • Python container built-in general function operations

<<:  Markup language - CSS layout

>>:  Native JS to achieve digital table special effects

Recommend

CSS3 realizes the childhood paper airplane

Today we are going to make origami airplanes (the...

JavaScript implements an input box component

This article example shares the specific code for...

Docker installation of Nginx problems and error analysis

question: The following error occurred when insta...

How to solve the abnormal error ERROR: 2002 in mysql

Recently, an error occurred while starting MySQL....

Looping methods and various traversal methods in js

Table of contents for loop While Loop do-while lo...

Docker exec executes multiple commands

The docker exec command can execute commands in a...

How to purchase and initially build a server

I haven't worked with servers for a while. No...

Display ellipsis effect when table cell content exceeds (implementation code)

illustrate In front-end development, you often en...

Summary of the Differences between find() and filter() Methods in JavaScript

Table of contents Preface JavaScript find() Metho...

5 basic skills of topic page design (Alibaba UED Shanmu)

This topic is an internal sharing in the second h...

How to select all child elements and add styles to them in CSS

method: Take less in the actual project as an exa...

Detailed summary of MySQL and connection-related timeouts

MySQL and connection related timeouts Preface: To...