Day 3 of #40DaysOfKubernetes
Table of contents
- 1. Set Up Docker Desktop.
- 2. Clone a Sample Repository.
- 3. Create Dockerfile and Write the Multistage Dockerfile.
- 4. Build the Dockerfile and Verify the Image Creation
- 5. Work with Basic Commands
- 6. Push to Registry
- 7. Pull and Run the Image
- 8. Manage the Docker Container
- 9. Conclusion
- 10. Connect with Me.
1. Set Up Docker Desktop.
The Multi-Stage Docker Build is used when we want to Reduce(Compress) the Image size or when we want to Optimize the performance of the Docker Container.
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 Repository.
Cloning the Repo: Use the command [ git clone https://github.com/piyushsachdeva/todoapp-docker.git ] to clone the repository to your local machine.
3. Create Dockerfile and Write the Multistage Dockerfile.
A Dockerfile is a script containing a series of instructions on how to build a Docker image for the application.
Multi-Stage Dockerfile code ->
FROM node:18-alpine AS installer
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM nginx:latest AS deployer
COPY --from=installer /app/build /usr/share/nginx/html
4. Build the Dockerfile and Verify the Image Creation
Building the Dockerfile converts the instructions into a Docker image. Verifying Ensure the image has been created and stored locally.
Build Command: Use the command docker build -t <image_name>
. to build the Docker image.
Tagging: Tag the image appropriately for versioning and identification.
Verifying: docker images
command will verify the image is created or not.
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 -dp 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 -dp 3000:3000 <username>/<repository>:<tag>
to run the image as a container.
8. Manage the Docker Container
To manage the Docker container, you might need to execute commands within the container or view logs. Here are some useful commands:
Enter the Container: Use docker exec -it container_name sh
or docker exec -it container_id sh
command.
View Docker Logs: Use docker logs container_name
or docker logs container_id
command.
Inspect Docker Container: Use docker inspect container_name
or docker inspect container_id
command.
For clearing the old docker image. Remove old Docker images from your local repository to free up space.
Cleanup Old Docker Images: Use docker image rm image-id
command.
9. Conclusion
Day 3 was all about learning and implementing Docker's multistage builds. We successfully cloned a sample repository, created a multistage Dockerfile, built and pushed the Docker image to Docker Hub, and finally ran the application in a Docker container. This approach is incredibly useful for optimizing image size and performance.
10. Connect with Me.
Linkedin [ https://www.linkedin.com/in/het-patel-0407bb1b7/ ] Twitter [ https://x.com/Het46022536 ]
Happy coding and always open to learning new things!
Subscribe to my newsletter
Read articles from HET PATEL directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by