DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • TDD vs. BDD: Choosing The Suitable Framework
  • Design Patterns for Microservices: Ambassador, Anti-Corruption Layer, and Backends for Frontends
  • Explainable AI: Making the Black Box Transparent
  • How To Check IP Addresses for Known Threats and Tor Exit Node Servers in Java
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Deploying a Django Application to AWS EC2 Instance With Docker

Deploying a Django Application to AWS EC2 Instance With Docker

In AWS we have several ways to deploy Django (and not Django applications) with Docker. We can use ECS or EKS clusters. If we don't have one ECS or Kubernete...

Gonzalo Ayuso user avatar by
Gonzalo Ayuso
·
Jul. 20, 20 · Tutorial
Like (1)
Save
Tweet
Share
8.07K Views

Join the DZone community and get the full member experience.

Join For Free

In AWS, we have several ways to deploy Django (and not Django applications) with Docker. We can use ECS or EKS clusters. If we don't have one ECS or Kubernetes cluster up and running, maybe it can be complex. Today, I want to show how deploy a Django application in production mode within a EC2 host. Let's start.

The idea is create one EC2 instance (one simple Amazon Linux AMI AWS-supported image). This host doesn't initially have Docker installed. We need to install it. When we launch one instance, when we're configuring the instance, we can specify user data to configure an instance or run a configuration script during launch.

We only need to add this shell script to set up Docker:

Shell
xxxxxxxxxx
1
15
 
1
#! /bin/bash
2
yum update -y
3
yum install -y docker
4
usermod -a -G docker ec2-user
5
curl -L https://github.com/docker/compose/releases/download/1.25.5/docker-compose-`uname -s`-`uname -m` | sudo tee /usr/local/bin/docker-compose > /dev/null
6
chmod +x /usr/local/bin/docker-compose
7
service docker start
8
chkconfig docker on
9
10
rm /etc/localtime
11
ln -s /usr/share/zoneinfo/Europe/Madrid /etc/localtime
12
13
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
14
15
docker swarm init


We also need to attach one IAM role to our instance. This IAM role only need to allow us the following policies:

  • AmazonEC2ContainerRegistryReadOnly (because we're going to use AWS ECR as container registry).
  • CloudWatchAgentServerPolicy (because we're going to emit our logs to Cloudwatch).

Also, we need to set up a security group to allow incoming SSH connections to port 22 and HTTP connections (in our example to port 8000).

When we launch our instance, we need to provide a key-pair to connect via ssh. I like to put this key-pair in my .ssh/config:

Plain Text
xxxxxxxxxx
1
 
1
Host xxx.eu-central-1.compute.amazonaws.com
2
    User ec2-user
3
    Identityfile ~/.ssh/keypair-xxx.pem


To deploy our application we need to follow those steps:

  • Build our Docker images.
  • Push our images to a container registry (in this case ECR).
  • Deploy the application.

I've created a simple shell script called deploy.sh to perform all tasks:

Shell
xxxxxxxxxx
1
23
 
1
#!/usr/bin/env bash
2
3
set -a
4
[ -f deploy.env ] && . deploy.env
5
set +a
6
7
echo "$(tput setaf 1)Building docker images ...$(tput sgr0)"
8
docker build -t ec2-web -t ec2-web:latest -t $ECR/ec2-web:latest .
9
docker build -t ec2-nginx -t $ECR/ec2-nginx:latest .docker/nginx
10
11
echo "$(tput setaf 1)Pusing to ECR ...$(tput sgr0)"
12
aws ecr get-login-password --region $REGION --profile $PROFILE |
13
  docker login --username AWS --password-stdin $ECR
14
docker push $ECR/ec2-web:latest
15
docker push $ECR/ec2-nginx:latest
16
17
CMD="docker stack deploy -c $DOCKER_COMPOSE_YML ec2 --with-registry-auth"
18
echo "$(tput setaf 1)Deploying to EC2 ($CMD)...$(tput sgr0)"
19
echo "$CMD"
20
21
DOCKER_HOST="ssh://$HOST" $CMD
22
echo "$(tput setaf 1)Building finished $(date +'%Y%m%d.%H%M%S')$(tput sgr0)"


This script assumes that there's a deploy.env file with our personal configuration (AWS profile, the host of the EC2, instance, the ECR, and things like that):

Plain Text
xxxxxxxxxx
1
 
1
PROFILE=xxxxxxx
2
3
DOKER_COMPOSE_YML=docker-compose.yml
4
HOST=ec2-user@xxxx.eu-central-1.compute.amazonaws.com
5
6
ECR=9999999999.dkr.ecr.eu-central-1.amazonaws.com
7
REGION=eu-central-1


In this example, I'm using Docker Swarm to deploy the application. I want to play also with secrets. This dummy application doesn't have any sensitive information, but I've created one "ec2.supersecret" variable

Shell
xxxxxxxxxx
1
 
1
echo "super secret text" | docker secret create ec2.supersecret -


That's the docker-compose.yml file:

YAML
xxxxxxxxxx
1
41
 
1
version: '3.8'
2
services:
3
  web:
4
    image: 999999999.dkr.ecr.eu-central-1.amazonaws.com/ec2-web:latest
5
    command: /bin/bash ./docker-entrypoint.sh
6
    environment:
7
      DEBUG: 'False'
8
    secrets:
9
      - ec2.supersecret
10
    deploy:
11
      replicas: 1
12
    logging:
13
      driver: awslogs
14
      options:
15
        awslogs-group: /projects/ec2
16
        awslogs-region: eu-central-1
17
        awslogs-stream: app
18
    volumes:
19
      - static_volume:/src/staticfiles
20
  nginx:
21
    image: 99999999.dkr.ecr.eu-central-1.amazonaws.com/ec2-nginx:latest
22
    deploy:
23
      replicas: 1
24
    logging:
25
      driver: awslogs
26
      options:
27
        awslogs-group: /projects/ec2
28
        awslogs-region: eu-central-1
29
        awslogs-stream: nginx
30
    volumes:
31
      - static_volume:/src/staticfiles:ro
32
    ports:
33
      - 8000:80
34
    depends_on:
35
      - web
36
volumes:
37
  static_volume:
38
39
secrets:
40
  ec2.supersecret:
41
    external: true


And that's all. Maybe ECS or EKS are better solutions to deploy docker applications in AWS, but we also can deploy easily to one docker host in a EC2 instance that it can be ready within a couple of minutes.

AWS Docker (software) application Django (web framework)

Published at DZone with permission of Gonzalo Ayuso, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • TDD vs. BDD: Choosing The Suitable Framework
  • Design Patterns for Microservices: Ambassador, Anti-Corruption Layer, and Backends for Frontends
  • Explainable AI: Making the Black Box Transparent
  • How To Check IP Addresses for Known Threats and Tor Exit Node Servers in Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: