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:
- Smaller Image Size: By including only the necessary components in the final image, you can significantly reduce the size of the Docker image.
- Enhanced Security: Reducing the number of layers and components in the final image minimizes the attack surface, making it more secure.
- 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
- Visit the Docker website: Docker Desktop
- Download and install the Docker Desktop client on your machine.
Step 2: Set Up Your Application
- Clone the sample repository (or use your existing web application):
git clone https://github.com/piyushsachdeva/todoapp-docker.git
- Change directory into the project folder:
cd todoapp-docker/
- Create a new Dockerfile in the project directory:
touch Dockerfile
Step 3: Write the Dockerfile
- Open the Dockerfile in your favorite text editor.
- 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
- Build the Docker image using the code and Dockerfile:
docker build -t todoapp-docker .
- Verify the image was successfully created:
docker images
Step 5: Push the Image to Docker Hub
- Create a new repository on hub.docker.com.
- Log in to Docker Hub from your terminal:
docker login
- Tag your Docker image for the remote repository:
docker tag todoapp-docker:latest username/reponame:tagname
- Push the image to the remote repository:
docker push username/reponame:tagname
Step 6: Deploy and Run the Container
- To pull the image in a different environment:
docker pull username/reponame:tagname
- Start the Docker container:
docker run -dp 3000:3000 username/reponame:tagname
- Verify that your application is running by accessing
localhost:3000
in your browser.
Step 7: Manage and Inspect the Container
- To enter the running container:
docker exec -it containername sh
or
docker exec -it containerid sh
- To view logs from the container:
docker logs containername
or
docker logs containerid
- To inspect the container’s details:
docker inspect containername
Step 8: Clean Up
- Remove old or unused Docker images:
docker image rm image-id
This version provides the same instructions with some variations in wording and structure.