Search code examples
dockerdocker-composedockerfiledocker-for-windows

Does Docker build a base image everytime a "child" container is run?


I am new to docker and the world of containers..Am using Microsoft servercore as my base image, and need an application installed on it, which I copy and install on the image, and copy some other files besides. I then build this image and use it as a base image for another container which works on these files.

However, it takes a long time for the second container to start doing its job, which left me wondering if Docker containers build their base images from scratch everytime they are run.

If this is the case, then is it possible for me to build a base image which comes installed with everything I need, so all the steps in the base image build (copying files, installing etc.) are not carried out everytime it is consumed by a "child" container?

If it is not the case, is there a reason why the child container should start that late? Some way to check which step is being executed by a container, perhaps?

UPDATE - I checked in a crude way, by removing one of the files which get copied into the base image, and by checking if it would create issues, but it didn't, so quite likely the base image container does not get built everytime it is consumed by a child container, but the time taken between Docker Run and the execution of a simple batch as the first step is quite high...

UPDATE 2 - For more clarity...

DockerFile 1 (builds as testdocker)

FROM mcr.microsoft.com/windows/servercore  
ADD 7z1900-x64.exe /7z1900-x64.exe  
RUN powershell.exe -Command Start-Process .\7z1900-x64.exe -ArgumentList '/S /D=c:/' -Wait

Dockerfile 2

FROM testdocker
COPY testbat.bat /testbat.bat
CMD testbat.bat && cmd

Batch file executed in second container

@ECHO ON
dir
pause

Solution

  • Base image is readonly for docker, so docker will not build base image everytime they run.

    But base image may has ONBUILD instruction, from offical document its behavior is:

    As part of processing the FROM instruction, the downstream builder looks for ONBUILD triggers, and executes them in the same order they were registered. If all triggers succeed, the FROM instruction completes and the build continues as usual.