How to Deploy a Django Application with Docker

By Esther Vaati, Alibaba Cloud Tech Share Author

In this tutorial, we are going to learn about Docker and how to apply it to website development. We will be deploying a Django application with Docker on an Alibaba Cloud ECS instance.

What is Docker?

Docker is a technology that makes it easier to create, deploy, and run applications by using containers. Containers allow developers to package applications with all the components required by the applications and later ship them out as packages. It also makes it possible to get more apps running on the same server.

With Docker, you can be assured of a higher level of security since applications that are running on containers are isolated from each other. In addition, Docker ensures that each container has its own resources and therefore an application will only use the resources that are assigned to it.

Prerequisites

Before you begin this guide you’ll need the following:

  • An Alibaba Cloud ECS Linux instance. If you haven’t yet set up your Linux instance, this article shows you various ways to set it up.
  • Docker
  • Python 2.7

Install Docker

Login to your server using the ssh command.

$ ssh root@47.88.220.88

Update Ubuntu packages.

$ sudo apt-get update

Install the latest version of Docker with the following command.

$ sudo apt-get install docker

To verify that Docker has installed correctly run the following command.

$ sudo docker run hello-world

If performed correctly, the above commands should let your instance download a test image and run it in a container.

Containers and Images in Docker

On an Alibaba Cloud ECS instance, you can use images to create ECS clusters with identical configurations. Similarly, Docker containers have images. Conceptually, they are very similar. Based on the official Docker documentation:

A container image is a lightweight, stand-alone, executable package of a piece of software that includes everything needed to run it: code, runtime, system tools, system libraries, settings.

You can view running containers by running $ sudo docker ps.

An image, on the other hand, is an inert, immutable, file that’s essentially a snapshot of a container. Images are created with the build command, and they’ll produce a container when started with the run command.

You can view images by running $ sudo docker images.

Build a Django Application

First, let’s install Django and Create a Django application.

Requirements File

Create a requirements file inside the djangoapp directory and define the dependencies required by the application.

Add the following dependencies.

Create Docker file

Docker has the ability to build images automatically by reading instructions from a Dockerfile. A docker file contains all the commands and instructions that Docker uses to build images.

Let’s define some of the basic commands used in a Dockerfile.

  • FROM — initializes a new build stage and sets the Base Image for subsequent instructions. As such, a valid Dockerfile must start with a FROM instruction.
  • RUN — runs the command specified.
  • ADD — Copy a file(s) into the container.
  • EXPOSE — informs Docker that the container listens on the specified network ports at runtime.
  • CMD — provide defaults for an executing container.

Now let’s create a file named Dockerfile.

$ nano Dockerfile

Let’s begin by defining all the properties required in a Dockerfile. Define the base image and maintainer name.

Next, copy the application folder inside the container and define the directory where CMD will execute.

Finally, set the default command to execute.

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

Your final Dockerfile should now look like this.

Build the Docker Image

Run the following command to build the docker image.

Your built image is now in your machine’s local Docker image registry. You can check your image by running $ sudo docker images.

Run the App

You should see a message that gunicorn is serving your app at http://0.0.0.0:8000. Navigate to your servers IP (ip_address:8000) and you should see the Django welcome page.

To see running containers:

Conclusion

Occasionally you might face some challenges when using Docker. The first thing to do when you experience an error is to check Docker logs files as they provide some information on what might have gone wrong.

Docker and other containers are a powerful alternative to traditional virtual machines for application development. To learn more about running containers on Alibaba Cloud, visit the Container Service page.

Reference:

https://www.alibabacloud.com/blog/how-to-deploy-a-django-application-with-docker_581157?spm=a2c41.11423013.0.0

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store