docker multistage, to use service from another image

taragrg6 profile image taragurung ・2 min read

Though, docker multistage sounds a complicate things to grasp in docker world. It isn't, and is also not compulsory things to implement. Things can be done, even without it. Today I will try to showcase how you can use service running in one docker image to another docker image. In a nutshell use multiple image in single Dockerfile or use service from already build image.

For demonstration purpose I would perform the following tasks:

  • Create one docker image with ubuntu as base image and install docker on it.
  • Then create another image and use the docker installed in first image to it.

STEP-1 Lets create our first docker image:
Please save it as Dockerfile and build it. You can also look into my short video demonstration if you would like to.

From ubuntu
RUN apt-get update && apt-get install -y docker.io

Lets' build it

docker build -t firstimage .

This will create a docker image with ubuntu as base image and docker installed . Now, lets jump into steps 2.

STEP-2 : Use docker service from Image already build:

I will keep it simple this time too. Suppose, we are in need of a docker image which need ubuntu as base image and the docker installed. Here, we have an option like we can simply copy what we did in step 1 and build our image or opt for multi-stage build, where we will utilize the service that is already available in another built image.

1. from firstimage as dockerimage

#this is the new image which will utilize docker without installing
#because comes from another already build image above

2. from ubuntu
3. RUN apt-get update && apt-get install -y libltdl7
4. COPY --from=dockerimage /usr/bin/docker /usr/bin/docker

As you can clearly see. We have two from command here line number (1,2). The first one gets the image we recently built and is named as dockerimage. The second one is the new image where we are running other commands. In line number (4) we are copying the docker from first image to the new image we are going to build. Step (3) is not compulsory. I had a problem in my container so had to do it.

That's it this is how we copy the service available in one image to another. Let's build the second image.

docker build -t secondimage .

Let's make sure that the second image we have can successfully utilize the docker command. As we just copied rather than installing on it.

To do that there are multiple options like. Adding RUN command with docker commands like docker --version in the Dockerfile, or getting into the running docker container with following command and running the docker commands.

docker run -it secondimage bash

It will lead you inside the running container and simply type docker to know if it successfully fetched the docker that we copied from firstimage

Link to short video demonstration. If you believe more in video


I know, my writing is not coherent so please help me to pin point my areas of mistake, which will me improve and motivate to write better articles every time.


Editor guide