Table of Contents

About

The build command 1) creates an image from:

  • and a context: A context is a list of files sent to the daemon

Example

Build an image

docker build -t "name:latest" .

where: with:

  • -t defines the image name name:latest
  • . defines the context as being the current directory
    • the Dockerfile is searched in this directory
    • All the files from the current directory are send to the daemon (without any .gitignore file restrictions)

How to minimize the context?

If the context is a whole code project, you can minimize the context data sent to the daemon with a docker ignore file .dockerignore that excludes all except the needed artifacts that you copy int

Example for a Java application:

# exclude all
**
# except
!build\libs\bytle-smtp-server-all.jar
!conf\**

Command line

Syntax

docker build [OPTIONS] PATH | URL | -

# example from stdin
docker build - < Dockerfile

where:

  • the context is defined by:
    • PATH. All the files in the local directory get tard and sent to the Docker daemon.
    • or URL for:

Passing argument

See Dockerfile - ARG instruction (argument)

Code

Context

The context can be:

  • one directory by version with a different Dockerfile in each o them
  • 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 .dockerignore file to exclude files from this context.

Example on Docker hub:

Docker Build Context

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

Documentation / Reference