In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains the "Dockerized Python Django application method", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's train of thought slowly in depth, together to study and learn "Dockerized Python Django application method" bar!
Step 1-install docker-ce
In this tutorial, we will install the docker-ce Community Edition from the docker repository. We will install docker-ce Community Edition and docker-compose (which supports compose file version 3).
Before installing docker-ce, use the apt command to install the required docker dependencies.
Sudo apt install-yapt-transport-httpsca-certificatescurlsoftware-properties-common
Now add the docker key and repository by running the following command.
Curl-fssl https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add- sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu$(lsb_release-cs) stable"
Install docker-ce
Update the warehouse and install docker-ce.
Sudo apt updatesudo apt install-y docker-ce
After the installation is complete, start the docker service and enable it to start each time the system boots.
Systemctl start dockersystemctl enable docker
Next, we will add a new user named omar and add it to the docker group.
Useradd-m-s / bin/bash omarusermod-a-g docker omar
Start docker
Log in as omar and run the docker command, as follows
Su-omardocker run hello-world
Make sure you get hello-world messages from docker
Check docker installation
The docker-ce installation is complete.
Step 2-install docker-compose
In this tutorial, we will use the latest docker-compose that supports version 3 of the compose file. We will install docker-compose manually
Use the curl command to download the latest version of docker-compose to the / usr/local/bin directory and use the chmod command to give it execute permission.
Run the following command:
Sudo curl-l https://github.com/docker/compose/releases/download/1.21.0/docker-compose-$(uname-s)-$(uname-m)-o / usr/local/bin/docker-composesudo chmod + x / usr/local/bin/docker-compose
Now check the docker-compose version.
Docker-compose version
Make sure you are installing the latest version of docker-compose 1.21
Install docker-compose
The latest version of docker-compose that supports version 3 of compose files has been installed.
Step 3-configure the project environment
In this step, we will configure the python django project environment. We will create a new directory, guide01, and make it the home directory for our project files, such as dockerfile, django projects, nginx configuration files, and so on.
Log in to the omar user.
Su-omar
Create a new directory guide01 and enter the directory.
Mkdir-p guide01cd guide01/
Now under the guide01 directory, create two new directories, project and config.
Mkdir project/ config/
Note:
Project directory: all our python django project files will be placed in this directory.
Config directory: the directory of project configuration files, including nginx configuration files, python pip requirements.txt files, and so on.
Create a new requirements.txt file
Next, use the vim command to create a new requirements.txt file in the config directory.
Vim config/requirements.txt
Paste the following configuration:
Django==2.0.4gunicorn==19.7.0psycopg2==2.7.4
Save and exit.
Create a nginx virtual host file django.conf
Create the nginx configuration directory under the config directory and add the virtual host configuration file django.conf.
Mkdir-p config/nginx/vim config/nginx/django.conf
Paste the following configuration:
Upstream web {ip_hash;server web:8000;} # portalserver {location / {proxy_pass http://web/;}listen 8000 X serverroomname localhost;location / static {autoindex on;alias / src/static/;}}
Save and exit.
Create dockerfile
Create a new file dockerfile in the guide01 directory.
Run the following command:
Vim dockerfile
Now paste the following dockerfile script:
From python:3.5-alpineenv pythonunbuffered 1run apk update & & apk add-- virtual build-deps gcc python-dev musl-dev & & apk add postgresql-dev bashrun mkdir / configadd / config/requirements.txt / config/run pip install-r / config/requirements.txtrun mkdir / srcworkdir / src
Save and exit.
Note:
We want to build an alpine linux-based docker image for our django project, and alpine is the smallest version of linux. Our django project will run on alpine linux with python 3.5 and add the postgresql-dev package to support the postgresql database. Then we will use the python pip command to install all the python packages listed on requirements.txt and create a new directory / src for our project.
Create a docker-compose script
Use the vim command to create the docker-compose.yml file in the guide01 directory.
Vim docker-compose.yml
Paste the following configuration:
Version: '3'services:db:image: postgres:10.3-alpinecontainer_name: postgres01nginx:image: nginx:1.13-alpinecontainer_name: nginx01ports:- "8000 postgres:10.3-alpinecontainer_name 8000" volumes:-. / project:/src-. / config/nginx:/etc/nginx/conf.ddepends_on:- webweb:build: .container _ name: django01command: bash-c "python manage.py makemigrations & & python manage.py migrate & & python manage.py collectstatic-- noinput & & gunicorn hello_django. Wsgi-b 0.0.0.0 project:/srcexpose:- 8000 "depends_on:- dbvolumes:-. / project:/srcexpose:-" 8000 "restart: always
Save and exit.
Note:
Using this docker-compose file script, we will create three services. Create a database service called db using the alpine linux version of postgresql, again use the alpine linux version of nginx to create the nginx service, and use the custom docker image generated from dockerfile to create our python django container.
Configure the project environment
Configure the django project
Copy the django project file to the project directory.
Cd ~ / djangocp-r * ~ / guide01/project/
Go to the project directory and edit the application settings settings.py.
Cd ~ / guide01/project/vim hello_django/settings.py
Note:
We will deploy a simple django application named "hello_django".
In the allow_hosts line, add the service name web.
Allow_hosts = ['web']
Now change the database settings, and we will use the postgresql database to run the service named db, using the default user and password.
Databases = {'default': {' engine': 'django.db.backends.postgresql_psycopg2','name':' postgres','user': 'postgres','host':' db','port': 5432,}}
As for the static_root configuration directory, add this line to the end of the file line.
Static_root = os.path.join (base_dir, 'static/')
Save and exit.
Configure the django project
Now we are ready to build and run the django project under the docker container.
Step 4-build and run the docker image
In this step, we want to build a docker image for our django project using the configuration in the guide01 directory.
Enter the guide01 directory.
Cd ~ / guide01/
Now use the docker-compose command to build the docker image.
Docker-compose build
Run docker Mirror
Start all services in the docker-compose script.
Docker-compose up-d
Wait a few minutes for docker to build our python image and download the nginx and postgresql docker images.
Use docker-compose to build an image
When finished, use the following command to check the running container and list the docker image on the system.
Docker-compose psdocker-compose images
Now, you will run three containers on the system, listing the docker images, as shown below.
Docke-compose ps command
Our python django application is now running inside the docker container and has created a docker image that serves us.
Step 5-Test
Open a web browser and type the server address using port 8000. Mine is:.
Now you will see the default django home page.
Default django Project Home Page
Next, test the administration page by adding the / admin path to url.
Then you will see the django Administration login page.
Thank you for your reading, the above is the content of "Dockerization Python Django application method", after the study of this article, I believe you have a deeper understanding of the method of Dockerization Python Django application, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.