Docker Tip #30: Running Docker Compose from a Different Directory
Once in a while you may want to execute Docker Compose commands from somewhere other than your project's directory. Here's how.
Typically when you want to interact with a Docker Compose project you would
cd
into the Compose project’s directory, otherwise known as the directory
that contains your docker-compose.yml
file.
For day to day usage, that’s something you’ll do a lot but when it comes to
running things in production and creating automation scripts that execute
docker-compose
commands, it’s often easier to just pass
in the location of the docker-compose.yml
file to docker-compose
itself.
Docker Compose has an -f
flag where you can pass in the location of the
docker-compose.yml
file (or a custom file if you happen to name it something
different).
That means you could run: docker-compose -f /tmp/myproject/docker-compose.yml up -d
from anywhere on your file system and things will work. I use this a lot in my
scripts.
You can also load multiple docker-compose.yml
files by supplying the -f
flag more than once. Docker Tip #87 goes over how that’s done.