Docker Tip #30: Running Docker Compose From a Different Directory

blog/cards/docker-tips-and-tricks.jpg

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.

Free Intro to Docker Email Course

Over 5 days you'll get 1 email per day that includes video and text from the premium Dive Into Docker course. By the end of the 5 days you'll have hands on experience using Docker to serve a website.


Comments