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 Image5. 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
Docker - Using Public Repositories To Host Docker Images Docker is a software platform for creating isolated virtualized environments for building, deploying, and testing applications with ease. In this tutorial, we will learn how to host public repositories on docker hub which is a hosted repository service provided by Docker for finding and sharing cont
10 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 Repository. 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: Create a Docker Account The f
3 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 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
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
Using CLI to Manage Docker Volumes If you want to share files and directories among multiple Docker Containers, you can easily mount Docker Volumes to different Containers. However, managing such a large number of Docker Volumes becomes very difficult at times. In this article, we are going to discuss how to manage Docker Volumes by
7 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
Docker - Search Image in Dockerhub through CLI There are tons of pre-built Docker Images that you can find on Docker's official repository called DockerHub. In fact, you can also push your own customized Image on DockerHub. These Images can then be pulled using the Docker pull command and can be used as Base Images. In this article, we will disc
2 min read
Docker - Using Image Tags Image tags are used to describe an image using simple labels and aliases. Tags can be the version of the project, features of the Image, or simply your name, pretty much anything that can describe the Image. It helps you manage the project's version and lets you keep track of the overall development
7 min read