简体   繁体   中英

docker-compose with multiple files in multiple directories

I have a few repos that I want to deploy using docker-compose within the same project, for each I have a docker-compose.yaml in the root of the directory that defines the list of services. The docker files are located in docker/{service}/Dockerfile relative to the repo root, and hence the docker-compose.yaml looks something like

version: "3"
services:
  service1:
    build:
      context: .
      dockerfile: ./docker/service1/Dockerfile
    networks:
      - default
  service2:
    build:
      context: .
      dockerfile: ./docker/service2/Dockerfile
    networks:
      - default
  service3:
    build:
      context: .
      dockerfile: ./docker/service3/Dockerfile
    networks:
      - default
networks:
  default:

However when I run the command from the directory above the repo roots

docker-compose -p project -f repo1/docker-compose.yaml -f repo2/docker-compose.yaml build

I get the error ERROR: Cannot locate specified Dockerfile: ./docker/service1/Dockerfile Which is a service from repo2

These commands run fine with just one file specified and only one set of services are "not found" (repo2's). I assume that the first file is therefore setting the context of compose, is there a way I can tell compose not to do this?

The documentation for the docker-compose -f option notes:

When you use multiple Compose files, all paths in the files are relative to the first configuration file specified with -f .

Practically, this probably means all of the Compose files need to be in the same directory. If you have a file that really has no host paths at all (neither a build: context directory nor volumes: bind mounts) it could in principle be somewhere else, but IME this is pretty unusual.

For the setup you describe, it might be more practical to launch the separate projects separately:

(cd repo1 && docker-compose up --build -d)
(cd repo2 && docker-compose up --build -d)

A more typical use of multiple Compose files is to provide options split across several files; for example, a base file that specifies image names and an override file that provides developer-oriented features like publishing database ports and building images from source. Share Compose configurations between files and projects describes this use case a little more. That page similarly comments:

Tracking which fragment of a service is relative to which path is difficult and confusing, so to keep paths easier to understand, all paths must be defined relative to the base file.

In short: it's an intentional feature of Compose that all path references in all Compose files are relative to the location of the first file, regardless of the location of the file that contains the path reference, and there's not an override option for it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM