• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
Docker | Build Images
  1. Notes
  2. Build an image

  1. Notes
    See these pages for more information:
    https://docs.docker.com/reference/cli/docker/buildx/build/
    https://docs.docker.com/build/building/multi-stage/

    Docker introduced a new backend for building images called Buildkit, which does not expose intermediate build steps in the same way as the legacy builder. To view intermediate build steps and containers using the legacy builder, set the environment variable DOCKER_BUILDKIT=0 before using the docker build command:
    $ DOCKER_BUILDKIT=0 docker build [OPTIONS] PATH | URL | -
    This disables Docker BuildKit, which is now the default builder and offers improved performance, output, and caching. It's recommended to enable BuildKit (DOCKER_BUILDKIT=1) or simply omit the variable. However, for some examples on this page, we will disable BuildKit to show intermediate build steps and containers.
  2. Build an image
    The 'docker build' command has the following syntax:
    $ docker build [OPTIONS] PATH | URL | -
    Let's use this Dockerfile:
    $ vi Dockerfile
    FROM ubuntu:latest
    
    RUN apt-get -y update
    
    RUN apt-get -y install curl
    RUN apt-get -y install nginx
    
    RUN groupadd -r mtitek --gid=1001
    RUN useradd -r -g mtitek --uid=1001 mtitek
    To build an image from a Dockerfile:
    $ DOCKER_BUILDKIT=0 docker build -t ubuntu-nginx:latest .
    The option '-t IMAGE_NAME:IMAGE_TAG' instructs Docker to create an image with the specified image name (e.g., ubuntu-nginx) and image tag (e.g., latest).

    The character '.' at the end of the docker build command specifies the current directory. You can specify any path you want. It represents the path where Docker looks for all files and directories managed in the Dockerfile.

    By default, Docker will look for a Dockerfile with the name 'Dockerfile' in the current directory. You can use the '-f' option to specify an alternate location and a custom Dockerfile name.

    You can add a ".dockerignore" file where you can list files and directories that you want to be excluded when building the image. Therefore, these files and directories are not sent to the builder, which can help improve build speed (especially when using a remote Docker host).

    The build command produces the following output:
    Sending build context to Docker daemon  10.75kB
    Step 1/6 : FROM ubuntu:latest
     ---> 6015f66923d7
    Step 2/6 : RUN apt-get -y update
     ---> Running in 780f40abb5cf
    ...
     ---> Removed intermediate container 780f40abb5cf
     ---> b34542b1b9fb
    Step 3/6 : RUN apt-get -y install curl
     ---> Running in be38cb7e6c21
    ...
     ---> Removed intermediate container be38cb7e6c21
     ---> 9e0f805a3972
    Step 4/6 : RUN apt-get -y install nginx
     ---> Running in 9317535ed169
    ...
     ---> Removed intermediate container 9317535ed169
     ---> 01756a2038d9
    Step 5/6 : RUN groupadd -r mtitek --gid=1001
     ---> Running in fb8aa37f4f7f
     ---> Removed intermediate container fb8aa37f4f7f
     ---> 41ca8f17b674
    Step 6/6 : RUN useradd -r -g mtitek --uid=1001 mtitek
     ---> Running in 0769c8d01f3e
    useradd warning: mtitek's uid 1001 is greater than SYS_UID_MAX 999
     ---> Removed intermediate container 0769c8d01f3e
     ---> 7b666fb64cbe
    Successfully built 7b666fb64cbe
    Successfully tagged ubuntu-nginx:latest
    In this case it's the first time the image is built. All the steps are executed and no cache was used by Docker.

    To list the images:
    $ docker image ls ubuntu*
    REPOSITORY     TAG       IMAGE ID       CREATED         SIZE
    ubuntu-nginx   latest    7b666fb64cbe   2 minutes ago   226MB
    ubuntu         latest    6015f66923d7   5 weeks ago     117M
    We can see the image we created "ubuntu-nginx" and the parent image "ubuntu".

    Note that the following commands are equivalent: docker image ls, docker image list, docker images
© 2025  mtitek