Day 2/40 - How To Dockerize a Project

CKA Full Course 2024 ☸️

Hey everyone! 👋

Welcome to Day 2 of my 40 Days of Kubernetes journey! Today, I’m diving into the practical side of Docker: Dockerizing an application. 🐳

This post is all about writing a Dockerfile, containerizing an application, and managing it using essential Docker commands. I’m sharing my learnings, mistakes, and insights as I go—so let’s get started!


🚀 What You’ll Learn:

1. Dockerfile Demystified: What is a Dockerfile, and how to write one.

2. Dockerizing Your Application: Step-by-step containerization.

3. Docker Command Breakdown: Explanation of commonly used Docker commands.

4. Essential Docker Commands: How to manage containers effectively.


🛠️ Tools and Resources

If you want to follow along, here are some resources you can use:

1. Docker and Kubernetes Sandbox Environments:

Play with Docker

Play with Kubernetes

2. Download Docker Desktop:

Docker Desktop


🏗️ Step-by-Step: Dockerizing a Project

I decided to use a simple Node.js application for this task. You can either use your project or clone the one I used:

1. Clone the Git Repository

Run the following command to get the sample project:

git clone https://github.com/docker/getting-started-app.git  

cd getting-started-app/

2. Create a Dockerfile

Create an empty file named Dockerfile:

touch Dockerfile

3. Write the Dockerfile

Open the file in a text editor and add the following content:

FROM node:18-alpine  

WORKDIR /app  

COPY . .  

RUN yarn install --production  

CMD ["node", "src/index.js"]  

EXPOSE 3000

Explanation of Each Line:

FROM: Specifies the base image (Node.js in this case).

WORKDIR: Sets the working directory inside the container.

COPY: Copies the application files into the container.

RUN: Installs production dependencies.

CMD: Defines the command to run when the container starts.

EXPOSE: Exposes port 3000 for the app.

4. Build the Docker Image

Run the following command to build your Docker image:

docker build -t day02-todo .

5. Verify the Image

Check if the image was created successfully:

docker images

6. Push the Image to Docker Hub

Create a repository on Docker Hub, then run these commands:

docker login  

docker tag day02-todo:latest username/repo-name:tag  

docker push username/repo-name:tag

7. Run the Container

Pull and run the container in a new environment:

docker pull username/repo-name:tag  

docker run -dp 3000:3000 username/repo-name:tag

8. Access the Application

Open http://localhost:3000 in your browser to see your app running! 🎉

9. Explore More Commands

• To enter the container:

docker exec -it <container_id> sh

• To view logs:

docker logs <container_id>

✍️ Key Takeaways

Here’s what I learned today:

• A Dockerfile is the recipe for building Docker images.

• Writing a clean and efficient Dockerfile is key to smooth containerization.

• The docker run command bridges the gap between images and running containers.

• Managing images and containers with commands like docker exec and docker logs makes debugging easier.


Task 2/40

1. Dockerize Your Application:

• Either clone a GitHub project or use your own app.

• Write a Dockerfile and follow the steps above.

• Push your image to Docker Hub.

2. Explore docker init:

• This command simplifies Dockerfile creation. Try it and see how it works for your project.

3. Share Your Learnings:

• Write a blog or share a LinkedIn/Twitter update.

• Don’t forget to tag @PiyushSachdeva, @CloudOps Community, and use #40daysofkubernetes.


📂 GitHub Repository

GitHub repo You’ll find the Dockerfile and step-by-step instructions here.


📽️ Watch the Video

Here’s a quick walkthrough for visual learners! 👇

Let’s keep learning together—see you on Day 3! 🚀