Table of Contents:
Create and launch an EC2 instance
Install Docker on Amazon EC2 instance.
Create and Run the Dockerfile
Deploying application on ABS
This article provides an introduction to Docker, a popular technology used in software development and deployment. It explains essential Docker concepts, including Dockerfiles, Docker images, and Docker containers. By following the article, you will gain knowledge on how to create Docker files and use the necessary commands to build Docker images and run Docker containers.
Furthermore, the article offers a fun and interactive learning experience by guiding you through the process of deploying a Docker containerized application on the AWS cloud. Specifically, it walks you through the steps of creating a 2048 game using Docker and demonstrates how to deploy it on AWS.
By following along with this article, you will not only grasp the fundamental concepts of Docker but also gain hands-on experience in building and deploying a Dockerized application engagingly.
Launch an Amazon Linux EC2 instance
Select the instance type as t2.micro
Please make sure that the Security group should allow HTTPS and HTTP traffic:
Login with your EC2 instance in your local machine terminal:
Here i am using mac os zsh terminal
ssh -i /path/to/your/key.pem username@public-ip-or-DNS
Install Docker #yum install docker -y
[ec2-user@ip-172-31-6-90 ~]$ sudo su
[root@ip-172-31-6-90 ec2-user]# yum install docker -y
Start Docker Service #Sevice docker start
Create a folder which will hold the configuration and code
[root@ip-172-31-6-90 ec2-user]# mkdir 2048
[root@ip-172-31-6-90 ec2-user]# cd 2048
[root@ip-172-31-6-90 ec2-user]# vi dockerfile
Create a docker file # vi dockerfile
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y nginx zip curl ca-certificates
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
WORKDIR /var/www/html/
RUN curl -o master.zip -L --insecure https://github.com/gabrielecirulli/2048/archive/refs/heads/master.zip \
&& unzip master.zip \
&& mv 2048-master/* . \
&& rm -rf 2048-master master.zip
EXPOSE 80
CMD ["/usr/sbin/nginx", "-c", "/etc/nginx/nginx.conf"]
Here is the explanation of the code:
FROM ubuntu:22.04
: Specifies the base image for the Dockerfile as Ubuntu 22.04.RUN apt-get update && apt-get install -y nginx zip curl ca-certificates
: Updates the package lists and installs the packagesnginx
,zip
,curl
, andca-certificates
using theapt-get
package manager.RUN echo "daemon off;" >> /etc/nginx/nginx.conf
: Appends the "daemon off;" directive to the Nginx configuration file (/etc/nginx/nginx.conf
). This directive is used to prevent Nginx from running in the background and becoming a foreground process.WORKDIR /var/www/html/
: Sets the working directory inside the container to/var/www/html/
. This is where the 2048 game source code will be downloaded and extracted.RUN curl -o
master.zip
-L --insecure
https://github.com/gabrielecirulli/2048/archive/refs/heads/master.zip
\
: Uses thecurl
command to download the 2048 game source code in the form of a ZIP archive from a GitHub repository.&& unzip
master.zip
\
: Unzips the downloaded ZIP archive (master.zip
).&& mv 2048-master/* . \
: Moves the contents of the extracted directory (2048-master
) to the current directory (/var/www/html/
).&& rm -rf 2048-master
master.zip
: Removes the temporary files, including the extracted directory and the downloaded ZIP archive.EXPOSE 80
: Exposes port 80 to indicate that the container will listen on that port at runtime.CMD ["/usr/sbin/nginx", "-c", "/etc/nginx/nginx.conf"]
: Specifies the command to run when the container starts. In this case, it runs the Nginx server using the specified configuration file (/etc/nginx/nginx.conf
).
Each line performs a specific action to set up the environment and configure the container for running the 2048 game with Nginx.
Run the docker file.
The command docker build -t 2048 .
is used to build a Docker image based on the Dockerfile located in the current directory (.
)
[root@ip-172-31-6-90 2048]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
2048 latest 6f8f809ce664 21 hours ago 181MB
ubuntu 22.04 99284ca6cea0 3 weeks ago 77.8MB
The docker images
command is used to list the Docker images that are currently available on your system.
The command docker run -d -p 80:80
6f8f809ce664 is used to run a Docker container based on the specified image with the ID 6f8f809ce664. Here's what each part of the command means:
docker run
: This command is used to run a Docker container.-d
: The-d
flag stands for "detached" mode. It runs the container in the background, allowing you to continue using the command prompt.-p 80:80
: The-p
flag is used to publish and map ports between the host and the container. In this case, it maps port 80 of the host machine to port 80 of the container. This is necessary to access the web server running inside the container.6f8f809ce664: This is the ID of the Docker image you want to run as a container.
Now enter in your browser the Public IP address that was assigned to your EC2 instance. In my case it is 54.176.236.2
Deploying Application on Amazon elastic beanstalk :
To proceed with the deployment of your application on Amazon Elastic Beanstalk, it is necessary to download the Dockerfile from an EC2 instance to your local machine. This Dockerfile contains the necessary instructions to configure the environment and run your application within a container. By downloading the Dockerfile locally, you can review and make any necessary modifications before deploying it to Elastic Beanstalk. This step ensures that you have a local copy of the Dockerfile, allowing you to maintain control and versioning of your application's configuration.
Open your terminal and paste below command
scp -i /path/to/private/key.pem ec2-user@ec2-instance-ip:/path/to/dockerfile /path/on/local/machine
Replace the following placeholders with the appropriate values:
/path/to/private/key.pem
: The path to your private key file used to connect to the EC2 instance.ec2-user
: The username used to connect to the EC2 instance.ec2-instance-ip
: The public IP address or hostname of the EC2 instance./path/to/dockerfile
: The path to the Dockerfile on the EC2 instance./path/on/local/machine
: The directory path on your local machine where you want to save the Dockerfile.
Now open AWS console & search for elastic beanstalk:
application name: 2048-game
To proceed with the setup, you can leave all settings at their default values. Choose your preferred key or create a new one if needed. Proceed by clicking "Next" to continue. This will allow you to access the subsequent configuration options where you can make any necessary adjustments to suit your requirements. By initially keeping the settings at their default values, you can easily review and modify the configuration as needed without missing any crucial steps.
Review and build your elastic beanstalk environment, then wait for it to launch.
Copy the URL above it and load it in your browser and you will see your application running successfully in elastic beanstalk environment
We have accomplished a successful deployment of a gaming application by leveraging Docker and AWS Elastic Beanstalk (EBS). Throughout the process, we gained a comprehensive understanding of the setup and execution of various Docker commands. Additionally, we explored in-depth the configuration and deployment of applications within the EBS environment, ensuring robustness and fault tolerance to address any potential failures.