Meet Jake, a DevOps engineer at a mid-sized tech company. Jake’s team recently developed a “Hello World” JavaScript application that showcases the fundamentals of web development. However, they’ve identified a challenge: to ensure the application runs consistently across various environments, they need to containerize it using Docker.
In this article, we’ll walk through the problem statement, the challenges Jake faces, the tools he needs, and the steps he can take to containerize the application.
Jake’s company has a growing number of applications deployed across multiple environments (development, staging, production). The team noticed frequent discrepancies in how the application behaves due to differences in environment configurations. The solution is to containerize the application to create a consistent and portable runtime environment.
Jake’s task:
Jake faces a few key challenges:
Before Jake begins, he needs to ensure the following tools are installed on his local machine:
Here’s a step-by-step workflow for Jake to containerize the “Hello World” JavaScript application.
Jake can download app file and place it in the hello-world-app/ directory as shown below:
hello-world-app/
└── index.html
Jake creates a Dockerfile in the same directory:
hello-world-app/
└── Dockerfile
The Dockerfile would be as below:
# Step 1: Use a lightweight web server image
FROM nginx:alpine
# Step 2: Copy the application to the web server's default location
COPY index.html /usr/share/nginx/html/
# Step 3: Expose the default HTTP port
EXPOSE 80
# Step 4: Start the Nginx server
CMD ["nginx", "-g", "daemon off;"]
Jake builds the Docker image by running the following command in the terminal:
docker build -t hello-world-app .
This command tells Docker to build an image named hello-world-app using the current directory (.).
Jake runs the container to test the application:
docker run -d -p 8080:80 hello-world-app
This maps port 80 inside the container to port 8080 on his local machine.
Jake opens a browser and navigates to http://localhost:8080 to ensure the application is running successfully.
To keep the image size small:
nginx:alpine base image, which is lightweight.COPY instruction specific.If the company uses a container registry (e.g., Docker Hub or Amazon ECR), Jake can push the image for others to use:
docker tag hello-world-app jake/hello-world-app:latest
docker push jake/hello-world-app:latest
Through these steps, Jake successfully containerized the JavaScript application. The containerization ensures consistent behavior across environments and sets the foundation for automated deployments.
This simple exercise demonstrates the power of Docker and prepares Jake to tackle more complex applications in the future!