DevOps Project - 1

Create a game using Docker and deploy it on AWS using Elastic beanstalk

DevOps Project - 1

Project Overview

In this blog, I will show you how I created a docker image of a game and first ran it on my localhost and then how I deployed it on AWS using a service named AWS Elastic Beanstalk.

Tools and technologies used

  1. Docker

  2. VS Code

  3. AWS (Elastic Beanstalk service)

About the game

Here is the link to the GitHub repo of the game I used for my project, Click here. It is a simple 2048 game.

Let us start

1. Create a folder and write our Dockerfile

Let us first create a folder where we will write our Dockerfile. I named it 2048-game you can give it any name.

Now, Inside the folder create a Dockerfile. Note that 'D' in the work Dockerfile should be capitalized and the rest of the words in small. Because that is the standard convention of creating a Dockerfile.

Here is the Dockerfile,

Here is an explanation of all the commands we used in our Dockerfile.

FROM -> To specify the base image which can be pulled from a container registry

RUN -> Executes commands during the image build process.

EXPOSE -> Specifies the port to be exposed for the Docker container.

CMD -> To specify the default command that should be executed when a container based on that image is started. It's essentially the primary process that runs inside the container.

Also, here is an explanation of this Dockerfile.

  1. FROM ubuntu:22.04

    • This line specifies the base image to use for this Docker image, in this case, Ubuntu 22.04.
  2. RUN apt-get update

    • Updates the package repository information within the container.
  3. RUN apt-get install -y nginx zip curl

    • Installs the Nginx web server, the zip utility, and the curl tool within the container.
  4. RUN echo "daemon off;" >>/etc/nginx/nginx.conf

    • Configures Nginx to run in the foreground so that it doesn't detach from the terminal, which is useful in containerized environments.
  5. RUN curl -o /var/www/html/master.zip -L codeload.github.com/gabrielecirulli/2048/zi..

    • Downloads the "2048" game source code as a ZIP archive from GitHub.
  6. RUN cd /var/www/html/ && unzip master.zip && mv 2048-master/\ . && rm -rf 2048-master [master.zip*](master.zip)

    • Moves into the /var/www/html/ directory, unzips the downloaded ZIP archive, moves the contents of the unzipped directory to the current directory, and then removes the unnecessary files and directories.
  7. EXPOSE 80

    • Informs Docker that the container will listen on port 80 (the default HTTP port) when it runs.
  8. CMD ["/usr/sbin/nginx", "-c", "/etc/nginx/nginx.conf"]

    • Specifies the default command to be executed when the container starts.

    • Starts the Nginx web server, passing the -c flag to specify the configuration file.

2. Create a Docker image from that Dockerfile

Now that our Dockerfile is ready, we will create a Docker image from it.

Note that when we build a Dockerfile we get a docker image and when we run that docker image we get the docker container.

Use this command to build the Dockerfile,

docker build -t 2048-game .

Here, the -t flag is used to give a name to our docker image which is 2048-game.

The dot represents the current path. Remember that if you are not inside the directory where your Dockerfile is located then you have to pass the complete path to your Dockerfile instead of just the dot.

Now when you hit enter you will see that all the commands you mentioned in the Dockerfile will be executed one by one. You will see a screen similar to this,

Now use this command to list all the images and you will see our image which is 2048-game,

docker images

Here is what you will see,

3. Create the container from that docker image

Now that we have our docker image created from our Dockerfile, let's go and create a container out of that.

Run the command,

docker run -d -p 80:80 2048-game

Here, the -d flag is used to run the container in the detached mode.

The -p flag is used to open ports. Here we are using port 80 on the host and port 80 on the container.

Then give the image name or the image ID.

Now open a new tab and in the search bar search for localhost:80. You will be able to access the docker container. Here is what you will see,

Congratulations, you were successful in accessing the docker container you created from the Dockerfile.

4. Deploying Docker container on AWS

It's an achievement till now. But right now this docker container is in your localhost, so no one can access it except you.

This is why we are going to host it on AWS.

The AWS service which we are going to be using is AWS Elastic Beanstalk.

It is a service in AWS that lets you create applications on different platforms like Python, Node, Docker, etc.

Open the AWS management console, search for Elastic Beanstalk and click on Create application.

Then it will ask you to give a name to your application. Enter any name you want.

Then it will ask you to add tags. Give Demo as key and project as value.

Then it will ask you to enter platform details. Select Docker as the platform and enter other details as shown in the image below.

Then it will ask you about the application code, select 'Upload your code'. And enter the Version label as '2048-source'. You can give anything in the Version label.

Then select 'Local file' and upload the Dockerfile you created.

Now click on 'next', then 'skip to review' and then 'submit' at the bottom right.

You will see a screen like this where it says 'Elastic Beanstalk is launching your environment'.

In the Events section at the bottom, you can see the list of events happening.

Everything that our app needs to run will be created by AWS Elastic Beanstalk automatically like instances, load balancers, auto-scaling groups, etc.

After a few minutes, your environment will be ready. Now click on the link shown by the arrow in the image below to open the app. Using this link anyone in the world can access your application.

You will see your game.

If you see this screen, this means you have successfully deployed your application on docker using the AWS Elastic Beanstalk service.

Conclusion

Congratulations, we just completed our DevOps project.

What's next?

As I make more and more projects, I will add them to this DevOps project blog series.

Thanks for reading till the end.