How to Combine Multiple Base Images Using Single Dockerfile?
Last Updated :
23 Oct, 2020
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.
print("geeksforgeeks")
4. Building the Docker Image
To build the Docker Image, we use the Docker Build command.
sudo docker build -t sample-image .

Building the 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

Running the Container
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.
Similar Reads
Tips to Manage Docker Containers using CLI
Before virtualization, the management of web servers and web applications was tedious and much less effective. Thanks to virtualization, this task has been made much easier. This was followed by containerization which took it a notch higher. For network engineers, learning the basics of virtualizati
13 min read
How to Push a Container Image to a Docker Repository?
In this article we will look into how you can push a container image to a Docker Repo. We're going to use Docker Hub as a container registry, that we're going to push our Docker image to. Follow the below steps to push container Image to Docker repository: Step 1: The first thing you need to do is m
2 min read
Docker Compose - How To Execute Multiple Commands?
Docker Compose is an orchestration tool that comes with Docker and enables users to run and manage multi-container applications using a YAML format file. Docker Compose comes pre-installed with Docker and does not require any additional installation or activation. In this article, we will explore wh
7 min read
Best Practices for Writing a Dockerfile
If you are a Docker developer or you have been trying to get your hands dirty on Docker, you must have noticed the importance of creating an efficient dockerfile. A Dockerfile allows you to mention a sequence of instructions that are executed step by step and each execution creates an intermediate i
4 min read
How to Build a Web Server Docker File?
In this article, you can learn how to create your own docker customized image and get familiar with the docker file. Similarly, you can build a web server image that can be used to build containers. Here we will be exploring the process of using an Apache Web Server on Ubuntu to build our docker ima
3 min read
How to Install and Configure Docker in Ubuntu?
Docker is a platform and service-based product that uses OS-level virtualization to deliver software in packages known as containers. Containers are separated from one another and bundle their software, libraries, and configuration files. Docker is written in the Go language. Docker can be installed
6 min read
How to Install Linux Packages Inside a Docker Container?
Once you understand how to pull base Docker Images from the Docker registry, you can now simply pull OS distributions such as Ubuntu, CentOS, etc directly from the Docker hub. However, the OS Image that you have pulled simply contains a raw file system without any packages installed inside it. When
2 min read
How to Run a Python Script using Docker?
Docker helps you to run your Python application very smoothly in different environments without worrying about underlying platforms. Once you build an image using dockerfile you can run that image wherever you want to run. Docker image will help you to package all the dependencies required for the a
8 min read
Mounting a Volume Inside Docker Container
When you are working on a micro-service architecture using Docker containers, you create multiple Docker containers to create and test different components of your application. Now, some of those components might require sharing files and directories. If you copy the same files in all the containers
10 min read
How to Install Kali Docker Image to the Linux ?
In the cybersecurity sector, Kali is a prominent security distribution. Penetration testers, in particular, adore it. Kali has a number of security exploitation tools that may be used to test various systems, such as servers, networks, application servers, databases, VoIP, and so on. Kali is availab
3 min read