In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you the content of a case study of Dockerized Python Django applications. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Docker is an open source project that provides an open platform for developers and system administrators to build, package applications into a lightweight container, and run anywhere. Docker automatically deploys the application in the software container.
Django is a Web application framework written in Python that follows the MVC (Model-View-Controller) architecture. It is free and released under an open source license. It is fast and is designed to help developers get their applications online as soon as possible.
In this tutorial, I'll show you step by step how to create a docker image for an existing Django application in Ubuntu 16.04. We will learn how to docker a Python Django application and then use a docker-compose script to deploy the application as a container to the docker environment.
To deploy our Python Django application, we need other docker images: a nginx docker image for the Web server and a PostgreSQL image for the database.
What are we going to do?
Install Docker-ce
Install Docker-compose
Configure the project environment
Build and run
test
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: http://ovh01:8000/.
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.
Http://ovh01:8000/admin/
Then you will see the Django Administration login page.
Django administration
The Docker Python Django application has completed successfully.
Thank you for reading! This is the end of this article on "case study of Dockerized Python Django applications". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, 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.
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.