Intermediate container is not always created

Viewed 199

This question is for demonstrating docker and how it works, the following is the dockerfile content

FROM node:7
ADD node.js /node.js
ENTRYPOINT ["node", "node.js"]

and the node.js file is exist in the same directory, the question is why there is no intermediate container created for step#2?

~/demos/docker$docker build -t nodeapp .
Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM node:7
7: Pulling from library/node
ad74af05f5a2: Pull complete 
2b032b8bbe8b: Pull complete 
a9a5b35f6ead: Pull complete 
3245b5a1c52c: Pull complete 
afa075743392: Pull complete 
9fb9f21641cd: Pull complete 
3f40ad2666bc: Pull complete 
49c0ed396b49: Pull complete 
Digest: sha256:af5c2c6ac8bc3fa372ac031ef60c45a285eeba7bce9ee9ed66dad3a01e29ab8d
Status: Downloaded newer image for node:7
 ---> d9aed20b68a4
Step 2/3 : ADD node.js /node.js
 ---> 44c4486c7b32
Step 3/3 : ENTRYPOINT ["node", "node.js"]
 ---> Running in c9291d1209b0
Removing intermediate container c9291d1209b0
 ---> cfb9beed3592
Successfully built cfb9beed3592
Successfully tagged nodeapp:latest

EDIT

For example see the following example from this lnk specifically this where COPY caused to create intermediate container

Step 4 : COPY package.json /usr/src/app/  ---> 334d93a151ee Removing
intermediate container a678c817e467

And from this link where

ADD Execute-MyCmdlet.ps1 c:\example\

resulted

Step 4/5 : ADD Execute-MyCmdlet.ps1 c:\example\  ---> a955b2621c31
Removing intermediate container b825593d39fc

So for ADD and COPY the instructions caused intermediate container to be created then removed.

1 Answers

There is no need for an intermediate container to be spun up for step 2/3. Docker will add the file node.js to the image layer without starting a temporary container.

This build follows these steps:

  1. download the image, creating layer ID d9aed20b68a4
  2. add node.js to create image layer ID 44c4486c7b32
  3. start container c9291d1209b0 from image ID 44c4486c7b32 and set the entry point
  4. save the result of step (3) as the final image layer ID cfb9beed3592

You will see the same thing if you use a COPY in the dockerfile, instead of ADD. COPY is usually the preferred command to move files into the image, but ADD is ok too (just don't ADD a URL, as that pattern is discouraged).


Edit:
The link you included in your update is from 2016. Back then, Docker used a special 'helper image' that ran a custom binary inside of the container in order to execute COPY/ADD directives. That was changed fairly recently (April 2019) to allow the build to directly manipulate the file system. More information can be found in the release notes, but I'll add the relevant section here:

FileOp
LLB supports new operation FileOp allowing built-in file operations during build like copying files, creating new files or directories and removing files. Previously ADD/COPY commands used a helper image that ran a custom binary inside a container, now these commands use FileOp directly. This allows better performance and use of these commands in air-gapped environments without preloading the helper image, as well as fixing issues reported with the helper image implementation.

Related