Find the directory where docker-compose was launched

I often start docker-compose in a directory and after a long hours and even days, it happens that I accidently close terminals or change directory and as a consequence, I couldn’t find the direcory from which I originally started docker-compose and cannot properly shutdown the compose containers.

In this case I just stop the containers one-by-one and delete them.

Any hint to find from what directory a docker-compose was launched?

Going to need a bit more information for anyone to help you. Let’s star with …

What OS ?
How was Docker and docker-compose installed ?
Are you running docker-compose as a binary from a command-prompt or as a container ?

Look back at you question and think, would anyone be able to answer my question knowing nothing about my execution context and only from the information I have provided. Help us to help you !

Thanks @goffinf for your reply.

How docker-compose was installed:

$ sudo curl -L https://github.com/docker/compose/releases/download/1.20.1/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose

$ sudo chmod +x /usr/local/bin/docker-compose

$ docker-compose --version
docker-compose version 1.20.1, build 5d8c71b

Environment:

$ which docker-compose 
/usr/local/bin/docker-compose

$ lsb_release -a
Distributor ID:	Debian
Description:	Debian GNU/Linux 8.9 (jessie)
Release:	8.9
Codename:	jessie

$ uname -a
Linux gns3-iouvm 3.16.0-4-amd64 #1 SMP Debian 3.16.43-2+deb8u2 (2017-06-26) x86_64 GNU/Linux

I didn’t estimate additional inf. useful because I thought this is inherent to docker-compose and I was looking for eventual known workaround or a command option to tell from what directory docker-compose was launched (independently of OS and context).

It looks like docker-compose reads only the local directory for docker-compose.yml to start, list and tear down the containers in the file.

Right so docker-compose was installed in /usr/local/bin, so just stick that on your path

I think he meant where was the compose file in executed the command from.

I guess maybe just do a ls | grep *compose.yml.

Also sometimes the container name is pre fixed with the folder name.

Apologize if that command doesn’t work. My bash knowledge isn’t first-class

I couldn’t find a simple a posteriori method to find the docker-composer launching directory, but you could create a wrapper script to register that operation in a log stored in a known location. As an example, lets make an script in /usr/local/bin/dkcompose.sh as follows:

#!/bin/bash
# register docker-compose commands and where they were launched.
#------------
LOG="/home/ajnouri/dkcompose.log"
NOW=$(date '+%F %T')
DIR=$(pwd)
echo "$NOW  Directory: $DIR" >> $LOG
echo "$NOW  Command: docker-compose $*" >> $LOG
docker-compose $*

#-- end --#

Don’t forget to chmod a+rx /usr/local/bin/dkcompose.sh to allow execution of the script. As the script is in a directory in the $PATH variable can be called from anywhere, so you could say

cd /projects/myproject2
dkcompose up

And the script will store the following in the LOG file before calling docker-compose:

2018-05-05 14:18:23 Directory: /projects/myproyect2
2018-05-05 14:18:23 Command: docker-compose up

Using the script in this way you will log any docker-compose command and when and where it was executed.

PORTABILITY: If you use a distinct shell from Bash, change the first line to identify your shell and $(command) expressions with backticks: `command`. If you cannot put the script in a global directory in the $PATH, put it in your home directory, apply the chmod a+rx to it and then call it directly from anywhere with:

cd /projects/myproject2
/home/ajnouri/dkcompose.sh up
1 Like

I agree with @jonyeezs suggestion. By default, docker-compose names the containers <directory_name>_<container_name>_#. You should be able to determine the directory name from that.

For example: if you launched docker-compose in a directory called xzy-project that had a container named webapp, the running container returned from docker ps would be named xzy-project_webapp_1. The only questions is how many directories called xzy-project are on your system? (but that should narrow it down)

~jr

1 Like

Thanks a lot @ssfjor.
That’s a good idea of script.

Here’s how I did it. First find any running docker-compose processes.

ps aux | grep docker

I noticed some docker-compose processes with the --file flag set to a docker compose YAML file, so I searched for that file name.

find / -name "docker-compose.yml"

Once I found the file I ran docker-compose using it.

 docker-compose --file /path/to/docker-compose.yml

Hi,
even if some delay it may help someone. Inspecting the docker container Labels may provide the required answer

docker inspect <container_name> | grep "com.docker.compose.project.working_dir"
4 Likes

You can use this method to view all running containers

docker inspect --format='container name: {{.Name}}, path: {{index (index .Config.Labels "com.docker.compose.project.working_dir")}}' $(docker ps)

Or for one specific

docker inspect --format='{{index (index .Config.Labels "com.docker.compose.project.working_dir")}}' <INSTANCE_ID>

In general, you can also search using other keys, everything is explained perfectly in the official documentation

1 Like