• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
Docker | Build Options
  1. --no-cache
  2. --file
  3. --build-arg

  1. --no-cache
    You can use the option '--no-cache' to instruct Docker to not use cache when building the image.

    Docker help:
    --no-cache                      Do not use cache when building the image
    --no-cache-filter stringArray   Do not cache specified stages
    Let's use this Dockerfile:
    $ vi Dockerfile
    FROM ubuntu:latest
    
    RUN groupadd -r mtitek --gid=1001 && useradd -r -g mtitek --uid=1001 mtitek
    The first time we build the Dockerfile we will get the following output:
    $ docker build -t ubuntu-test-no-cache:latest .
    [+] Building 1.3s (7/7) FINISHED                                                                                                                                      docker:default
     => [internal] load build definition from Dockerfile                                                                                                                            0.0s
     => => transferring dockerfile: 133B                                                                                                                                            0.0s
     => [internal] load metadata for docker.io/library/ubuntu:latest                                                                                                                0.0s
     => [internal] load .dockerignore                                                                                                                                               0.0s
     => => transferring context: 2B                                                                                                                                                 0.0s
     => [1/2] FROM docker.io/library/ubuntu:latest@sha256:6015f66923d7afbc53558d7ccffd325d43b4e249f41a6e93eef074c9505d2233                                                          0.6s
     => => resolve docker.io/library/ubuntu:latest@sha256:6015f66923d7afbc53558d7ccffd325d43b4e249f41a6e93eef074c9505d2233                                                          0.5s
     => [auth] library/ubuntu:pull token for registry-1.docker.io                                                                                                                   0.0s
    
     => [2/2] RUN groupadd -r mtitek --gid=1001 && useradd -r -g mtitek --uid=1001 mtitek                                                                                           0.3s
    
     => exporting to image                                                                                                                                                          0.2s
     => => exporting layers                                                                                                                                                         0.1s
     => => exporting manifest sha256:d29add6517c260a07d37781e4e70158572711a701f5fa40331a65191c93251dc                                                                               0.0s
     => => exporting config sha256:45a4fd31c91fcf641818459d9f29164134d1b3cbc6e1af3a05bd88cb5e6270a4                                                                                 0.0s
     => => exporting attestation manifest sha256:f12ad540e9c0fa11fcaed029b79691aa4af6489b67f4e7553ba99ef58e95ca3f                                                                   0.0s
     => => exporting manifest list sha256:85e0ea95caa6802f8c74485c302e965f00f2c1a934ca6a359181c0aa60a8f281                                                                          0.0s
     => => naming to docker.io/library/ubuntu-test-no-cache:latest                                                                                                                  0.0s
     => => unpacking to docker.io/library/ubuntu-test-no-cache:latest
    If we build again the Dockerfile (with no changes) we will get the following output:
    $ docker build -t ubuntu-test-no-cache:latest .
    [+] Building 0.2s (6/6) FINISHED                                                                                                                                      docker:default
     => [internal] load build definition from Dockerfile                                                                                                                            0.0s
     => => transferring dockerfile: 133B                                                                                                                                            0.0s
     => [internal] load metadata for docker.io/library/ubuntu:latest                                                                                                                0.0s
     => [internal] load .dockerignore                                                                                                                                               0.0s
     => => transferring context: 2B                                                                                                                                                 0.0s
     => [1/2] FROM docker.io/library/ubuntu:latest@sha256:6015f66923d7afbc53558d7ccffd325d43b4e249f41a6e93eef074c9505d2233                                                          0.0s
     => => resolve docker.io/library/ubuntu:latest@sha256:6015f66923d7afbc53558d7ccffd325d43b4e249f41a6e93eef074c9505d2233                                                          0.0s
    
     => CACHED [2/2] RUN groupadd -r mtitek --gid=1001 && useradd -r -g mtitek --uid=1001 mtitek                                                                                    0.0s
    
     => exporting to image                                                                                                                                                          0.1s
     => => exporting layers                                                                                                                                                         0.0s
     => => exporting manifest sha256:d29add6517c260a07d37781e4e70158572711a701f5fa40331a65191c93251dc                                                                               0.0s
     => => exporting config sha256:45a4fd31c91fcf641818459d9f29164134d1b3cbc6e1af3a05bd88cb5e6270a4                                                                                 0.0s
     => => exporting attestation manifest sha256:9949898eb53d54d07c3d0371e9bfdcbd52bf65d28e24154ec4d642e3fe80befa                                                                   0.0s
     => => exporting manifest list sha256:97d14803e7398851c1dcef5ab7f563a7481d3f83abaf1a65308d38d4c4e1c179                                                                          0.0s
     => => naming to docker.io/library/ubuntu-test-no-cache:latest                                                                                                                  0.0s
     => => unpacking to docker.io/library/ubuntu-test-no-cache:latest
    You can notice that the step 2 has the mention '=> CACHED ...' which means that Docker just used the already built layer from this step.

    Using local cache can speed up build time; especially when avoiding rebuilding image layers that take long time to build or take up large amounts of disk space.

    In some cases (either to play safe or because we encountered some issues with the build) we might want to disable the cache, for that we use the option --no-cache:

    $ docker build --no-cache -t ubuntu-test-no-cache:latest .
    [+] Building 0.7s (6/6) FINISHED                                                                                                                                      docker:default
     => [internal] load build definition from Dockerfile                                                                                                                            0.0s
     => => transferring dockerfile: 133B                                                                                                                                            0.0s
     => [internal] load metadata for docker.io/library/ubuntu:latest                                                                                                                0.0s
     => [internal] load .dockerignore                                                                                                                                               0.0s
     => => transferring context: 2B                                                                                                                                                 0.0s
     => CACHED [1/2] FROM docker.io/library/ubuntu:latest@sha256:6015f66923d7afbc53558d7ccffd325d43b4e249f41a6e93eef074c9505d2233                                                   0.0s
     => => resolve docker.io/library/ubuntu:latest@sha256:6015f66923d7afbc53558d7ccffd325d43b4e249f41a6e93eef074c9505d2233                                                          0.0s
    
     => [2/2] RUN groupadd -r mtitek --gid=1001 && useradd -r -g mtitek --uid=1001 mtitek                                                                                           0.4s
    
     => exporting to image                                                                                                                                                          0.2s
     => => exporting layers                                                                                                                                                         0.1s
     => => exporting manifest sha256:19e6fa37eb98759c1537c4054008142026c2205a10c90945780dccbbd00c4678                                                                               0.0s
     => => exporting config sha256:745759414b5a46cb7295ca50fb37ce6c986285036bf464238354c743fe52f234                                                                                 0.0s
     => => exporting attestation manifest sha256:54b9a2afd02045d1c4d8c84aee04541666464bedf9ddc0f8c60c8423ab74c6ec                                                                   0.0s
     => => exporting manifest list sha256:81f25bc23c19f2ef72d48daa0490e210f66a3c5e062a7b71ff35e57083c91249                                                                          0.0s
     => => naming to docker.io/library/ubuntu-test-no-cache:latest                                                                                                                  0.0s
     => => unpacking to docker.io/library/ubuntu-test-no-cache:latest
    You can notice that the step 2 has no cache mention which means that Docker is rebuilding again the image layer for that step.
  2. --file
    You can use the option '-f ' (--file) to let Docker use a Dockerfile with a different name and/or a different path.

    By default Docker will use the Dockerfile in the current directory ('./Dockerfile').

    Docker help:
    -f, --file string                   Name of the Dockerfile (default: "PATH/Dockerfile")
    Syntax: docker build --file FILE_NAME|FILE_LOCATION

    Let's create a Dockerfile named 'Dockerfile1':
    $ vi Dockerfile1
    FROM ubuntu:latest
    To use the Dockerfile 'Dockerfile1' (located in the current directory):
    $ docker build -t ubuntu-custom-dockerfile1:latest --file Dockerfile1 .
    You can also provide the full path where Dockerfile1 is located (e.g. '/opt/dockerfiles/'):
    $ docker build -t ubuntu-custom-dockerfile1:latest --file /opt/dockerfiles/Dockerfile1 .
  3. --build-arg
    You can use the option '--build-arg' to set build arguments.

    Docker help:
    --build-arg stringArray         Set build-time variable
    Let's use this Dokerfile:
    $ vi Dockerfile
    ARG UBUNTU_VERSION=latest
    
    FROM ubuntu:$UBUNTU_VERSION
    
    ARG GROUP_ID=1001
    ARG USER_ID=1001
    
    RUN groupadd -r mtitek --gid=$GROUP_ID && useradd -r -g mtitek --uid=$USER_ID mtitek
    We defined three arguments: UBUNTU_VERSION, GROUP_ID, and USER_ID

    Note that the arguments:
    - are defined as following: ARG ARG_NAME=ARG_DEFAULT_VALUE.
    - are used as variables ($ARG_NAME) in the Dockerfile.
    - can also be used with the FROM instruction to define the base image.

    If you don't specify a value for an argument in the build command, then the default values in the Dockerfile will be used:
    $ docker build -t ubuntu-build-arg:latest .
    [+] Building 0.7s (6/6) FINISHED                                                                                                                                      docker:default
     => [internal] load build definition from Dockerfile                                                                                                                            0.0s
     => => transferring dockerfile: 208B                                                                                                                                            0.0s
     => [internal] load metadata for docker.io/library/ubuntu:latest                                                                                                                0.0s
     => [internal] load .dockerignore                                                                                                                                               0.0s
     => => transferring context: 2B                                                                                                                                                 0.0s
     => [1/2] FROM docker.io/library/ubuntu:latest@sha256:6015f66923d7afbc53558d7ccffd325d43b4e249f41a6e93eef074c9505d2233                                                          0.0s
     => => resolve docker.io/library/ubuntu:latest@sha256:6015f66923d7afbc53558d7ccffd325d43b4e249f41a6e93eef074c9505d2233                                                          0.0s
     => [2/2] RUN groupadd -r mtitek --gid=1001 && useradd -r -g mtitek --uid=1001 mtitek                                                                                           0.3s
     => exporting to image                                                                                                                                                          0.2s
     => => exporting layers                                                                                                                                                         0.1s
     => => exporting manifest sha256:dcbc23fdd1ae888418bedda08462acafecc16c28b465f43bfddb24f95b0b0341                                                                               0.0s
     => => exporting config sha256:0bc1d44b52b012cd2552301187f3bd87273e08a67705ce2cdde64c40169978dc                                                                                 0.0s
     => => exporting attestation manifest sha256:27b183d3ed31f046d51efc8b38ff75afe0b0ea3665aa92acdcf1e6e252aeb5b3                                                                   0.0s
     => => exporting manifest list sha256:a88fcd493df552d9d29985bda7d7c2374293d17185f140660f3f7d691081bf27                                                                          0.0s
     => => naming to docker.io/library/ubuntu-build-arg:latest                                                                                                                      0.0s
     => => unpacking to docker.io/library/ubuntu-build-arg:latest
    To pass values for the arguments, use the option '--build-arg':
    $ docker build -t ubuntu-build-arg:20.04 \
        --build-arg UBUNTU_VERSION=20.04 \
        --build-arg GROUP_ID=1002 \
        --build-arg USER_ID=1003 \
        .
    [+] Building 3.3s (6/6) FINISHED                                                                                                                                      docker:default
     => [internal] load build definition from Dockerfile                                                                                                                            0.0s
     => => transferring dockerfile: 208B                                                                                                                                            0.0s
     => [internal] load metadata for docker.io/library/ubuntu:20.04                                                                                                                 0.7s
     => [internal] load .dockerignore                                                                                                                                               0.0s
     => => transferring context: 2B                                                                                                                                                 0.0s
     => [1/2] FROM docker.io/library/ubuntu:20.04@sha256:8feb4d8ca5354def3d8fce243717141ce31e2c428701f6682bd2fafe15388214                                                           1.8s
     => => resolve docker.io/library/ubuntu:20.04@sha256:8feb4d8ca5354def3d8fce243717141ce31e2c428701f6682bd2fafe15388214                                                           0.0s
     => => sha256:13b7e930469f6d3575a320709035c6acf6f5485a76abcf03d1b92a64c09c2476 27.51MB / 27.51MB                                                                                1.1s
     => => extracting sha256:13b7e930469f6d3575a320709035c6acf6f5485a76abcf03d1b92a64c09c2476                                                                                       0.7s
     => [2/2] RUN groupadd -r mtitek --gid=1002 && useradd -r -g mtitek --uid=1003 mtitek                                                                                           0.5s
     => exporting to image                                                                                                                                                          0.2s
     => => exporting layers                                                                                                                                                         0.1s
     => => exporting manifest sha256:5ca3bfdc27c756df4bd8d127a44191d675479749f7d2005ef377a80d5b95f70e                                                                               0.0s
     => => exporting config sha256:9dcc3baa28a145138fcca9c21aa747b578738fda66dfd2c1bafb4cab02d27365                                                                                 0.0s
     => => exporting attestation manifest sha256:18fda102bd6e25c89d35cf20ebe951d7e0043fc50170f47e44b83cc7cd89221e                                                                   0.0s
     => => exporting manifest list sha256:13169fde11d8f8591db98f904243681eb189398f5cd029863a352673225a9603                                                                          0.0s
     => => naming to docker.io/library/ubuntu-build-arg:20.04                                                                                                                       0.0s
     => => unpacking to docker.io/library/ubuntu-build-arg:20.04
    Check that the arguments were used as expected:
    $ docker container run --rm -it ubuntu-build-arg:20.04 /bin/bash -c 'cat /etc/passwd | grep mtitek'
    mtitek:x:1003:1002::/home/mtitek:/bin/sh
© 2025  mtitek