2

I was wondering if it would be possible to list all running docker-compose files with a bash function? Something like docker ps or docker container ls, but I want it to show the files only.

I got started with this - lsof | egrep 'REG|DIR' | awk '{print $9}' but this provides me tons of unwanted information as well.

What would be the best approach here?

Thanks in advance.

kvelev
  • 53
  • 10

3 Answers3

2

This bash oneliner shows the working dir of each container's compose file.

for c in `docker ps -q`; do echo $c; docker inspect $c --format '{{index .Config.Labels "com.docker.compose.project.working_dir"}}' ; done

I edited kvelev's command to get this. kvelev's command was just printing "docker-compose.yaml" for each of my loaded containers, so I edited the filter to show the working dir, which works for me.

gagany
  • 21
  • 3
1

So I played around with docker inspect and came up with that:

for c in `docker ps -q`; do echo $c; docker inspect $c --format '{{index .Config.Labels "com.docker.compose.project.config_files"}}' ; done

So it is possible ;)

kvelev
  • 53
  • 10
0

It is not possible to backtrack and find the docker-compose file that was used in container deployment. To overcome such issues, a project pipeline is recommended using tools like maven, jenkins, gradle etc along with a repository platform like github. If its a personal project you can organize your project by wrapping the docker deployment commands and source files in a script and only use them to create deployments. This way it will be organized to some extent.

Vijayendar Gururaja
  • 752
  • 1
  • 9
  • 16
  • Oh, okay, I see. Could you suggest a good starting point for wrapping the project then? – kvelev Dec 07 '21 at 13:49
  • A good starting point would be to create a repository on github.com and commit you're files on to it. Additionally you can name your directories with names that makes sense to you. If possible you can maintain a versions file and keep updating the versions each time you commit a change. – Vijayendar Gururaja Dec 07 '21 at 14:41
  • Alright, thanks for the feedback. Sadly, I'd have to disagree with you, I'm using a VS code extension that does just that - here's a screenshot for reference: https://prnt.sc/22a1mt7 It would show the container and its docker-compose file, I just have to figure out how to do it with bash ( : – kvelev Dec 08 '21 at 08:35