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.

目录
相关文章
|
6天前
|
Ubuntu API 网络虚拟化
ubuntu22 编译安装docker,和docker容器方式安装 deepseek
本脚本适用于Ubuntu 22.04,主要功能包括编译安装Docker和安装DeepSeek模型。首先通过Apt源配置安装Docker,确保网络稳定(建议使用VPN)。接着下载并配置Docker二进制文件,创建Docker用户组并设置守护进程。随后拉取Debian 12镜像,安装系统必备工具,配置Ollama模型管理器,并最终部署和运行DeepSeek模型,提供API接口进行交互测试。
129 15
|
6天前
|
网络协议 API Docker
Docker+consul容器服务的更新与发现
通过本文的介绍,我们详细探讨了如何结合Docker和Consul来实现容器服务的更新与发现。通过Consul的服务注册和发现功能,可以高效地管理和监控容器化服务,确保系统的高可用性和可扩展性。希望本文能帮助您在实际项目中更好地应用Docker和Consul,提高系统的可靠性和管理效率。
39 23
|
1月前
|
数据库 Docker 容器
docker容器为啥会开机自启动
通过配置适当的重启策略,Docker容器可以在主机系统重启后自动启动。这对于保持关键服务的高可用性和自动恢复能力非常有用。选择适合的重启策略(如 `always`或 `unless-stopped`),可以确保应用程序在各种情况下保持运行。理解并配置这些策略是确保Docker容器化应用可靠性的关键。
225 93
|
1月前
|
数据库 Docker 容器
docker容器为啥会开机自启动
通过配置适当的重启策略,Docker容器可以在主机系统重启后自动启动。这对于保持关键服务的高可用性和自动恢复能力非常有用。选择适合的重启策略(如 `always`或 `unless-stopped`),可以确保应用程序在各种情况下保持运行。理解并配置这些策略是确保Docker容器化应用可靠性的关键。
63 17
|
1月前
|
运维 Java 虚拟化
《docker基础篇:1.Docker简介》,包括Docker是什么、容器与虚拟机比较、能干嘛、去哪下
《docker基础篇:1.Docker简介》,包括Docker是什么、容器与虚拟机比较、能干嘛、去哪下
120 12
|
1月前
|
Ubuntu NoSQL Linux
《docker基础篇:3.Docker常用命令》包括帮助启动类命令、镜像命令、有镜像才能创建容器,这是根本前提(下载一个CentOS或者ubuntu镜像演示)、容器命令、小总结
《docker基础篇:3.Docker常用命令》包括帮助启动类命令、镜像命令、有镜像才能创建容器,这是根本前提(下载一个CentOS或者ubuntu镜像演示)、容器命令、小总结
160 6
《docker基础篇:3.Docker常用命令》包括帮助启动类命令、镜像命令、有镜像才能创建容器,这是根本前提(下载一个CentOS或者ubuntu镜像演示)、容器命令、小总结
|
1月前
|
Kubernetes Linux 虚拟化
入门级容器技术解析:Docker和K8s的区别与关系
本文介绍了容器技术的发展历程及其重要组成部分Docker和Kubernetes。从传统物理机到虚拟机,再到容器化,每一步都旨在更高效地利用服务器资源并简化应用部署。容器技术通过隔离环境、减少依赖冲突和提高可移植性,解决了传统部署方式中的诸多问题。Docker作为容器化平台,专注于创建和管理容器;而Kubernetes则是一个强大的容器编排系统,用于自动化部署、扩展和管理容器化应用。两者相辅相成,共同推动了现代云原生应用的快速发展。
203 11
|
2月前
|
Ubuntu Linux 开发工具
docker 是什么?docker初认识之如何部署docker-优雅草后续将会把产品发布部署至docker容器中-因此会出相关系列文章-优雅草央千澈
Docker 是一个开源的容器化平台,允许开发者将应用程序及其依赖项打包成标准化单元(容器),确保在任何支持 Docker 的操作系统上一致运行。容器共享主机内核,提供轻量级、高效的执行环境。本文介绍如何在 Ubuntu 上安装 Docker,并通过简单步骤验证安装成功。后续文章将探讨使用 Docker 部署开源项目。优雅草央千澈 源、安装 Docker 包、验证安装 - 适用场景:开发、测试、生产环境 通过以上步骤,您可以在 Ubuntu 系统上成功安装并运行 Docker,为后续的应用部署打下基础。
96 8
docker 是什么?docker初认识之如何部署docker-优雅草后续将会把产品发布部署至docker容器中-因此会出相关系列文章-优雅草央千澈
|
2月前
|
搜索推荐 安全 数据安全/隐私保护
7 个最能提高生产力的 Docker 容器
7 个最能提高生产力的 Docker 容器
197 35