AWS Elastic Beanstalk Docker deployment

3 minute read Published:

Using Amazon Elastic Beanstalk to deploy the Pagure git forge, a Python web application

Elastic Beanstalk in Amazon Web Services provides a straightforward method of performing Docker application deployments by providing a Dockerrun.aws.json file describing your Docker container.

Here’s an example where I provide an EBS deployment file for the Fedora Pagure git forge.

Docker files

Contents of the Dockerfile I created for Pagure (which is a Python web service):

FROM fedora
MAINTAINER sevag.hanssian@gmail.com

RUN dnf update -y
RUN dnf install -y git python-virtualenv libgit2-devel libjpeg-devel gcc libffi-devel redhat-rpm-config
RUN mkdir -p {/pagure,/pagure_log} && git clone https://pagure.io/pagure.git /pagure

WORKDIR /pagure

RUN virtualenv pagure_env
RUN source ./pagure_env/bin/activate && pip install pygit2==$(dnf list installed libgit2 | awk '{print $2}' | cut -d$'\n' -f3 | awk -F'.' '{print $1"."$2".*"}') && pip install -r requirements.txt && python createdb.py && deactivate
RUN sed -i "s/127.0.0.1/0.0.0.0/g" runserver.py
ADD ./pagure_config.py /pagure/pagure_config.py
ADD ./entrypoint.sh /pagure/entrypoint.sh

EXPOSE 5000 

ENTRYPOINT ["/bin/bash", "/pagure/entrypoint.sh"]

Contents of entrypoint.sh:

mkdir -p {/pagure_data/repos,/pagure_data/docs,/pagure_data/forks,/pagure_data/tickets,/pagure_data/requests,/pagure_data/remotes}
/pagure/pagure_env/bin/python /pagure/runserver.py -c=/pagure/pagure_config.py 2>&1 >> /pagure_log/app.log

I only ever learned about the .dockerignore file from a tip provided on the EBS interface. Here are the contents of the .dockerignore file:

# Elastic Beanstalk Files
.elasticbeanstalk/*
.git
.gitignore

Dockerrun.aws.json

Now we bring all of the above together in an EBS file:

{
  "AWSEBDockerrunVersion": "1",
  "Ports": [
    {
      "WebserverPort": "5000"
    }
  ],
  "Volumes": [
    {
      "HostDirectory": "/var/pagure",
      "ContainerDirectory": "/pagure_data"
    }
  ],
  "Logging": "/pagure_log"
}

Run the example

Here’s how to use the EBS command-line tool to deploy Pagure from the directory containing the above files:

$ pip install awsebcli
$ eb init

At this step you will need to create an IAM user and provide the access and secret key. You also need to give the IAM user correct permissions:

iam-ebs

Here’s the output of the eb init step:

$ eb init
Select a default region
...
(default is 3): 1

Select an application to use
1) My First Elastic Beanstalk Application
2) [ Create new Application ]
(default is 2): 2

Enter Application Name
(default is "pagure-docker"): 
Application pagure-docker has been created.

It appears you are using Docker. Is this correct?
(y/n): y

Select a platform version.
1) Docker 1.9.1
2) Docker 1.7.1
3) Docker 1.6.2
(default is 1): 1
Do you want to set up SSH for your instances?
(y/n): y

Select a keypair.
...
(default is 2): 1

After this step we’ll create and deploy:

$ eb create pagure
Creating application version archive "app-f031-160516_044344".
Uploading pagure-docker/app-f031-160516_044344.zip to S3. This may take a while.
Upload Complete.
Environment details for: pagure
Application name: pagure-docker
Region: us-east-1
...
INFO: Successfully pulled fedora:latest
INFO: Successfully built aws_beanstalk/staging-app
INFO: Docker container 91a0c4a6d39f is running aws_beanstalk/current-app.
INFO: Environment health has transitioned from Pending to Ok. Initialization completed 6 seconds ago and took 8 minutes.

Now to make any changes to the environment, you can use eb deploy to update the environment:

$ eb deploy

Check the status to find the hosted CNAME:

$ eb status
Environment details for: pagure
  Application name: pagure-docker
  Region: us-east-1
  Deployed Version: app-f031-160516_045431
  Environment ID: e-25jskaq2ik
  Platform: 64bit Amazon Linux 2016.03 v2.1.0 running Docker 1.9.1 Tier: WebServer-Standard
  CNAME: pagure.xxxtyk4m9r.us-east-1.elasticbeanstalk.com
  Updated: 2016-05-16 08:56:04.675000+00:00
  Status: Ready
  Health: Green

Let’s navigate to $CNAME, or in this case, pagure.xxxtyk4m9r.us-east-1.elasticbeanstalk.com:

To browse the EBS data directory, you can SSH to the EC2 container and go to /var/pagure which is the volume we chose to mount in the Dockerrun.aws.json file.

Finally, to bring down the example, run eb terminate:

$ eb terminate
The environment "pagure" and all associated instances will be terminated.
To confirm, type the environment name: pagure
INFO: terminateEnvironment is starting.