[PYTHON] Deploy a Django application with Docker

** Docker ** is a technology that makes it easy to create, deploy, and execute applications using containers.

What is Docker?

Docker is a technology that makes it easy to create, deploy, and run applications using containers. Containers allow developers to package all the components they need for their application and later ship them as packages. It also allows you to run more applications on the same server.

With Docker, applications running on a container are isolated from each other, providing a higher level of security. In addition, Docker guarantees that each container has its own resources, so your application will only use the allocated resources.


Before you start this guide, you need the following:

--Alibaba Cloud ECS Linux instance. If you haven't set up your Linux instance yet, this article I will introduce various setup methods in.

Docker installation

Log in to the server using the ssh command.

$ ssh [email protected]

Update the Ubuntu package.

$ sudo apt-get update

Install the latest version of Docker with the following command.

$ sudo apt-get install docker

To verify that Docker is installed correctly, run the following command.

$ sudo docker run hello-world

If done correctly, the above command should allow the instance to download the test image and run it inside the container.

Containers and images in Docker

In the Alibaba Cloud ECS (https://www.alibabacloud.com/en/product/ecs) instance, you can use the image to create an ECS cluster with the same configuration. Similarly, the Docker container has an image. Conceptually, the two are very similar. It is based on the official Docker documentation.

A container image is a lightweight, standalone, executable package of software that contains everything you need to run it: code, runtimes, system tools, system libraries, settings.

You can see the running container by running $ sudo docker ps.

An image, on the other hand, is an inactive, immutable file, such as a snapshot of a container. The image is created with the build command, and when started with the run command, it creates a container.

Images can be viewed by running $ sudo docker images.

Build a Django application

First, let's install Django and create a Django application.

$ sudo pip install django==1.9
$ django-admin startproject djangoapp

Requirements file

Create a requirements file in the djangoapp directory to define the dependencies your application needs.

$ cd djangoapp
$ nano requirements.txt

Add the following dependency.



Creating a Docker file

Docker has the ability to read instructions from Docker files and build images automatically. The Docker file contains all the commands and instructions that Docker uses to build the image.

Let's define some basic commands used in the Dockerfile.

--FROM --Initializes a new build stage and sets the base image for subsequent instructions. Therefore, a valid Dockerfile must start with a FROM instruction. --RUN --Executes the specified command. --ADD --Copy the file to the container. --EXPOSE --Notifies Docker at run time that the container is listening on the specified network port. --CMD --Provides defaults for running containers. Now let's create a file called Dockerfile.

$ nano Dockerfile

First, let's define all the necessary properties in the Dockerfile. Defines the base image and maintainer name.

# base image 
FROM python:2.7

# File Author / Maintainer

Then copy the application folder inside the container and define the directory where CMD will be executed.

# Copy the application folder inside the container
ADD . /usr/src/app

# set the default directory where CMD will execute
WORKDIR /usr/src/app

Finally, set the default command and execute it.

CMD exec gunicorn djangoapp.wsgi:application --bind --workers 3

The final Dockerfile should look like this:

# set the base image 
FROM python:2.7

# File Author / Maintainer

#add project files to the usr/src/app folder
ADD . /usr/src/app

#set directoty where CMD will execute 
WORKDIR /usr/src/app

COPY requirements.txt ./

# Get pip to download and install requirements:
RUN pip install --no-cache-dir -r requirements.txt

# Expose ports

# default command to execute    
CMD exec gunicorn djangoapp.wsgi:application --bind --workers 3 

Build Docker image

Run the following command to build the docker image.

$ sudo docker build -t django_application_image .

Sending build context to Docker daemon   12.8kB
Step 1/7 : FROM python:2.7
 ---> 2863c80c418c
Step 2/7 : ADD . /usr/src/app
 ---> 09b03ff8466e
Step 3/7 : WORKDIR /usr/src/app
Removing intermediate container a71a3bf6af90
 ---> 3186c92adc85
Step 4/7 : COPY requirements.txt ./
 ---> 701c0be5e039
Step 5/7 : RUN pip install --no-cache-dir -r requirements.txt
 ---> Running in ed034f98db74
Collecting Django==1.9 (from -r requirements.txt (line 1))
  Downloading Django-1.9-py2.py3-none-any.whl (6.6MB)
Collecting gunicorn==19.6.0 (from -r requirements.txt (line 2))
  Downloading gunicorn-19.6.0-py2.py3-none-any.whl (114kB)
Installing collected packages: Django, gunicorn
Successfully installed Django-1.9 gunicorn-19.6.0
Removing intermediate container ed034f98db74
 ---> 1ffd08204a07
Step 6/7 : EXPOSE 8000
 ---> Running in 987b48e1a4ef
Removing intermediate container 987b48e1a4ef
 ---> ef889d6e8fcb
Step 7/7 : CMD exec gunicorn djangoapp.wsgi:application --bind --workers 3
 ---> Running in 4d929e361d0f
Removing intermediate container 4d929e361d0f
 ---> c6baca437c64
Successfully built c6baca437c64
Successfully tagged django_application_image:latest

The built image is in the machine's local Docker image registry. To see the images, run $ sudo docker images.

REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
django_application_image   latest              c6baca437c64        34 minutes ago      702MB

Run the app

$ sudo docker run -p 8000:8000 -i -t django_application_image

[2018-03-25 12:29:08 +0000] [1] [INFO] Starting gunicorn 19.6.0
[2018-03-25 12:29:08 +0000] [1] [INFO] Listening at:
[2018-03-25 12:29:08 +0000] [1] [INFO] Using worker: sync
[2018-03-25 12:29:08 +0000] [8] [INFO] Booting worker with pid: 8
[2018-03-25 12:29:08 +0000] [9] [INFO] Booting worker with pid: 9
[2018-03-25 12:29:08 +0000] [10] [INFO] Booting worker with pid: 10

A message appears at stating that ** gunicorn ** is servicing the app. You should be taken to your server's IP (ip_address: 8000) and you should see the Django welcome page.

To see a running container

$ sudo docker ps -a
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS                           PORTS                            NAMES
100695b41a0a        django_application_image   "/bin/sh -c 'exec gu…"   13 seconds ago      Exited (0) 4 seconds ago                                          hopeful_easley


When using Docker, you sometimes run into some problems. The first thing to do when an error occurs is to check the Docker log files.

Docker and other containers are powerful alternatives to traditional virtual machines for application development. For more information on how to run containers on Alibaba Cloud, please visit the Container Services page (https://www.alibabacloud.com/en/product/container-service).

Recommended Posts

Deploy a Django application with Docker
Build a web application with Django
A memo about building a Django (Python) application with Docker
Deploy a Django application on EC2 with Nginx + Gunicorn + Supervisor
Run a Python web application with Docker
I made a WEB application with Django
[Python] Build a Django development environment with Docker
Create a one-file hello world application with django
Deploy Django serverless with Lambda
Create a homepage with django
Web application creation with Django
Rails application building with Docker
How to deploy a Django application on Alibaba Cloud
Deploy a Django application on Google App Engine (Python3)
Launch a Python web application with Nginx + Gunicorn with Docker
Deploy an existing app with docker + pyenv-virtualenv + uwsgi + django
Deploy a Django app made with PTVS on Azure
Launch Django on a Docker container with docker-compose up
Build a development environment with Poetry Django Docker Pycharm
Build a Django development environment with Docker! (Docker-compose / Django / postgreSQL / nginx)
Measure Django application coverage with Coverage.py
[Memo] Build a development environment for Django + Nuxt.js with Docker
Creating a Flask server with Docker
Build a deb file with Docker
Deploy your Django application on Heroku
Django Tips-Create a ranking site with Django-
Twitter posting application made with Django
[Django] Build a Django container (Docker) development environment quickly with PyCharm
Run python3 Django1.9 with mod_wsgi (deploy)
Django + Docker
Make a filter with a django template
Create a Todo app with Django ① Build an environment with Docker
Application development with Docker + Python + Flask
Create a file uploader with Django
If you know Python, you can make a web application with Django
A series of amateur infrastructure engineers touching Django with Docker ③: Django admin
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
Launched a web application on AWS with django and changed jobs
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
A simple RSS reader made with Django
Get a local DynamoDB environment with Docker
Let's scrape a dynamic site with Docker
Creating a login screen with Django allauth
[Python] A quick web application with Bottle!
Application development using SQLite with Django (PTVS)
[Linux] Build a jenkins environment with Docker
A note on enabling PostgreSQL with Django
Create a web service with Docker + Flask
Launch Flask application with Docker on Heroku
[Linux] Build a Docker environment with Amazon Linux 2
Django Heroku Deploy 1
Internationalization with django
A series of amateur infrastructure engineers touching Django with Docker (2): Creating a model
Django Heroku Deploy 2
CRUD with Django
Django + Docker command
Until you publish (deploy) a web application made with bottle on Heroku
Build Django + NGINX + PostgreSQL development environment with Docker
I made a GUI application with Python + PyQt5
Start Django in a virtual environment with Pipenv
Start a simple Python web server with Docker