CONTEXT

Either a path to a directory containing a Dockerfile, or a url to a git repository.

When the value supplied is a relative path, it is interpreted as relative to the location of the Compose file. This directory is also the build context that is sent to the Docker daemon.

Compose builds and tags it with a generated name, and uses that image thereafter.

build:
  context: ./dir

That mean, its a path to the directory that contains Dockerfile file. It purposes to build a new image for the service.

Reference: https://docs.docker.com/compose/compose-file/compose-file-v3/

In your case:

   context: .

Dockerfile file should be existing in the current directory (in the same folder of the docker-compose.yml file).

Answer from Thanh Nguyen Van on Stack Overflow
🌐
Docker Docs
docs.docker.com › reference › compose file reference › compose build specification
Compose Build Specification | Docker Docs
When build is specified as a string, the whole path is used as a Docker context to execute a Docker build, looking for a canonical Dockerfile at the root of the directory. When build is specified as a detailed structure, build arguments can ...
Top answer
1 of 3
84

CONTEXT

Either a path to a directory containing a Dockerfile, or a url to a git repository.

When the value supplied is a relative path, it is interpreted as relative to the location of the Compose file. This directory is also the build context that is sent to the Docker daemon.

Compose builds and tags it with a generated name, and uses that image thereafter.

build:
  context: ./dir

That mean, its a path to the directory that contains Dockerfile file. It purposes to build a new image for the service.

Reference: https://docs.docker.com/compose/compose-file/compose-file-v3/

In your case:

   context: .

Dockerfile file should be existing in the current directory (in the same folder of the docker-compose.yml file).

2 of 3
21

From the official documentation we know that:

context defines either a path to a directory containing a Dockerfile, or a URL to a git repository.

In your case, . is a relative path representing the current directory where you run docker-compose command and where Compose can find a Dockerfile (and obviously also the docker-compose.yaml file).

The Dockerfile can also be elsewhere using the dockerfile keyword like that in this example:

version: "3.3"
services:
  
  service-A
    build:
      context: ./all-service
      dockerfile: ./service-A/Dockerfile

  service-B
    build:
      context: ./all-service
      dockerfile: ./service-B/Dockerfile

  service-C
    build:
      context: ./all-service
      dockerfile: ./service-C/Dockerfile 

The additional context keyword here is to tell the Dockerfile where to find its dependent files.

For instance, in one of your Dockerfile there are these lines:

WORKDIR /my-app
COPY package.json package.json
COPY package-lock.json package-lock.json
RUN npm install
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

These three files must be in the context directory or git repository: package.json, package-lock.json, requirements.txt.

Discussions

Adding a build context to support docker compose watch changes the behavior of other commands
Description I love the new docker compose command, I want to use it everywhere. I found this minor annoyance and figured it was worth some time to document it. TL;DR; In order to use docker compose watch we have to give a service a build... More on github.com
🌐 github.com
1
November 16, 2023
Support multiple build contexts
Dockerfile 1.4 supports multiple build contexts - a way to specify them when building in compose would be great I'd suggest something like: services: myapp: build: contexts: additional: ../foo ... More on github.com
🌐 github.com
10
May 13, 2022
Docker-compose with --context
I can’t really find any information on how docker-compose work with --context All I’ve found is this blog post If I have a docker-compose.yml on my server I can run docker-compose up -d etc. and it works just as it should. If I take this exact docker-compose.yml file and put it on my local ... More on forums.docker.com
🌐 forums.docker.com
3
0
November 10, 2020
Build context is always the location of docker-compose.yaml
Adding docker config to an old wordpress project created with wordpress in the top level dir, we have a deploy/ dir with scripts, etc. So I want to put docker-compose.yaml in there. project/ |-- de... More on github.com
🌐 github.com
16
May 23, 2017
🌐
Docker
docs.docker.com › manuals › docker build › build context
Build context | Docker Docs
You can specify the address of a remote Git repository, tarball, or plain-text file as your build context. For Git repositories, the builder automatically clones the repository.
🌐
Baeldung
baeldung.com › home › docker › guide to docker compose context
Guide to Docker Compose Context | Baeldung on Ops
November 12, 2024 - In this example, if context: . is defined in docker-compose.yml, then Docker packages everything in the my_django_project directory. It packages unnecessary files like the .git directory. However, if we only need the files in the app/ directory, we can modify the context path in the context field: version: "3" services: app: build: context: ./app ports: - "8000:8000" volumes: - ./app:/app command: > sh -c "python manage.py runserver 0.0.0.0:8000"
🌐
GitHub
github.com › docker › compose › issues › 11198
Adding a build context to support docker compose watch changes the behavior of other commands · Issue #11198 · docker/compose
November 16, 2023 - In order to use docker compose watch we have to give a service a build context but for cases where one image is used to run multiple services this can change the behavior of other non watch commands.
Author   antiBaconMachine
🌐
GitHub
github.com › docker › compose › issues › 9461
Support multiple build contexts · Issue #9461 · docker/compose
May 13, 2022 - Dockerfile 1.4 supports multiple build contexts - a way to specify them when building in compose would be great I'd suggest something like: services: myapp: build: contexts: additional: ../foo deps: https://github.com/myvendor/deps dist:...
Published   May 13, 2022
Author   ciaranmcnulty
🌐
Medium
medium.com › @manandharsabbir › docker-compose-different-build-context-path-and-dockerfile-file-path-8795a992821a
Docker-Compose: Different Build Context Path and Dockerfile File Path | by Sabbir Manandhar | Medium
August 9, 2019 - Follow is definition of my docker-compose entry: helloworld: build: context: /workspace/docker-helloworld dockerfile: /workspace/docker-helloworld/docker/Dockerfile image: hello-world
Find elsewhere
🌐
Docker
docs.docker.com › manuals › docker build › bake › using bake with additional contexts
Using Bake with additional contexts | Docker Docs
target "app" { contexts = { src = "../path/to/source" } } To use a result of one target as a build context of another, specify the target name with target: prefix.
🌐
Docker
docs.docker.com › reference › docker compose › docker compose build
docker compose build | Docker Docs
If the Compose file specifies an image name, the image is tagged with that name, substituting any variables beforehand. See variable interpolation. If you change a service's Dockerfile or the contents of its build directory, run docker compose build to rebuild it.
🌐
Docker Community
forums.docker.com › docker engine › compose
Docker-compose with --context - Compose - Docker Community Forums
November 10, 2020 - If I take this exact docker-compose.yml file and put it on my local computer then add a docker context to my server like this: docker context create remote --docker "host=ssh://username@servername" Note: The username here is the same I use when ...
🌐
GitHub
github.com › docker › compose › issues › 4857
Build context is always the location of docker-compose.yaml · Issue #4857 · docker/compose
May 23, 2017 - I've tried a bunch of variants (calling from inside deploy/ without the --file option, using --project-directory to specify the project location (which makes compose very slow!), etc). And I always see the content of the deploy/ dir. The only way I have been able to build a working site is by moving docker-compose.yaml to the project root (and adjusting the build context to .).
Published   May 23, 2017
Author   bitterjug
🌐
Docker
docker.com › blog › dockerfiles-now-support-multiple-build-contexts
Dockerfiles now Support Multiple Build Contexts | Docker
May 3, 2022 - Named build context defined with --build-context [name]=.. ... If no --from flag is set, files are loaded from the main build context.
🌐
GitHub
github.com › docker › compose › issues › 4926
Want a way to specify a dockerfile outside the context in the docker-compose dockerfile directive · Issue #4926 · docker/compose
June 14, 2017 - OK, but it seems to me that that should not apply to the dockerfile directive. After all, you can specify a dockerfile outside the context when using the docker build command itself, using -f.
Author   bitdancer
🌐
APXML
apxml.com › courses › docker-for-ml-projects › chapter-6-docker-compose-ml-workflows › compose-building-images
Building Docker Images via Docker Compose
The primary mechanism for this is the build keyword within a service definition in your docker-compose.yml file. It replaces or complements the image keyword. At its simplest, you can provide the build keyword with a string value representing the path to the directory containing the Dockerfile. ...
🌐
Medium
medium.com › @achanandhi.m › docker-compose-build-what-it-is-and-when-you-need-it-a79788f42fce
Docker-compose build- What It Is and When You Need It? | by Achanandhi M | Medium
April 10, 2025 - This tells Docker to build the images first if needed and then start the containers — all in one command. 🚀 ... FROM python:3.11 WORKDIR /app COPY . . RUN pip install -r requirements.txt CMD ["python", "app.py"] ... Think of docker-compose ...
🌐
LabEx
labex.io › tutorials › docker-how-to-use-docker-compose-build-command-to-build-services-555073
How to use docker compose build command to build services | LabEx
context: . specifies the build context, which is the directory containing the Dockerfile. args: is a map of build arguments to pass to the Dockerfile. We are passing the value "Custom message from Compose!" to the MESSAGE argument.
🌐
Tvaidyan
tvaidyan.com › 2024 › 12 › 05 › contexts-and-additional-contexts-in-docker-compose
Contexts and Additional Contexts in Docker Compose – Tom Vaidyan
December 5, 2024 - Now, with all that introduction out of the way, in Docker, the build context refers to the set of files and directories accessible to the Docker engine when building an image. In Docker Compose, the context is specified within the build section of a service definition.
🌐
Docker Community
forums.docker.com › docker engine › compose
Relative path of build context in docker-compose configuration is not resolved relative to the compose yml file location - Compose - Docker Community Forums
November 22, 2017 - I have a docker-compose project with container configurations in subdirectories like . ├── a.yml └── b └── b.yml a.yml is version: "2.2" services: a: build: context: ./ b.yml is version: "2.2" services: b: build: context: ./ COMPOSE_FILE=a.yml:b/b.yml docker-compose config results in services: a: build: context: /Volumes/Disk/projects/docker-compose-relative-path b: build: context: /Volumes/Disk/projects/docker-compose-relative-path v...
🌐
Warp
warp.dev › terminus › docker-compose-target
Warp: Understanding The Target Field In Docker Compose
February 16, 2024 - In Docker Compose, the target property ... to build, you can use the target property as follows: Where: context is the relative or absolute path to the Dockerfile ....
🌐
OneUptime
oneuptime.com › home › blog › how to use multiple build contexts in docker builds
How to Use Multiple Build Contexts in Docker Builds
February 8, 2026 - # docker-compose.yml with multiple build contexts version: "3.9" services: api: build: context: ./services/api additional_contexts: shared: ./shared proto: ./proto dockerfile: Dockerfile ports: - "8080:8080" web: build: context: ./services/web additional_contexts: shared: ./shared assets: ./design/assets dockerfile: Dockerfile ports: - "3000:3000"