DEV Community

Cover image for Day 2/40 CKA Dockerize an Application
Emmanuel Oghre
Emmanuel Oghre

Posted on

Day 2/40 CKA Dockerize an Application

Dockerizing Your Project: A Step-by-Step Guide
Docker is an essential tool for modern software development, providing a consistent and isolated environment for your applications. In this guide, we will walk you through the process of dockerizing a sample Node.js application. By the end, you will have a Docker image that you can run anywhere, ensuring your application behaves the same in all environments.

Prerequisites
Docker Desktop: Download and install the Docker Desktop client from Docker's official website.
Simplify Setup: By providing a starting point, docker init helps developers get up and running with Docker more quickly, reducing the initial setup time and effort.

Sample Application: We will use a sample application for this demo. You can clone it from GitHub or use your project.
Step 1: Clone the Sample Repository
First, clone the sample repository:

git clone https://github.com/docker/getting-started-app.git
cd getting-started-app/
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a Dockerfile
A Dockerfile is a text file that contains instructions on how to build a Docker image. Create an empty Dockerfile in your project directory:

touch Dockerfile
Enter fullscreen mode Exit fullscreen mode

Using your preferred text editor, open the Dockerfile and add the following content:

Dockerfile

# Use the official Node.js 18 image from the Docker Hub
FROM node:18-alpine

# Set the working directory inside the container
WORKDIR /app

# Copy all files from the current directory to the working directory in the container
COPY . .

# Install dependencies using yarn
RUN yarn install --production

# Specify the command to run the application
CMD ["node", "src/index.js"]

# Expose port 3000 to the host
EXPOSE 3000
Enter fullscreen mode Exit fullscreen mode

Step 3: Build the Docker Image
With the Dockerfile in place, you can build the Docker image. Run the following command in your terminal:


docker build -t day02-todo .
Enter fullscreen mode Exit fullscreen mode

This command tells Docker to build an image named day02-todo using the instructions in the Dockerfile.

Step 4: Verify the Image
After the build process completes, verify that the image has been created and stored locally:


docker images

Enter fullscreen mode Exit fullscreen mode

You should see day02-todo listed among the available images.

Step 5: Push the Image to Docker Hub
To share your Docker image with others or deploy it to another environment, push it to Docker Hub. First, log in to Docker Hub:

docker login
Enter fullscreen mode Exit fullscreen mode

Tag the image with your Docker Hub username and repository name:

docker tag day02-todo:latest username/new-reponame:tagname
Enter fullscreen mode Exit fullscreen mode

Push the image to Docker Hub:

docker push username/new-reponame:tagname
Enter fullscreen mode Exit fullscreen mode

Step 6: Pull the Image to Another Environment
To use the image in another environment, you can pull it from Docker Hub:

docker pull username/new-reponame:tagname

Enter fullscreen mode Exit fullscreen mode

Step 7: Run the Docker Container
Start a container from your Docker image:

docker run -dp 3000:3000 username/new-reponame:tagname

Enter fullscreen mode Exit fullscreen mode

This command maps port 3000 of your host machine to port 3000 of the container, allowing you to access the application locally.

Step 8: Verify the Application
If everything was set up correctly, your application should be running and accessible at http://localhost:3000.

Step 9: Access the Container
If you need to enter the container for debugging or other purposes, use the following command:


docker exec -it containername sh
Enter fullscreen mode Exit fullscreen mode

Replace containername with the actual name or ID of your running container.

Conclusion
Congratulations! You have successfully dockerized a Node.js application. This Docker image can now be run anywhere, ensuring a consistent environment across development, testing, and production. For further exploration, consider using sandbox environments like Play with Docker or Play with Kubernetes.

Kindly refer:
youtube playlist CKA Full Course 2024 by Piyush Sachdeva for more hands-on.
Github Link: https://github.com/Emmy-code-dev/CKA-2024

Happy Dockerizing!

Top comments (0)