Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How centos8 uses Docker to deploy Django projects

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you how centos8 uses Docker to deploy Django projects, the content is easy to understand, I hope you can learn, after learning there will be gains, the following let the editor take you to take a look at it.

Introduction

Since the learning process is recorded, the current higher version is used.

Python version 3.8.3

Django version 3.0.6

Nginx version 1.17.10

Okay, after a brief introduction, let's get down to business.

Create a working directory

Create a working directory to store files such as projects, Dockerfile, etc.

Mkdir uwsgidocker

Give a brief description of each file

Docker-compose.yml: Docker Compose is a command-line tool provided by docker to define and run applications consisting of multiple containers. With compose, we can define each service of the application declaratively through the YAML file, and the application is created and started by a single command. I didn't use docker-compose.ymlDockerfile at first: it's a text file used to build an image, and the text contains instructions and instructions for building the image. My_django: is a newly created django project, mainly changing ALLOWED_HOSTS = [] to ALLOWED_HOSTS = ["*"] nginxconf: a folder containing Dockerfile files for nginx configuration and creating nginx images. Pip.conf: it's about the configuration of pip, mainly for pip to accelerate downloads. Uwsgi_conf.ini: configuration file for uwsgi

Note: in the settings.py file in the django project, you need to change ALLOWED_HOSTS = [] to ALLOWED_HOSTS = ["*"].

Make a uwsgi image

Make the image of uwsgi according to the Dockerfile file and run it.

FROM python:3.8.3# create directory RUN mkdir-p / usr/src/app# set working directory WORKDIR / usr/src/app# copy pip.conf file to / root/.pip/pip.confCOPY pip.conf / root/.pip/pip.conf# update pipRUN pip install-- upgrade pip# download django and uwsgi in a general project just download requirement.txtRUN pip install django & & pip install uwsgi# and copy all the files in the current directory Only copy the project, the uwsgi configuration file COPY. / usr/src/app/# starts uwsgiCMD uwsgi when run-- ini uwsgi_conf.ini# exposed port EXPOSE 80 8080 8000 8888

Uwsgi profile, official website

[uwsgi] # Project directory, since the startup file of chdir = my_django# uwsgi is written directly in the current directory, the wsgi.pymodule = my_django.wsgi# under the project allows the main thread to exist (true) master = number of true# processes processes = the port used to specify the running port of the project, and socket and http can be used I use http for easy viewing http = 0.0.0.0 socket 800 cycles http = 10.0.0.10 socket 800 cycles socket = / usr/src/app/my_django/uwsgi.sock# automatically cleans up the environment when the server exits, deleting the unix socket file and the pid file vacuum = true

All right, with two files, you can make a uwsgi image. Execute the following command to generate a mirror image.

Docker build-t myuwsgi. /

Use docker images to view images

Run uwsgi Mirror

Now that the image has been made, the next step is to run the image and view it in the browser.

Use the following command directly to expose the port for easy viewing.

Docker run-- rm-it-- name webuwsgi-p 8000 it 8000 myuwsgi

Running result

Next, you can access it in the browser. Enter ip and port to access 192.168.56.102Drex8000.

This proves that the uwsgi image has been made and can run successfully.

The next step is to make a nginx image for reverse proxy

Make a Nginx image

First enter the nginxconf directory, cd nginxconf/ is easy to operate, and then edit the Dockerfile file

FROM nginx# deletes the default nginx configuration file RUN rm-rf / etc/nginx/conf.d/default.conf# copies the configuration file in the current directory to COPY nginx.conf / etc/nginx/conf.d/nginx.confEXPOSE 80 in the / etc/nginx/conf.d/ directory

Edit the nginx.conf file

Server {# listens on port listen 80; # hostname server_name localhost; location / {include uwsgi_params; # uwsgi service ip and port. Proxy_pass http://192.167.0.2:8000; # uwsgi can directly use # uwsgi_pass 192.167.0.2 proxy_set_header X-Real-IP 8000 when using socket; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for } location / static {# static file alias / usr/share/nginx/html/static;}}

The IP of the uwsgi container can use the docker inspect container name

Docker inspect webuwsgi

View

All right, the next step is to make a nginx image. Use the following command to create an image.

Docker build-t mynginx. /

Use docker images to view images

Run Nginx Mirror

Now that the image has been made, it is time to run the image and view it in the browser.

Use the following command directly to expose the port for easy viewing.

Docker run-rm-it-p 80:80-name nginxweb mynginx

Running result

Next, you can access it in the browser. Enter ip to access 192.168.56.102 directly.

Okay, this is deploying the django project using uwsgi+nginx on docker.

Next we use uwsgi+nginx+docker-compose to deploy the django project.

Use Docker-compose

Edit the docker-compose.yml file, assign ip, and make minor changes in the nginx,uwsgi configuration file

Docker-compose.yml file

Version: '3'services:version:' 3'services: uwsgi: build: context:. / image: uwsgi restart: always networks: django: ipv4_address: 10.0.0.10 ports:-"8000 ports 8000" volumes:-/ root/uwsgidocker/:/usr/src/app/:rw command: uwsgi-- ini / usr/src/app/uwsgi_conf.ini nginx: image: myweb build: context:. / nginxconf ports:-"80:80"- "8080 root/uwsgidocker/nginxconf/nginx.conf:/etc/nginx/conf.d/nginx.conf:rw restart 8080" volumes:-/ root/uwsgidocker/nginxconf/nginx.conf:/etc/nginx/conf.d/nginx.conf:rw restart: always privileged: true networks: django: ipv4_address: 10.0.0.20networks: django: ipam: config:-subnet: 10.0.0.0 subnet 24

Uwsgi_conf.ini file, just change the ip.

[uwsgi] chdir = my_djangomodule = my_django.wsgiuid = rootgid = rootmaster = trueprocesses = "http = 0.0.0.0 my_djangomodule 800 socket = 10.0.0.10:8000socket = 10.0.0.10 my_djangomodule = 800 socket = / usr/src/app/my_django/uwsgi.sockvacuum = true

Nginx.conf file, same way, change ip

Server {listen 80; server_name localhost; location / {include uwsgi_params; # proxy_pass http://192.167.0.2:8000; # uwsgi_pass 192.167.0.2 proxy_add_x_forwarded_for; 8000; # proxy_set_header Host $host; # proxy_redirect off; # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # uwsgi_pass unix:/usr/src/app/my_django/uwsgi.sock; uwsgi_pass 10.0.0.10 Proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;} location / static {alias / usr/share/nginx/html/static;}}

All right, after making a few changes to the configuration, you can run it directly.

Docker-compose operation

1. Boot-build Image & & launch Container

Docker-compose up or docker-compose up-d running in the background

two。 Stop it

Docker-compose stop # stop the container from running

3. Stop and delete the container

Docker-compose down

4. Enter IP for access, and the result

Do you find it very convenient to use docker-compose? here I also recommend you to use this!

This is about how centos8 uses Docker to deploy Django projects. If you have learned knowledge or skills, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report