40 Days Of Kubernetes (3/40)

Day 3/40

Multi Stage Docker Builds

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, …


This content originally appeared on DEV Community and was authored by Subham Nandi

Day 3/40

Multi Stage Docker Builds

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
  1. Change directory into the project folder:
   cd todoapp-docker/
  1. Create a new Dockerfile in the project directory:
   touch Dockerfile

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

Step 4: Build the Docker Image

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

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
  1. Tag your Docker image for the remote repository:
   docker tag todoapp-docker:latest username/reponame:tagname
  1. Push the image to the remote repository:
   docker push username/reponame:tagname

Step 6: Deploy and Run the Container

  1. To pull the image in a different environment:
   docker pull username/reponame:tagname
  1. Start the Docker container:
   docker run -dp 3000:3000 username/reponame:tagname
  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

or

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

or

   docker logs containerid
  1. To inspect the container’s details:
   docker inspect containername

Step 8: Clean Up

  1. Remove old or unused Docker images:
   docker image rm image-id

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


This content originally appeared on DEV Community and was authored by Subham Nandi


Print Share Comment Cite Upload Translate Updates
APA

Subham Nandi | Sciencx (2024-08-19T15:07:16+00:00) 40 Days Of Kubernetes (3/40). Retrieved from https://www.scien.cx/2024/08/19/40-days-of-kubernetes-3-40/

MLA
" » 40 Days Of Kubernetes (3/40)." Subham Nandi | Sciencx - Monday August 19, 2024, https://www.scien.cx/2024/08/19/40-days-of-kubernetes-3-40/
HARVARD
Subham Nandi | Sciencx Monday August 19, 2024 » 40 Days Of Kubernetes (3/40)., viewed ,<https://www.scien.cx/2024/08/19/40-days-of-kubernetes-3-40/>
VANCOUVER
Subham Nandi | Sciencx - » 40 Days Of Kubernetes (3/40). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/19/40-days-of-kubernetes-3-40/
CHICAGO
" » 40 Days Of Kubernetes (3/40)." Subham Nandi | Sciencx - Accessed . https://www.scien.cx/2024/08/19/40-days-of-kubernetes-3-40/
IEEE
" » 40 Days Of Kubernetes (3/40)." Subham Nandi | Sciencx [Online]. Available: https://www.scien.cx/2024/08/19/40-days-of-kubernetes-3-40/. [Accessed: ]
rf:citation
» 40 Days Of Kubernetes (3/40) | Subham Nandi | Sciencx | https://www.scien.cx/2024/08/19/40-days-of-kubernetes-3-40/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.