DEV Community

Cover image for Dockerize a Snake and Ladder Game Application in EC2

Dockerize a Snake and Ladder Game Application in EC2

Prerequisite

Refer https://dev.to/aws-builders/docker-fundamentals-on-amazon-ec2-a-practical-introduction-2l60

The html code which is used can be accessed here https://github.com/nandinicloudpractioner/snakeandladdergamewebapplication/blob/main/snake-and-ladder.html

Lets Dockerize the snake and ladder game application

I have the html file snake and ladder.html file which I have created using Amazon Q cli

Now Lets Dockerize that

Lets begin with creating the directories and storing the code

Create a folder in ec2 instance and store the code files

mkdir snake-ladder-game

Move into the folder
cd snake-ladder-game


Now we need to create two files and store it inside our folder
snake-ladder-game/
│── snake-and-ladder.html
│── Dockerfile

To create a file in ec2, Lets use a nano editor
Lets first create snake-and-ladder.html
Type the command to open nano editor
nano snake-and-ladder.html
Copy the code which is available in https://github.com/nandinicloudpractioner/snakeandladdergamewebapplication/blob/main/snake-and-ladder.html and press
Ctrl + O
Nano will ask:

File Name to Write: snake-and-ladder.html

👉 Just press Enter to confirm.
Exit nano by pressing

_Ctrl + X_

Now, Create a Dockerfile for the HTML Game
Create a Dockerfile:

nano Dockerfile
Add the content as per the below image

Copy the code and press
Ctrl + O
Nano will ask:

File Name to Write: Dockerfile

👉 Just press Enter to confirm.

Exit nano by pressing

_Ctrl + X_

Explanation of the Dockerfile

  • nginx:alpine – lightweight web server image
  • COPY – moves your HTML file into the container
  • EXPOSE 80 – opens the web server port

This Dockerfile turns your HTML file into a runnable web application.

Build the Docker Image

Run the following command from the project directory:

docker build -t snake-ladder-game .

This creates a Docker image named snake-ladder-game.

Lets check if the image is ready by using the command
docker images


Yes, snake-ladder-game image is ready

Run the Docker Container

Start the container using:

docker run -d -p 80:80 snake-ladder-game

  • 80 is the local port
  • 80 is the container port

Lets check the run the command

docker ps
to see active containers

Open your browser using:

http://<EC2-PUBLIC-IP>
Enter fullscreen mode Exit fullscreen mode

Make sure your EC2 security group allows inbound traffic on port 80.



Top comments (0)