{"id":24732774,"url":"https://github.com/gonzalo123/django-2-ec2","last_synced_at":"2025-10-17T10:28:05.843Z","repository":{"id":46846958,"uuid":"260898465","full_name":"gonzalo123/django-2-ec2","owner":"gonzalo123","description":"Deploying Django application to a AWS EC2 instance with Docker","archived":false,"fork":false,"pushed_at":"2021-09-22T18:57:37.000Z","size":13,"stargazers_count":4,"open_issues_count":3,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-03T12:12:36.987Z","etag":null,"topics":["aws","docker","ec2","python","swarm"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gonzalo123.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-03T11:39:17.000Z","updated_at":"2023-02-24T15:35:14.000Z","dependencies_parsed_at":"2022-09-16T23:12:10.422Z","dependency_job_id":null,"html_url":"https://github.com/gonzalo123/django-2-ec2","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/gonzalo123/django-2-ec2","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gonzalo123%2Fdjango-2-ec2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gonzalo123%2Fdjango-2-ec2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gonzalo123%2Fdjango-2-ec2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gonzalo123%2Fdjango-2-ec2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gonzalo123","download_url":"https://codeload.github.com/gonzalo123/django-2-ec2/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gonzalo123%2Fdjango-2-ec2/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279002406,"owners_count":26083374,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-09T02:00:07.460Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","docker","ec2","python","swarm"],"created_at":"2025-01-27T17:54:01.049Z","updated_at":"2025-10-10T01:32:53.620Z","avatar_url":"https://github.com/gonzalo123.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"### Deploying Django application to AWS EC2 instance with Docker\n\nIn AWS we have several ways to deploy Django (and not Django applicaions) 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 complicated. Today I want to show how deploy one Django application in production mode within a EC2 host. Let's start.\n\nI'm getting older to provision one host by hand I prefer to use docker. The idea is create one EC2 instance (one simple Amazon Linux AMI AWS-supported image). This host don't 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.\n \nWe only need to put this shell script to set up docker\n\n```shell script\n#! /bin/bash\nyum update -y\nyum install -y docker\nusermod -a -G docker ec2-user\ncurl -L https://github.com/docker/compose/releases/download/1.25.5/docker-compose-`uname -s`-`uname -m` | sudo tee /usr/local/bin/docker-compose \u003e /dev/null\nchmod +x /usr/local/bin/docker-compose\nservice docker start\nchkconfig docker on\n\nrm /etc/localtime\nln -s /usr/share/zoneinfo/Europe/Madrid /etc/localtime\n\nln -s /usr/local/bin/docker-compose /usr/bin/docker-compose\n\ndocker swarm init\n```\n\nWe also need to attach one IAM role to our instance. This IAM role only need to allow us the following policies:\n* AmazonEC2ContainerRegistryReadOnly (because we're going to use AWS ECR as container registry)\n* CloudWatchAgentServerPolicy (because we're going to emit our logs to Cloudwatch)\n\nAlso we need to set up a security group to allow incoming SSH conections to port 22 and HTTP conections (in our example to port 8000)\n\nWhen we launch our instance we need to provide a keypair to connect via ssh. I like to put this keypair in my .ssh/config\n\n\u003e .ssh/config\n``` \nHost xxx.eu-central-1.compute.amazonaws.com\n    User ec2-user\n    Identityfile ~/.ssh/keypair-xxx.pem\n```\n\nTo deploy our application we need to follow those steps:\n* Build our docker images\n* Push our images to a container registry (in this case ECR)\n* Deploy the application.\n\nI've created a simple shell script called deploy.sh to perform all tasks:\n```shell script\n#!/usr/bin/env bash\n\nset -a\n[ -f deploy.env ] \u0026\u0026 . deploy.env\nset +a\n\necho \"$(tput setaf 1)Building docker images ...$(tput sgr0)\"\ndocker build -t ec2-web -t ec2-web:latest -t $ECR/ec2-web:latest .\ndocker build -t ec2-nginx -t $ECR/ec2-nginx:latest .docker/nginx\n\necho \"$(tput setaf 1)Pusing to ECR ...$(tput sgr0)\"\naws ecr get-login-password --region $REGION --profile $PROFILE |\n  docker login --username AWS --password-stdin $ECR\ndocker push $ECR/ec2-web:latest\ndocker push $ECR/ec2-nginx:latest\n\nCMD=\"docker stack deploy -c $DOCKER_COMPOSE_YML ec2 --with-registry-auth\"\necho \"$(tput setaf 1)Deploying to EC2 ($CMD)...$(tput sgr0)\"\necho \"$CMD\"\n\nDOCKER_HOST=\"ssh://$HOST\" $CMD\necho \"$(tput setaf 1)Building finished $(date +'%Y%m%d.%H%M%S')$(tput sgr0)\"\n```\n\nThis 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)\n\n```\nPROFILE=xxxxxxx\n\nDOKER_COMPOSE_YML=docker-compose.yml\nHOST=ec2-user@xxxx.eu-central-1.compute.amazonaws.com\n\nECR=9999999999.dkr.ecr.eu-central-1.amazonaws.com\nREGION=eu-central-1\n```\n\nIn this example I'm using docker swarm to deploy the application. I want to play also with secrets. This dummy application don't have any sensitive information but I've created one \"ec2.supersecret\" variable\n\n```shell script\necho \"super secret text\" | docker secret create ec2.supersecret -\n```\n\nThat's the docker-compose.yml file:\n\n```yaml\nversion: '3.8'\nservices:\n  web:\n    image: 999999999.dkr.ecr.eu-central-1.amazonaws.com/ec2-web:latest\n    command: /bin/bash ./docker-entrypoint.sh\n    environment:\n      DEBUG: 'False'\n    secrets:\n      - ec2.supersecret\n    deploy:\n      replicas: 1\n    logging:\n      driver: awslogs\n      options:\n        awslogs-group: /projects/ec2\n        awslogs-region: eu-central-1\n        awslogs-stream: app\n    volumes:\n      - static_volume:/src/staticfiles\n  nginx:\n    image: 99999999.dkr.ecr.eu-central-1.amazonaws.com/ec2-nginx:latest\n    deploy:\n      replicas: 1\n    logging:\n      driver: awslogs\n      options:\n        awslogs-group: /projects/ec2\n        awslogs-region: eu-central-1\n        awslogs-stream: nginx\n    volumes:\n      - static_volume:/src/staticfiles:ro\n    ports:\n      - 8000:80\n    depends_on:\n      - web\nvolumes:\n  static_volume:\n\nsecrets:\n  ec2.supersecret:\n    external: true\n```\n\nAnd 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.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgonzalo123%2Fdjango-2-ec2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgonzalo123%2Fdjango-2-ec2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgonzalo123%2Fdjango-2-ec2/lists"}