AWS Elastic Beanstalk with docker-compose.yml file

Muharrem K.
adessoTurkey
Published in
5 min readMar 8, 2022

Running your applications (multi-container) on the Docker platform on Amazon Linux 2 Amazon Machine Images (AMI) with a docker-compose.yml file with AWS Elastic Beanstalk Command Line Interface (EB CLI).

What is AWS Elastic Beanstalk?

AWS Elastic Beanstalk is a service that enables the deployment of applications/services written in certain programming languages, some application servers, and dockerized platforms. It also uses some web servers such as Nginx, Apache, Passenger, and Internet Information Services (IIS). At the beginning of 2022, the available platforms in AWS Elastic Beanstalk are .Net Core on Linux, .Net Windows Server, Docker, Glassfish, Go, Java, Node.js, Python, PHP, Ruby, Tomcat.

When code is uploaded, AWS Elastic Beanstalk handles the deployment which takes place according to some default configuration values. After the deployment is done you can modify your configuration. In this hands-on, we will use EB CLI to create a WordPress application (WordPress and MySQL containers) running on the Docker platform on Amazon Linux 2 Amazon Machine Images (AMI) with a docker-compose.yml file. EB CLI provides interactive commands that facilitate creating, updating, and monitoring environments from a local repository.

Prerequisites:

* AWS account,
* IAM permissions,
* AWS CLI,
* pip
* aws configure with AWS Credentials and region,
* EB CLI (pip install awsebcli --upgrade --user)
* Docker Desktop

Open a terminal and run the command below to see the EB CLI is installed, usage of “eb”, commands, and options to use with the “eb” command.

eb

The first command to initialize the current directory with the EB CLI and to create the application is “eb init”.

mkdir ~/ebsdemo && cd ~/ebsdemoeb init

Select your region (us-east-1) for the AWS Elastic Beanstalk application, name the application (myapp- the current directory name is set by default), and choose the platform (Docker) that the application will be run.

Select the platform branch (Docker running on 64bit Amazon Linux 2) where docker containers will be run. If you want to connect the instances that AWS Elastic Beanstalk creates, select the keypair.

In the current directory (~/ebsdemo), “eb init” command creates .gitignore and .elasticbeanstalk directories. Change to .elasticbeanstalk directory and check the config.yml file. You can see the general configuration of the WordPress application in the config.yml file.

cd ~/ebsdemo/.elasticbeanstalkcat config.yml

Pull the docker images from DockerHub with the following commands. Use these images in your docker-compose.yml file after tagging them properly.

docker pull amd64/wordpress && docker pull mysql --platform linux/x86_64

We will create two ECR repositories for the docker images of WordPress and MySQL.

aws ecr create-repository --repository-name wordpress
aws ecr create-repository --repository-name mysql

To take the URI of ECR repositories, run the command below and copy the “repositoryUri” of the WordPress and MySQL repositories. Then tag the local images for pushing them to ECR repositories.

aws ecr describe-repositories | grep repositoryUridocker tag <local repository name: local tag name> <ECR repository URI:latest>docker tag mysql:latest 609374022227.dkr.ecr.us-east-1.amazonaws.com/mysql:latest && docker tag amd64/wordpress:latest 609374022227.dkr.ecr.us-east-1.amazonaws.com/wordpress:latest

To log in to ECR repositories, an authorization token is needed. Run the commands below to log in to ECR repositories and to push the images to the ECR repositories. See “Login Succeeded” on the terminal after run the “aws ecr get-login-password …”.

aws ecr get-login-password --region <us-east-1> | docker login --username AWS --password-stdin <ECR repository URI without repository name>docker push <mysql ECR repository URI:tag> && docker push <wordpress ECR repository URI:tag>aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 609374022227.dkr.ecr.us-east-1.amazonaws.comdocker push 609374022227.dkr.ecr.us-east-1.amazonaws.com/mysql:latest && docker push 609374022227.dkr.ecr.us-east-1.amazonaws.com/wordpress:latest

Create the docker-compose.yml file with the following content or clone it from the GitHub repository. This docker-compose.yml file will create a WordPress container as frontend and a MySQL container as backend. Change image’s value with the properly tagged WordPress and MySQL images.

cd ~/ebsdemovim docker-compose.yml''
version: "3.9"

services:
db:
image: 609374022227.dkr.ecr.us-east-1.amazonaws.com/mysql:latest
volumes:
- db_data:/var/lib/mysql
- "${EB_LOG_BASE_DIR}/db:/var/log/mysql"
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress1234
MYSQL_DATABASE: wordpress
MYSQL_USER: admin
MYSQL_PASSWORD: wordpress12

wordpress:
depends_on:
- db
image: 609374022227.dkr.ecr.us-east-1.amazonaws.com/wordpress:latest
volumes:
- wordpress_data:/var/www/html
- "${EB_LOG_BASE_DIR}/wordpress:/var/log/wordpress"
ports:
- "80:80"
restart: always
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: admin
WORDPRESS_DB_PASSWORD: wordpress12
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
wordpress_data: {}
''

The next step is to create the environment where the application will run. Create an environment using the “eb create name-of-the-environment” command.

cd ~/ebsdemoeb create dev-env

It will archive the files in the current directory (~/ebsdemo), upload them to the S3 bucket named elasticbeanstalk-<region>-<AccountID> with the application name. Then it will start creating an environment for the application. The environment consists of a Security Group for EC2 instances, a Classic Load Balancer, a Security Group for Load Balancer, an Auto Scaling Group, two Auto Scaling policies and their CloudWatch alarm, a Launch Configuration, and IAM roles. If your EC2 instance IAM role doesn’t have managed policy AmazonEC2ContainerRegistryFullAccess, add it to the EC2 instance IAM role for accessing ECR repositories. You can open your application with the URL of the dev-env environment from the Elastic Beanstalk management console or just run the “eb open” command from the terminal. You can connect your EC2 instance with ssh and check docker containers are running. You can change your environment configuration from the Elastic Beanstalk management console.

eb open

Clean Up

Run the following command to delete your CloudFormation stack which is created by EB CLI.

eb terminate

You also need to delete the ECR repositories from CLI or AWS Management Console.

aws ecr delete-repository --repository-name wordpress --force 
aws ecr delete-repository --repository-name mysql --force

Conclusion

AWS Elastic Beanstalk and EB CLI are easily used to deploy applications/services without thinking about the infrastructure. In this article, we used a docker-compose.yml file for our application and ECR repositories for the docker images. While creating our application with EB CLI, we chose “us-east-1” as region, “myapp” as application name, “Docker” as the platform, “Docker running on 64bit Amazon Linux 2” as Docker platform branch. After pulling the images that we used in the docker-compose.yml file from DockerHub; we created the ECR repositories, tagged the images appropriately, and pushed them to the ECR repositories. Then we created the docker-compose.yml file in the ~/ebsdemo directory and launched the environment with the “eb create dev-env” command. AWS Elastic Beanstalk handled all the infrastructure issues in dev-env environment and ran the docker-compose commands on the EC2 instance. Lastly, we checked our containers in docker-compose.yml were running on the EC2 instance and accessed the application on the browser.

--

--