2048 game on AWS

Hosting 2048 on AWS using Docker

·

3 min read

2048 game on AWS

You might have played the famous and fun game 2048 at least once. In this article, we will embark on an AWS project that helps us understand Docker, Containerization, and how to write a Dockerfile to host our application on AWS. I will guide you through each step of the process, ensuring that by the end of this article, your game will be hosted on AWS.

Step 1 - Install Docker on your system:

Firstly, install Docker on your system by following the instructions in this link - Docker Installation Guide. After installation, verify if Docker is running by opening your terminal and typing the command:

docker --version

This command should return the installed Docker version. To check if the Docker daemon is up and running, use the command:

docker info

This command will provide a valid response if Docker is running, otherwise, try restarting your system.

Step 2 - Creating your Dockerfile:

Now that Docker is installed, let's create a Docker file which we will later upload to AWS to create our containerized application.

To use the 2048 game, we will utilize a Git repository available at 2048 GitHub Repository. Open your Visual Studio Code and create a new folder named "2048". Inside this folder, create a file named "Dockerfile" with "D" capitalized and all other letters in lowercase.

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://codeload.github.com/gabrielecirulli/2048/zip/master
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"]

Paste the above content into the "Dockerfile" you just created.

About the Docker file:

In this Docker file, we are using the official Ubuntu image from Docker Hub and installing dependencies like Nginx. Then, we fetch the game from the GitHub repository, unzip it, and configure Nginx. Port 80 is exposed to allow checking if the application is running.

Save the file, open the terminal, navigate to the "2048" folder, and run the command:

docker build -t 2048-game .

This command builds the image from the Docker file. "2048-game" is the image name, and the dot (.) represents the current directory.

To verify if the image is created, run:

docker images

Now that we have the image, let's create a container. Use the command:

docker run -d -p 80:80 <Image_Id>

For example:

docker run -d -p 80:80 331bcc124751

Replace the number with your Image ID. To check if the container is running, open your browser and visit localhost:80 (as port 80 is exposed in the Dockerfile). You should see your game up and running.

Finally, log in to your AWS account. We will use AWS Elastic Beanstalk to upload our Docker file. Once the environment is created, your application will be up and running on AWS.

Enjoy your game! :)