Create a Game using Docker and Deploy to AWS using EBS

Create a Game using Docker and Deploy to AWS using EBS

In this DevOps Project we will - Create a Game using Docker and Deploy to AWS Cloud in Just 15 Minutes!" this article is excellent devops resource to learn Docker concepts like what is docker file, how to create docker file, commands to create docker image, docker container and then deploy the docker containerized application on AWS cloud in a fun way by creating a 2048 game.

This DevOps project tutorial will help beginnersa learn devops concept like building an app using docker and its deployment on AWS Elasticbeanstalk service.

Creating Dockerfile

Open the terminal in your LOCAL or AWS instance. We are using ubuntu here as the OS.

Create a folder which will hold the configuration and code.

mkdir 2048 cd 2048

Create a docker file

vim Dockerfile

FROM ubuntu:22.04

RUN apt-get update
RUN apt-get install -y nginx zip curl
RUN echo "daemon off;" >>/etc/nginx/nginx.conf
RUN curl -o /var/www/html/master.zip -L https://github.com/gabrielecirulli/2048/archive/refs/heads/master.zip
RUN cd /var/www/html/ && unzip master.zip && mv 2048-master/* . && rm -rf 2048-master master.zip

EXPOSE 80

CMD ["/usr/sbin/nginx","-c", "/etc/nginx/nginx.conf"]

Code Insight

FROM ubuntu:22:04
This line specifies the base image that the Dockerfile will use as its starting point. In this case, the image is ubuntu:22:04, which is a specific version of Ubuntu Linux.
RUN apt-get update

RUN apt-get install -y nginx zip curl

These two lines use the RUN instruction to execute commands within the image build process. The first command updates the package index for Ubuntu, while the second command installs three packages: Nginx (a popular open-source web server), zip (a utility for working with ZIP files), and curl (a command-line tool for transferring data over the network).

RUN echo "daemon off;" >>/etc/nginx/nginx.conf

This line appends the text "daemon off;" to the end of the Nginx configuration file, located at /etc/nginx/nginx.conf. This change ensures that Nginx will run in the foreground, allowing the Docker container to stay running as long as the Nginx process is active.

RUN curl -o /var/www/html/master.zip -L https://github.com/gabrielecirulli/2048/archive/refs/heads/master.zip

RUN cd /var/www/html/ && unzip master.zip && mv 2048-master/* . && rm -rf 2048-master master.zip

These two lines download a ZIP file from a specific URL using curl, and then unzip the contents of the ZIP file to the /var/www/html/ directory within the Docker image. The unzipped contents are moved to the root directory and then the original directory and ZIP file are deleted.

EXPOSE 80

This line specifies that the Docker container should listen on port 80. This is the default port for HTTP traffic and allows users to access the application through a web browser.

CMD ["/usr/sbin/nginx","-c", "/etc/nginx/nginx.conf"]

Finally, this line specifies the default command to be executed when the Docker container is launched. The command starts the Nginx web server with the specified configuration file, allowing the application to be served to users who access the container through a web browser.

Our file in VIM

Running our Dockerfile

Now we will Build and Run the the container from our dockerfile

docker build -t 2048-game .

docker images

docker run -d -p 80:80 118e43d4280c

Now run it on port 80(depending on wheather you have created it on localhost or ec2)

Deploying Application on EB

Create EBS

Give Application name

Select platform as docker

Upload you dockerfile or use an s3 link of you docker file

Keep everything default and select you key (or create one) and keep everthing defualt and HIT next , you can configure changes based on your needs.

Review and create you EBS environment and wait for it to launch your environment.

Once your application is successfully deployed you will get below screen.

Copy the EBS URL above it and load it in your browser and you will see your application running successfully in EBS environment.

Summary :

We have successfully deployed a game application using docker and AWS EBS and seen all the configuration and understood docker commands in detail for the same and also seen how we configure and deploy applications on EBS environment thereby taking care of all the failures and fault tolerance.