DEV Community

Subham Nandi
Subham Nandi

Posted on • Updated on

40 Days Of Kubernetes (3/40)

Day 3/40

Multi Stage Docker Builds

GitHub link - https://github.com/SUBHAM-NANDI/40DaysKubernetes/tree/main/Day%203

Docker multistage builds are a technique in Docker that allows you to use multiple FROM statements in a single Dockerfile to create separate build stages. Each stage can have its own base image, and you can selectively copy artifacts from one stage to another, which helps in creating a smaller and more optimized final image.

Key Benefits:

  1. Smaller Image Size: By including only the necessary components in the final image, you can significantly reduce the size of the Docker image.
  2. Enhanced Security: Reducing the number of layers and components in the final image minimizes the attack surface, making it more secure.
  3. Efficient Builds: You can reuse intermediate stages for different parts of your application, optimizing the build process.

Use Cases:

  • CI/CD Pipelines: Multistage builds are particularly useful in CI/CD pipelines where you want to build, test, and deploy applications efficiently.
  • Microservices: Ideal for microservices where you need lightweight and secure containers.

Step 1: Install Docker Desktop

  1. Visit the Docker website: Docker Desktop
  2. Download and install the Docker Desktop client on your machine.

Step 2: Set Up Your Application

  1. Clone the sample repository (or use your existing web application):
   git clone https://github.com/piyushsachdeva/todoapp-docker.git
Enter fullscreen mode Exit fullscreen mode
  1. Change directory into the project folder:
   cd todoapp-docker/
Enter fullscreen mode Exit fullscreen mode
  1. Create a new Dockerfile in the project directory:
   touch Dockerfile
Enter fullscreen mode Exit fullscreen mode

Step 3: Write the Dockerfile

  1. Open the Dockerfile in your favorite text editor.
  2. Paste the following content into the Dockerfile:
   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
Enter fullscreen mode Exit fullscreen mode

Step 4: Build the Docker Image

  1. Build the Docker image using the code and Dockerfile:
   docker build -t todoapp-docker .
Enter fullscreen mode Exit fullscreen mode
  1. Verify the image was successfully created:
   docker images
Enter fullscreen mode Exit fullscreen mode

Step 5: Push the Image to Docker Hub

  1. Create a new repository on hub.docker.com.
  2. Log in to Docker Hub from your terminal:
   docker login
Enter fullscreen mode Exit fullscreen mode
  1. Tag your Docker image for the remote repository:
   docker tag todoapp-docker:latest username/reponame:tagname
Enter fullscreen mode Exit fullscreen mode
  1. Push the image to the remote repository:
   docker push username/reponame:tagname
Enter fullscreen mode Exit fullscreen mode

Step 6: Deploy and Run the Container

  1. To pull the image in a different environment:
   docker pull username/reponame:tagname
Enter fullscreen mode Exit fullscreen mode
  1. Start the Docker container:
   docker run -dp 3000:3000 username/reponame:tagname
Enter fullscreen mode Exit fullscreen mode
  1. Verify that your application is running by accessing localhost:3000 in your browser.

Step 7: Manage and Inspect the Container

  1. To enter the running container:
   docker exec -it containername sh
Enter fullscreen mode Exit fullscreen mode

or

   docker exec -it containerid sh
Enter fullscreen mode Exit fullscreen mode
  1. To view logs from the container:
   docker logs containername
Enter fullscreen mode Exit fullscreen mode

or

   docker logs containerid
Enter fullscreen mode Exit fullscreen mode
  1. To inspect the container’s details:
   docker inspect containername
Enter fullscreen mode Exit fullscreen mode

Step 8: Clean Up

  1. Remove old or unused Docker images:
   docker image rm image-id
Enter fullscreen mode Exit fullscreen mode

This version provides the same instructions with some variations in wording and structure.

Top comments (0)