Generating a docker-compose file from a running container

Simple steps to convert a running docker container into a docker compose file.

This is essentially a reminder post to myself.

I needed to convert a running container to a docker-compose file in order to migrate it from one host to another using Portainer UI. Came up to this repo below:

GitHub - Red5d/docker-autocompose: Generate a docker-compose yaml definition from a running container
Generate a docker-compose yaml definition from a running container - Red5d/docker-autocompose

Usage is pretty simple. Pull the docker image for docker-autocompose, then run it with the container name that you want to turn its runtime details into a docker-compose file. The commands are available in the repo, but I will replicate them here as well for quick reference.

Pull docker-autocompose:

docker pull ghcr.io/red5d/docker-autocompose:latest

Execute:

docker run --rm -v /var/run/docker.sock:/var/run/docker.sock ghcr.io/red5d/docker-autocompose <container-name-or-id> <additional-names-or-ids>...

To convert all containers into compose file:

docker run --rm -v /var/run/docker.sock:/var/run/docker.sock ghcr.io/red5d/docker-autocompose $(docker ps -aq)