background
The author accidentally deleted the original file once before docker-compose.yml
, but the running Docker containers are still there. After searching for a long time, I found a method to generate an equivalent Docker Compose configuration file from these containers. This article describes using autocompose
the tool to reverse generate a file from a running container docker-compose.yml
.
Tools required for installation:
First, make sure you have installed docker-autocompose
the tool, and associated Python packages.
Install docker-autocompose
docker pull ghcr.io/red5d/docker-autocompose:latest
Generate Docker Compose file
Use the following command to reverse-generate a Docker Compose configuration file from a running container. Replace <container-name-or-id>
with the name or ID of the container you want to export.
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock ghcr.io/red5d/docker-autocompose <container-name-or-id>
Manual editing and adjustments:
The generated docker-compose.yml
file may contain many details, especially the data volume inside the container, environment variables, etc. You may need to further manually edit and tweak the resulting file, such as making some cuts. Check the configuration of the container's port mapping, network settings, volume mounts, etc.
Start the service:
docker-compose.yml
Save and rebuild the service based on the generated file. Start the service with the following command:
docker-compose up -d
You have now successfully generated a new Docker Compose configuration file from a running container and successfully re-built the service.
in conclusion
docker-autocompose
The tool provides a convenient way to reverse-generate a Docker Compose configuration file from a running Docker container. When the original configuration file is lost, this tool can help us quickly restore the service environment and avoid the workload caused by repeated configuration.