Multi-stage Docker builds

Byte Blog
3 min readApr 27, 2023

--

Docker is a popular platform for packaging and deploying applications. It provides a simple and efficient way to create portable and isolated containerized applications. In this tutorial, we will explain how to create a Dockerfile with separate build vs base layer.

Separating build and base layers is a best practice for building Docker images. By separating the build and base layers, you can reduce the size of the final image and speed up the build process.

Prerequisites:

  • Docker installed on your system

Step 1: Create a base image

The base image is the foundation of your Docker image. It contains the operating system and other essential software components needed to run your application.

Create a file named `Dockerfile.base` with the following content:


FROM python:3.8-slim-buster

RUN apt-get update && \
apt-get install -y curl && \
rm -rf /var/lib/apt/lists/*

This Dockerfile uses the official Python 3.8 slim-buster image as the base image. It also installs the curl package for testing purposes.

Build the base image using the following command:


docker build -t myapp-base -f Dockerfile.base .

This command creates a Docker image named `myapp-base` using the `Dockerfile.base` file.

Step 2: Create a build image

The build image contains the software components and dependencies needed to build your application. This image will be used to compile and package your application code.

Create a file named `Dockerfile.build` with the following content:


FROM myapp-base

WORKDIR /app
COPY requirements.txt /app
RUN pip install - no-cache-dir -r requirements.txt
COPY . /app
RUN python setup.py install

This Dockerfile uses the `myapp-base` image as the base image. It sets the working directory to `/app` and copies the `requirements.txt` file into the image. It then installs the Python dependencies specified in `requirements.txt` and copies the rest of the application code into the image. Finally, it runs the `setup.py` file to install the application.

Build the build image using the following command:


docker build -t myapp-build -f Dockerfile.build .

This command creates a Docker image named `myapp-build` using the `Dockerfile.build` file.

Step 3: Create a final image

The final image contains the compiled and packaged application code. It is created by copying the application code from the build image to a new image.

Create a file named `Dockerfile` with the following content:


WORKDIR /app
COPY - from=myapp-build /app /app
CMD ["python", "app.py"]

This Dockerfile uses the `myapp-base` image as the base image. It sets the working directory to `/app` and copies the compiled application code from the `myapp-build` image into the image. Finally, it specifies the command to run the application.

Build the final image using the following command:


docker build -t myapp -f Dockerfile .

This command creates a Docker image named `myapp` using the `Dockerfile` file.

Step 4: Run the container

Run the container using the following command:


docker run -p 5000:5000 myapp

This command runs the `myapp` Docker image and maps port 5000 on the host to port 5000 in the container.

Conclusion

In this tutorial, we have explained how to create a Dockerfile with separate build vs base layers. By following this best practice, you can reduce the size of your Docker image and speed

--

--

Byte Blog
Byte Blog

Written by Byte Blog

Technology enthusiast with a passion for transforming complex concepts into bite sized chunks

No responses yet