Search code examples
dockermultistage

Docker: Multistage builds result in multiple images


Given this small example of a multistage build

FROM node:10 AS ui-build
WORKDIR /usr/src/app

FROM node:10 AS server-build
WORKDIR /root/

EXPOSE 3070

ENTRYPOINT ["node"]
CMD ["index.js"]

why does this result in 3 images on my local file system?

"<none>";"<none>";"58d63982fbef";"2020-04-15 17:53:14";"912MB"
"node";"10";"bd83fcefc19d";"2020-04-14 01:32:21";"912MB"
"test";"latest";"3913dd4d03b6";"2020-04-15 17:53:15";"912MB"

I expected two images, the base image and the server-build image. I used the standard docker build command, i.e.

docker build -t test . 

so which of the parts of the image is none and which is test?

I am confused


Solution

  • Each block in the Dockerfile starting with a FROM line creates a new image. If you use a docker build -t option, only the last stage gets tagged with the name you specify; the remaining blocks will appear as <none> in places like docker images output.

    # node:10 is a base image
    
    # Not the final image, will appear as <none>:<none>
    FROM node:10 AS ui-build
    ...
    
    # The final image, will appear as test:latest (`docker build -t` option)
    FROM node:10 AS server-build
    ...
    

    You will occasionally see Dockerfiles where a base image is reused in later build stages, and there it will not show up at all in docker images output.

    # Will be hidden because it has descendant images
    FROM node:10 AS base
    RUN apt-get update && apt-get upgrade
    
    # Will appear as <none>:<none>
    FROM base AS ui
    ...
    
    # Will get the `docker build -t` tag
    FROM base