cclloyd
cclloyd

Reputation: 9195

Include directory of docker-compose files

How can I run docker-compose with the base docker-compose.yml and a whole directory of docker-compose files.

Like if I had this directory structure:

parentdir
    docker-compose.yml
    folder1/
        foo.yml
        bar.yml
    folder2/
        foo.yml
        other.yml

How can I specify which folder of manifests to run when running compose?

Upvotes: 0

Views: 3025

Answers (3)

zaibaq
zaibaq

Reputation: 157

You can pass multiple files to docker-compose by using -f for each file. For example if you have N files, you can pass them as follows:

docker-compose -f file1 -f file2 ... -f fileN [up|down|pull|...]

If you have files in sub-directories and you want to pass them to docker-compose recursively, you can use the following:

docker-compose $(for i in $(find . -type f | grep yaml)
do
echo -f $i
done
) [up|down|pull|...]

Upvotes: 0

Mornor
Mornor

Reputation: 3783

I hope i understood your question well.
You could use the -f flags: docker-compose -f docker-compose1.yml

Edit
To answer your comment: no you can't docker-compose several files with only one command. You need to specify a file path, not a directory path.
What you could do is create a shell script like:

#!/bin/bash

DOCKERFILE_PATH=$DOCKER_PATH

for dockerfile in $DOCKERFILE_PATH
do
  if [[ -f $dockerfile ]]; then
    docker-compose -f $dockerfile
  fi;
done

By calling it like: DOCKER_PATH=dockerfiles/* ./script.sh which will execute docker-compose -f with every files in DOCKER_PATH.

(docs)

Upvotes: 1

cclloyd
cclloyd

Reputation: 9195

My best option was to have a run.bash file in the base directory of my project.

I then put all my compose files in say compose/ directory, then run it with this command:

docker-compose $(./run.bash) up

run.bash:

#!/usr/bin/env bash

PROJECT_NAME='projectname'     # need to set manually since it normally uses current directory as project name
DOCKER_PATH=$PWD/compose/*
MANIFESTS=' '

for dockerfile in $DOCKER_PATH
do
    MANIFESTS="${MANIFESTS} -f $dockerfile"
done

MANIFESTS="${MANIFESTS} -p $PROJECT_NAME"

echo $MANIFESTS

Upvotes: 0

Related Questions