Day 2 of #40DaysOfKubernetes

How to Dockerize a Project

·

3 min read

1. Set Up Docker Desktop.

Docker Desktop is an essential tool for developing containerized applications. It's available for both Windows and macOS, providing an easy-to-use interface for managing Docker containers and images.

Installation: Download Docker Desktop from the official Docker website and follow the installation instructions.

Configuration: Once installed, launch Docker Desktop and configure it to your preferences. Ensure that Docker Engine is running and set up your Docker Hub account for managing images.

2. Clone a Sample App.

Cloning the Repo: Use the command [ git clone github.com/docker/getting-started-app.git ] to clone the repository to your local machine.

3. Create Dockerfile.

A Dockerfile is a script containing a series of instructions on how to build a Docker image for the application.

Base Image: Start by specifying a base image using the FROM instruction, e.g., FROM node:18-alpine for a Node.js Alpine app.

Working Directory: Set the working directory using the WORKDIR /app instruction.

Copy Files: Use the COPY . . instruction to copy application files into the container.

Install Dependencies: Run commands to install dependencies using the RUN npm install --production instruction.

Start the Application: Specify the command to start the application using the CMD ["node", "src/index.js"] instruction.

Expose Ports: Use the EXPOSE 3000 instruction to expose the necessary ports.

4. Build the Dockerfile

Building the Dockerfile converts the instructions into a Docker image.

Build Command: Use the command docker build -t <image_name> . to build the Docker image.

Tagging: Tag the image appropriately for versioning and identification.

5. Work with Basic Commands

Getting familiar with basic Docker commands is crucial for managing images and containers.

List Images: docker images to list all Docker images.

Run Container: docker run -d -p 3000:3000 <image_name> to run a container in detached mode.

List Containers: docker ps to list running containers.

Stop Container: docker stop <container_id> to stop a running container.

Remove Container: docker rm <container_id> to remove a stopped container.

Remove Image: docker rmi <image_name> to remove an image.

6. Push to Registry

After building the Docker image, it needs to be pushed to a Docker registry like Docker Hub for easy distribution.

Login: Use docker login to log into Docker Hub.

Tag Image: Tag the image for Docker Hub using docker tag /:.

Push Image: Use docker push /: to push the image to Docker Hub.

7. Pull and Run the Image

Once the image is in the Docker registry, it can be pulled and run on any system with Docker installed.

Pull Image: Use docker pull <username>/<repository>:<tag> to pull the image from Docker Hub.

Run Image: Use docker run -d -p 3000:3000 <username>/<repository>:<tag> to run the image as a container.

9. Conclusion

Day 2 was packed with practical steps to Dockerize a project, from setting up Docker Desktop to pushing an image to Docker Hub. This experience highlights the power and convenience of Docker for developing, distributing, and running applications consistently across different environments.

10. Connect with Me.

Linkedin [ linkedin.com/in/het-patel-0407bb1b7 ] Twitter [ x.com/Het46022536 ]

Happy coding and always open to learning new things!