Docker Community Forums

Share and learn in the Docker community.

Find the directory where docker-compose was launched

docker

(Ajnouri) #1

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?


(Goffinf) #2

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 !


(Ajnouri) #3

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.


(Goffinf) #4

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


(Jonyeezs) #5

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


(Francisco de Jesús Orozco Ruiz) #6

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

(John Rofrano) #7

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


(Ajnouri) #8

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