Find the directory where docker-compose was launched

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