10

I have docker-compose.yml file with build context property specified like this:

version: '3'
services:
  my-service:
    container_name: my-service
    image: my-service
    build:
      context: foo
    ports:
    - 8088:8088

  # other services

When I run docker-compose up locally, build context does exist and everything works fine. However, my CI server is configured to use the same docker-compose.yml file but there is no build context (images are copied as .tar archive via SSH and then loaded via docker load). Now I've got an error:

ERROR: build path /foo either does not exist, is not accessible, or is not a valid URL.

So I've tried to find a way to suppress looking for this build context when running docker-compose up (I don't want to build images cause they are already up-to-date), but docker-compose up --no-build does not work. Any ideas?

k13i
  • 4,011
  • 3
  • 35
  • 63
  • just give set it to an empty dir if you want to avoid setting it to `.`. But the directory must exists thats why (amongst other reasons) a common practice to create a `docker` dir in the project root, and move the `Docerfile` into it. Then build in the `docker` directory which will be an empty context. – lependu Nov 20 '18 at 09:43
  • 3
    But the point is that I don't want to modify docker-compose.yml file. This file is copied from repository to CI server, so I don't want to change it (I would have to use some regex probably). Alternatively, I can maintain two separate compose files, but I'd like to avoid it if it's unnecessary – k13i Nov 20 '18 at 10:01
  • 1
    Did you try to add a `foo` directory in your root of a repo with am empty `.keep` or `Readme` file in it which explains why the empty dir is necessary. – lependu Nov 20 '18 at 11:18
  • 1
    On CI server there is no foo directory. There is just .tar archive with docker image – k13i Nov 20 '18 at 12:11
  • 2
    I opened a feature request based on your question, in case anyone wants to thumbs-up there: https://github.com/docker/compose/issues/7674 – zeehio Aug 17 '20 at 09:43
  • 1
    I found an open discussion on the same issue https://github.com/docker/compose/issues/4047. it does not solve the problem but there are some workarounds – Maria Dorohin Aug 25 '20 at 08:32

3 Answers3

3

I posted your issue as a feature request on the docker-compose repository. Let's see how it progresses:

https://github.com/docker/compose/issues/7674

Meanwhile, you will have to workaround this by modifying your CI script that does the docker-compose up --no-build so it does the mkdir -p ... that you need.

zeehio
  • 4,023
  • 2
  • 34
  • 48
2

docker-compose.override.yml is good solution in this case. You may override only build block and this is not hard to mantain as two independent files.

docker-compose.override.yml:

version: '3'
  services:
    my-service:
      build:
        context: foo

docker-compose.yml

version: '3'
  services:
    my-service:
      container_name: my-service
      image: my-service
      ports:
        - 8088:8088

See https://docs.docker.com/compose/extends/

dngnezdi
  • 29
  • 2
  • 1
    IMHO, it leads to an extra complexity. Just use `docker-compose up --no-build`. As of what @zeehio has said. – Emran Apr 26 '21 at 07:21
  • @EmranBatmanGhelich we can use --no-build for up, but we still face the same issue for down. docker-compose down still needs build context and --no-build doesn't work for down – Hieu Le Nov 06 '22 at 18:54
0

I had the same problem, and my solution, until "docker-compose config" provides a way to skip the directory-exists check, is to automatically create those directories that "docker-compose config" expects.

Here is a one-liner that does this:

egrep '    (context:|build)' < docker-compose.yml | sed -E 's/\s+\S+:\s*//' | xargs mkdir -p .

It's ugly, but I couldn't figure out another way. (The .extends. way mentioned by dngnezdi is not a solution, as I needed an automatic method.)

knipknap
  • 5,934
  • 7
  • 39
  • 43
  • I tried it but I get an error "Cannot locate specified Dockerfile": Dockerfile and if I create an empty Dockerfile I get an error that "the Dockerfile (Dockerfile) cannot be empty". what did you do with that? – Maria Dorohin Sep 01 '20 at 21:07