Docker - Build (an image from a Dockerfile)

1 - About

Build an image from a Dockerfile.

3 - Command line

3.1 - Syntax


docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile


Options:
      --build-arg value         Set build-time variables (default [])
      --cgroup-parent string    Optional parent cgroup for the container
      --cpu-period int          Limit the CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int           Limit the CPU CFS (Completely Fair Scheduler) quota
  -c, --cpu-shares int          CPU shares (relative weight)
      --cpuset-cpus string      CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string      MEMs in which to allow execution (0-3, 0,1)
      --disable-content-trust   Skip image verification (default true)
  -f, --file string             Name of the Dockerfile (Default is 'PATH/Dockerfile')
      --force-rm                Always remove intermediate containers
      --help                    Print usage
      --isolation string        Container isolation technology
      --label value             Set metadata for an image (default [])
  -m, --memory string           Memory limit
      --memory-swap string      Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --no-cache                Do not use cache when building the image
      --pull                    Always attempt to pull a newer version of the image
  -q, --quiet                   Suppress the build output and print image ID on success
      --rm                      Remove intermediate containers after a successful build (default true)
      --shm-size string         Size of /dev/shm, default value is 64MB
  -t, --tag value               Name and optionally a tag in the 'name:tag' format (default [])
      --ulimit value            Ulimit options (default [])

3.2 - Example


docker build -t "name:latest" .

3.3 - Passing argument

4 - Code

4.1 - Context

When building an image, the directory of the Dockerfile gives the build context.

You can then to attach the build to a continuous building software

  • create one directory by version with a different Dockerfile in each o them
  • create a branch/or tag

All of the recursive contents of files and directories in the current directory are sent to the Docker daemon as the build context. You can add file in a .gitgnore file to exclude file from this context.

Example on Docker hub:

See build-context

Example with the -f flag to change the context to the dir newContextDir


docker build -t "gerardnico/imageName:tagName" -f newContextDir/Dockerfile .

4.2 - Cache

As each instruction is examined Docker looks for an existing image in its cache that it can reuse, rather than creating a new (duplicate) image. If you do not want to use the cache at all you can use the --no-cache=true option on the docker build command.

CACHE is:

  • docker instruction based. RUN apt-get update if not modified will not be performed twice
  • hash file based. COPY will copy only if the hash of the file are note the same

Example:

  • A COPY instruction will get updated only when the files in the argument are updated.
  • An instruction with arguments will not get updated because the string stay the same

5 - Documentation / Reference


Data Science
Data Analysis
Statistics
Data Science
Linear Algebra Mathematics
Trigonometry

Powered by ComboStrap