Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting context in docker-compose file for a parent folder

I got a docker-compose file in which I want to set a context and docker file to look something like this:

build:
  context: <path to context>
  dockerfile: <path to dockerfile>

For now my file is in the root folder so its simply:

build: 
  context: .
  dockerfile: .

This way it does work.

The structure of the project is something like this:

./
  - folder1/
    - folder2/
         docker-compose.yaml
         DockerFile

I want to copy files as part of the commands in the DockerFile and I want the paths to be relative to the root folder of the project.

How with this project structure do I set the context to be the root folder of the project? I tried doing context: ../../ but I then got an error:

Error response from daemon: unexpected error reading Dockerfile: read (path): is a directory

How do I set the context correctly?

like image 758
Yonatan Nir Avatar asked Jan 25 '26 15:01

Yonatan Nir


2 Answers

You've set:

 dockerfile: .

Just try to use a relative path to you Dockerfile from the set context:

context: ../../
dockerfile: ./folder1/folder2/Dockerfile
like image 88
user1941546 Avatar answered Jan 27 '26 07:01

user1941546


You cannot change the context to be a parent folder, you can read it about it in the docker-compose documentation. https://docs.docker.com/compose/compose-file/compose-file-v3/#context

what you can do is have the composefile in the folder which in the the highest hierarchy and change the context accordingly. like so:

version: "3.7"

services:

  srv2:
    build:
      dockerfile: src/services/srv2/Dockerfile
      context: ./
    image: srv2

  srv1:
    build:
      dockerfile: src/services/srv1/Dockerfile
      context: ./
    
    image: srv1

or you can make the context the directory of the service that contains the Dockerfile of the service

like image 27
Eitank Avatar answered Jan 27 '26 06:01

Eitank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!