How to Deploy a Django Application with Docker

简介: In this tutorial, we are going to learn about Docker, and 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.

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

Requirements File

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

$ cd djangoapp
$ nano requirements.txt

Add the following dependencies.

#requirements.txt

Django==1.9
gunicorn==19.6.0

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.

# base image 
FROM python:2.7

# File Author / Maintainer
MAINTAINER Esther

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

# 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 to execute.

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

Your final Dockerfile should now look like this.

# set the base image 
FROM python:2.7

# File Author / Maintainer
MAINTAINER Esther

#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
EXPOSE 8000

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

Build the 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 0.0.0.0:8000 --workers 3
 ---> Running in 4d929e361d0f
Removing intermediate container 4d929e361d0f
 ---> c6baca437c64
Successfully built c6baca437c64
Successfully tagged django_application_image:latest

Your built image is now in your machine’s local Docker image registry. You can check your image by running $ 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: http://0.0.0.0:8000
[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

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:

$ 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

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.

目录
相关文章
|
14天前
|
负载均衡 网络协议 开发者
掌握 Docker 网络:构建复杂的容器通信
在 Docker 容器化环境中,容器间的通信至关重要。本文详细介绍了 Docker 网络的基本概念和类型,包括桥接网络、宿主网络、覆盖网络和 Macvlan 网络等,并提供了创建、管理和配置自定义网络的实用命令。通过掌握这些知识,开发者可以构建更健壮和灵活的容器化应用,提高应用的可扩展性和安全性。
|
12天前
|
Linux iOS开发 Docker
Docker:容器化技术的领航者 —— 从基础到实践的全面解析
在云计算与微服务架构日益盛行的今天,Docker作为容器化技术的佼佼者,正引领着一场软件开发与部署的革命。它不仅极大地提升了应用部署的灵活性与效率,还为持续集成/持续部署(CI/CD)提供了强有力的支撑。
192 69
|
13天前
|
运维 Cloud Native Docker
云原生技术入门:Docker容器化实战
【9月更文挑战第20天】本文将引导你走进云原生技术的世界,通过Docker容器化技术的实战演练,深入理解其背后的原理和应用。我们将一起探索如何在云平台上利用Docker简化部署、扩展和管理应用程序的过程,并揭示这一技术如何改变现代软件的开发和运维模式。
|
7天前
|
Cloud Native 持续交付 Docker
云原生技术入门与实践:Docker容器化部署示例
【9月更文挑战第25天】在数字化转型的浪潮下,云原生技术成为推动企业创新的重要力量。本文旨在通过浅显易懂的语言,为初学者揭示云原生技术的核心概念及其应用价值。我们将以Docker容器为例,逐步引导读者了解如何将应用程序容器化,并在云端高效运行。这不仅是对技术趋势的跟随,更是对资源利用和开发效率提升的探索。
26 4
|
5天前
|
Kubernetes Linux 开发者
深入探索Docker容器化技术的奥秘
深入探索Docker容器化技术的奥秘
15 1
|
8天前
|
存储 Docker 容器
Docker中容器间的通信方式有哪些13
Docker中容器间的通信方式有哪些13
14 4
|
15天前
|
运维 Ubuntu Linux
深入理解并实践Docker容器化技术
深入理解并实践Docker容器化技术
41 6
|
18天前
|
Prometheus 监控 Cloud Native
docker安装prometheus+Granfan并监控容器
【9月更文挑战第14天】本文介绍了在Docker中安装Prometheus与Grafana并监控容器的步骤,包括创建配置文件、运行Prometheus与Grafana容器,以及在Grafana中配置数据源和创建监控仪表盘,展示了如何通过Prometheus抓取数据并利用Grafana展示容器的CPU使用率等关键指标。
|
3天前
|
网络协议 安全 开发者
掌握 Docker 网络:构建复杂的容器通信
在 Docker 容器化环境中,容器间的通信至关重要。本文详细介绍了 Docker 网络的基础知识,包括网络驱动、端口映射和命名等核心概念,并深入探讨了 Bridge、Host、Overlay 和 Macvlan 四种网络类型的特点及应用场景。此外,还提供了创建、连接、查看和删除自定义网络的命令示例,以及高级网络配置方法,如网络命名空间、DNS 解析和安全通信配置,帮助开发者构建更健壮的容器化应用。
下一篇
无影云桌面