Search code examples
dockerdocker-composedocker-swarm

How to extract docker-compose file from running docker start


I have docker stack started with docker stack deploy --compose-file ... and later manually edited via Docker Portainer UI.

I'd like to write a script that updates the docker image tag of one of the services.

To do that I need to "download" the latest "docker-compose" stack definition however I cannot find the appropriate docker command.

I do know that the best would be to stop changing stack manually and rely on its definition stored in git but unfortunately, it is not up to me.

Please point me to the appropriate docker command or confirm that it is not available.


Solution

  • As far as i know there is no command you could get the compose file from the running container directly. At least not implemented out of the box in docker. You could try to parse all the relevant information from docker inspect and few other commands to list/inspect all relevant objects?.

    I have once came across the similar situation where we had a running container but no run/compose command which we needed to update. At the time (roughly a year ago) i found and used docker-autocompose which did very good job. We only had to manually verify and adjust few things,but it got all the difficult parts with run parameters done for us.

    It could help in your case to automate it if your compose configs are simple enough.

    But if you wanted to fully automate it to mimic CD, then i would not recommend the approach above. In that case i would check if you could use portainer api as @LinFelix recommended. Or store compose files somewhere - prepared with parameters ($IMAGE_TAG) (git/on server) so you can then generate temporary compose files with all configuration and then remove the current one.