Compose: Build context is always the location of docker-compose.yaml

Created on 23 May 2017  ยท  13Comments  ยท  Source: docker/compose

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/
  |-- deploy/
  |    |-- docker-compose.yaml
  |
  |-- Dockerfile
  |-- wp-config.php
  |-- index.php
  | ... etc...

(Ideally I'd like Dockerfile in deploy/ too, but first things first)

I cant find a way to build my project so that the whole project/ dir becomes the build context.]
Here's how I think it should work:

# docker-compose.yaml
version: '3'

services:
    db:
    ...

    web
      build: 
         context: ..
      ...

volumnes:
 ...

Then, in project/:

  $ docker-compose --file deploy/docker-compose.yaml build --no-cache

At which point

  $ docker-compose --file deploy/docker-compose.yaml run web ls

Should show my wordpress files. But instead it shows contents od deploy/ dir.

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 .).

Is this supposed to work? If so, how should I approach it?

$ docker-compose --version
docker-compose version 1.13.0, build 1719ceb
$ docker version
Client:
 Version:      17.05.0-ce
 API version:  1.29
 Go version:   go1.7.5
 Git commit:   89658be
 Built:        Thu May  4 22:10:54 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.05.0-ce
 API version:  1.29 (minimum version 1.12)
 Go version:   go1.7.5
 Git commit:   89658be
 Built:        Thu May  4 22:10:54 2017
 OS/Arch:      linux/amd64
 Experimental: false
kinquestion stale

Most helpful comment

The bug fully shows itself when COMPOSE_FILE directive is being used.
Demonstration.
Directory /Users/alexei.chekulaev/tmp/bug_demo

Structure:

bug_demo/
  conf/
    docker-compose.yml
  build/
    Dockerfile

docker-compose.yml contents:

version: "2.1"

services:
  test:
    build: build

Actual result when building from bug_demo dir:

$ pwd
/Users/alexei.chekulaev/tmp/bug_demo

$ COMPOSE_FILE="conf/docker-compose.yml" docker-compose config
ERROR: build path /Users/alexei.chekulaev/tmp/bug_demo/conf/build either does not exist, is not accessible, or is not a valid URL.

Expected result: docker-compose should use CWD as current path, not the path where first config file was found.

All 13 comments

I've been meaning to look at this more closely but haven't had the time yet. Thank you for being patient!

Possibly related to #3874

Experiencing the same issue with docker-compose in a subdirectory and have tried a variety of different methods to no avail as well. Only with docker-compose.yml and Dockerfile at root of the project am I able to successfully run the container.

Will move forward for now with all files at root but am looking forward to any suggestions / resolutions on how to get this working from a subdirectory to clean up the messiness of root.

@bitterjug So, a similar setup works just fine for me:

$ tree
.
โ”œโ”€โ”€ a.txt
โ”œโ”€โ”€ deploy
โ”‚ย ย  โ””โ”€โ”€ docker-compose.yml
โ””โ”€โ”€ Dockerfile

1 directory, 3 files

$ cat Dockerfile 
FROM alpine
COPY . /optional

$ cat deploy/docker-compose.yml 
version: '2.1'
services:
  foo:
    build:
      context: ..
      dockerfile: ./Dockerfile
    command: ls /optional

$ docker-compose -f deploy/docker-compose.yml build --no-cache
Building foo
Step 1/2 : FROM alpine
 ---> 4a415e366388
Step 2/2 : COPY . /optional
 ---> 08a1c85a6ba6
Removing intermediate container ca3cc5a308c7
Successfully built 08a1c85a6ba6
Successfully tagged deploy_foo:latest

$ docker-compose -f deploy/docker-compose.yml run foo ls /optional
Dockerfile  a.txt       deploy

$ docker-compose -f deploy/docker-compose.yml run foo cat /optional/a.txt
Hello World

Is there anything else in your Dockerfile or Compose file that could cause the discrepancy?

I have a similar problem right now, but with a slightly different directory structure:

project root/
  docker-compose.yml
  backend/
    Dockerfile
    somefile.py
  frontend/
    ...
version: '2'
services:
  web:
    build: ./backend/
    command: python somefile.py

Instead of running in ./backend/ it runs in ./, so ADD . copies the whole project and somefile.py ends up in a wrong directory.

UPD: sorry, I made a silly mistake in config. My problem is actually unrelated to this.

Schoolboy error. :blush:

I was mounting . as a volume which defaults to the root of the build context. Volumes are not constrained to be in the build context, of course. So I can put docker-compose.yaml and my Dockerfile in deploy as I wanted. And mount ../ as my volume.

I solved this problem by using environment variables. (it is not smart solution)

/home/tomi/repos/docker-compose_sample% tree ./
./
โ”œโ”€โ”€ back
โ”‚ย ย  โ”œโ”€โ”€ back
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ Dockerfile
โ”‚ย ย  โ””โ”€โ”€ docker-compose.yml
โ”œโ”€โ”€ docker-compose.override.yml
โ”œโ”€โ”€ docker-compose.yml
โ””โ”€โ”€ ubuntu
    โ”œโ”€โ”€ docker-compose.yml
    โ””โ”€โ”€ front
        โ””โ”€โ”€ Dockerfile

4 directories, 6 files

/home/tomi/repos/docker-compose_sample% cat back/docker-compose.yml 
version: '2'

services:
  back-web:
    build: ${BACK_ROOT}back

/home/tomi/repos/docker-compose_sample% cat ubuntu/docker-compose.yml 
version: '2'

services:
  front-web:
    build: ${UBUNTU_ROOT}front

/home/tomi/repos/docker-compose_sample% cat docker-compose.yml 
version: '2'

/home/tomi/repos/docker-compose_sample% cat docker-compose.override.yml 
version: '2'
services:
  front-web:
    depends_on:
      - back-web
  back-web:

/home/tomi/repos/docker-compose_sample% BACK_ROOT=back/ UBUNTU_ROOT=ubuntu/ docker-compose -f docker-compose.yml -f back/docker-compose.yml -f ubuntu/docker-compose.yml -f docker-compose.override.yml up -d --build
Building back-web
Step 1/1 : FROM ubuntu
 ---> ccc7a11d65b1
Successfully built ccc7a11d65b1
Successfully tagged dockercomposesample_back-web:latest
Building front-web
Step 1/1 : FROM ubuntu
 ---> ccc7a11d65b1
Successfully built ccc7a11d65b1
Successfully tagged dockercomposesample_front-web:latest
Starting dockercomposesample_back-web_1 ... 
Starting dockercomposesample_back-web_1 ... done
Starting dockercomposesample_front-web_1 ... 
Starting dockercomposesample_front-web_1 ... done

The bug fully shows itself when COMPOSE_FILE directive is being used.
Demonstration.
Directory /Users/alexei.chekulaev/tmp/bug_demo

Structure:

bug_demo/
  conf/
    docker-compose.yml
  build/
    Dockerfile

docker-compose.yml contents:

version: "2.1"

services:
  test:
    build: build

Actual result when building from bug_demo dir:

$ pwd
/Users/alexei.chekulaev/tmp/bug_demo

$ COMPOSE_FILE="conf/docker-compose.yml" docker-compose config
ERROR: build path /Users/alexei.chekulaev/tmp/bug_demo/conf/build either does not exist, is not accessible, or is not a valid URL.

Expected result: docker-compose should use CWD as current path, not the path where first config file was found.

@bitterjug Would you mind posting your full setup? I'm still stuck with this issue :-/

In will be good to ignore .dockerignore file from the project's root when e.g. using .Dockerfile.development (defined in this line: dockerfile: Dockerfile.development). Such solution (usage of the project's subdirectories) seems to me never works.

[ ]. build context: .. - ~work~

[ ]. COMPOSE_FILE=F U L L _ P A T H . . .

[ ]. Possibility to ignore the .dockerignore file.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This issue has been automatically closed because it had not recent activity during the stale period.

I believe this is still an issue I've encountered same problem with latest RC release! It takes the path of first main docker-compose.yml file, but not the last one. https://github.com/docker/compose/issues/7350

Was this page helpful?
0 / 5 - 0 ratings