Intermediate containers in Docker !!!

Play this article

Docker is the containerization platform and now people use docker and container interchangeably. If you don't know docker and want to learn more then please go through the below article which will you understand.

Docker beginner tutorial.

Now let us first understand.

what are Containers :

Imagine you're packing for a trip. You have a suitcase that can hold all of your clothes, shoes, and toiletries. Each item is organized neatly inside the suitcase, and it's easy to find what you need.

Similarly, containers are like suitcases for computer programs. They allow developers to bundle up all the different parts of an application, like the code, libraries, and dependencies, into a single package. This package can then be easily moved around, just like a suitcase, and run on different computers or servers.

Docker is a popular tool for creating and managing containers. It provides a simple and efficient way for developers to package up their applications and run them in a consistent environment, no matter where they are.

So, just like how a suitcase helps you keep your belongings organized and portable, containers like Docker help developers keep their applications organized and easy to move around.

What are Intermediate containers?

Have you seen the logs while building the docker image, it's interesting to always see logs, either it creates more question or resolve your doubts. If you have seen any docker build logs then those are like the below, let us take an example of a java application:

Step 1/5 : FROM openjdk:11-jdk-slim
11-jdk-slim: Pulling from library/openjdk

Step 2/5 : WORKDIR /app


Step 3/5 : COPY target/my-app.jar my-app.jar
Removing intermediate container 123asdj4b22

Step 4/5 : ENTRYPOINT ["java","-jar","my-app.jar"]

Successfully built 1a2b3c4d5e6f
Successfully tagged my-app-image:latest

As docker images are layered, When you build a new image, Docker does this for each instruction, it follows three steps:

Or sometimes you have seen logs like removing the intermediate container, if not then go and watch jenkins build logs.

Removing intermediate container 12312asdaw3
 ---> asdq245310as01
  1. create a temporary container from the previous image layer image for the first command;

  2. run the Dockerfile instruction in the temporary "intermediate" container;

  3. save the temporary container as a new image layer.

When we build a Docker image, we use a Dockerfile that contains a series of steps. Each step in the Dockerfile creates a new layer in the final image. These layers can be reused by other images, making the image-building process faster and more efficient.

Intermediate containers are created when Docker builds each layer of the image. These containers are temporary and are used to execute the commands in each step of the Dockerfile. Once a layer is complete, Docker saves it as a new image and discards the intermediate container.

So, let's say we have a Dockerfile with three steps: Step 1, Step 2, and Step 3. When we build the Docker image, Docker creates an intermediate container for each step. The Step 1 intermediate container is used to execute the commands in Step 1 of the Dockerfile, and once that step is complete, the Step 1 intermediate container is discarded. Then, Docker creates a new intermediate container for Step 2, and so on, until the final image is complete.

To summarize, intermediate containers are like temporary workspaces that Docker uses to build each layer of the Docker image. They're like little helpers that do some work and then disappear once their job is done.

Coding time:

Assuming you have a Spring Boot Java application codebase and a Dockerfile in a directory called my-app, here's an example of what the Dockerfile might look like:

# Use an official OpenJDK runtime as a parent image
FROM openjdk:11-jre-slim

# Set the working directory to /app

# Copy the current directory contents into the container at /app
COPY target/myapp-0.0.1-SNAPSHOT.jar /app

# Make port 8080 available to the world outside this container

# Run the JAR file
CMD ["java", "-jar", "myapp-0.0.1-SNAPSHOT.jar"]

Logs are like this:

Sending build context to Docker daemon  18.94MB
Step 1/5 : FROM openjdk:11-jre-slim
11-jre-slim: Pulling from library/openjdk

Step 2/5 : WORKDIR /app

Step 3/5 : COPY target/myapp-0.0.1-SNAPSHOT.jar /app

Step 4/5 : EXPOSE 8080

Step 5/5 : CMD ["java", "-jar", "myapp-0.0.1-SNAPSHOT.jar"]

Successfully built 1a2b3c4d5e6f
Successfully tagged myapp:latest

During each step, Docker creates a new intermediate container to execute the commands in that step. Once the commands are executed, Docker saves the intermediate container as a new layer in the final image and then discards the intermediate container.

In this example, the first intermediate container is created for Step 1, the second intermediate container is created for Step 2, and so on, until the final image is complete.

I hope this helps, you!!

More such articles:


If this article adds any value to you then please clap and comment.

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!