How to Combine Multiple Base Images Using Single Dockerfile?
If you are working on a large micro-service project using Docker Containers, the development cycle consists of some phases. Now, maintaining different dockerfiles for different phases of the development uses up lots of resources, leads to redundancy as several project components might contain common files. It really becomes unnecessary to use separate dockerfiles for the build phase, development phase, release phase, and, testing phase.
In later versions of Docker, it provides the use of multi-stage dockerfiles. Using multi-stage dockerfiles, you can use several base images as well as previous intermediate image layers to build a new image layer. Basically, it allows you to create a complete hierarchy of Docker instructions that could be used to create different sets of images with different functionalities but all in a single dockerfile. Use of two commands – FROM and AS, in particular, allows you to create a multi-stage dockerfile. It allows you to create multiple image layers on top of the previous layers and the AS command provides a virtual name to the intermediate image layer. The last FROM command in the dockerfile creates the actual final image.
In this article, we will see an example of a multi-stage dockerfile and how you can use it in your Docker projects.
1. Creating a multi-stage dockerfile
To begin with, let’s consider the dockerfile below.
#Create the base OS image FROM python:3 AS base #Update the OS ubuntu image RUN apt-get -y update #Install packages RUN apt-get -y install firefox \ && apt-get -y install vim #Create another image layer on top of base to install requirements FROM base AS requirements #Install the requirements RUN pip3 install -r requirements.txt #Create an intermediate image layer for testing purpose FROM requirements as test #Create the build context COPY /usr/src/my-app /desktop/my-app #Test the final app CMD ["python3", "index.py"]
Let’s go through the above dockerfile step by step.
- First, we have pulled the python 3 base images directly from the Docker registry. It also sets the base image’s OS to be Ubuntu by default. We have used a virtual name called “base” for this image layer.
- Then, we run an apt update on the Ubuntu OS.
- After that, we install some basic packages such as Firefox browser and vim text editor.
- Using the base image, we create another image layer on top of it called “requirements” which installs the dependencies from a separate file called “requirements.txt”.
- Using this image as the base image, we have created another intermediate image layer called “test” which creates the build context and copies the files and directories, and finally runs the python application for testing.
2. Creating the requirements file
In the requirements file, we mention the dependencies that we want to install.
flask pandas numpy
3. Creating the index.py file
The main file that we want to run and have specified in the dockerfile’s CMD arguments is the index.py file. In the index.py file, we simply include a print statement for demonstration purposes.
4. Building the Docker Image
To build the Docker Image, we use the Docker Build command.
sudo docker build -t sample-image .
5. Running the Docker Container
After we have successfully built the Docker Image, we can run the container using the Docker run command.
sudo docker run -it sample-image
We can clearly see how the combination of FROM and AS commands can help us create a unique hierarchy for all our projects or project components. Basically, it allows us to perform inheritance of image components including hierarchical or multiple inheritances based on how you combine those commands.
This proves to be very helpful because it allows you to perform all the tasks using a single dockerfile thus making version management easier, it gives a better overview of the whole project, it reduces the overall size of the final image by eliminating the need to use the same files in the different image since there is only one final image now.
To conclude, in this article we discussed how to use Docker multi-stage builds to use and inherit multiple bases and customize image layers in a single dockerfile.