CodeBlanks Logo Icon CodeBlanks

Build context

Learn what build context is and how it is used.

The build context

When docker build starts, it sends the whole context folder to the daemon. Files outside the context (like COPY ../gizli.txt .) can't be copied. A big context (e.g. node_modules, data files) slows the build:

$ docker build -t web .
[+] Building 12.4s
 => [internal] load build context
 => => transferring context: 845.21MB     # too big!

Choosing the context

The context and the Dockerfile location can be given separately. In a monorepo, making the root folder the context and using the Dockerfile in a subfolder is common:

# proje/
#   paylasilan/
#   servisler/api/Dockerfile
cd proje
docker build -f servisler/api/Dockerfile -t api .   # context: proje/

The context can also be a Git address: docker build https://github.com/ali/api.git#main.

Practice A monorepo build

You are inside the proje folder. Write the build command using servisler/api/Dockerfile, with the current folder as the context and the image name api:dev.

Starter code

terminal
# buraya yaz

Expected output

[+] Building 6.3s (10/10) FINISHED
 => naming to docker.io/library/api:dev done

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Why does COPY ../config.yml . fail?

  • COPY doesn't take single files
  • The file is outside the build context Correct answer
  • YAML isn't supported
  • The dot is wrong

Explanation: The daemon only sees the context sent to it.

2 Make the parent folder the context.

Question
docker build -f Dockerfile -t api ____
Answer
docker build -f Dockerfile -t api ..

Explanation: .. is the parent folder.

3 Match.

Term Meaning
context Folder sent to the daemon
-f Dockerfile location
transferring context Size sent
Git adresi Remote context

All lessons in Beginner