docker-inspect2compose
is a simple Python program that converts Docker container information into a
Docker Compose service definition. It retrieves information about running containers on the host
and generates a docker-compose.yml
configuration. The script can also merge this information
into an existing docker-compose.yml
file.
Why would you want this ? Maybe you started a container using docker run
. Maybe you started it so
long ago, you don't quite remember the volume mounts and other options you used. This allows you
to capture some key information to create a more permanent Docker Compose definition.
- Retrieves Docker container information using Docker SDK or
docker inspect
command. - Generates Docker Compose service (
docker-compose.yml
) definition. - Includes
deploy.restart_policy
,deploy.resources
,logging
, andnetworks
sections if available. - Include environment variable (optionally including
PATH
) --add-to
merges a new service definition into an existingdocker-compose.yml
file.
The output may not capture every possible Docker Compose configuration option, but captures the main settings used in simple deployments. Sections like
build:
won't be added sincedocker inspect
doesn't have this information.
- Python 3.6+
- Docker SDK for Python (
docker-py
) - PyYAML
-
Clone the repository:
git clone <repository-url> cd <repository-directory>
-
Install the package:
pip install .
First, find a running container id or name using docker ps
.
docker-inspect2compose [container_id_or_name] [options]
--output
,-o
: Output file to write the Docker Compose definition. Use-
for stdout. Default is-
.--include-path-env
: Include thePATH
environment variable in the output.--add-to
: Path to an existingdocker-compose.yml
file to add the new service to.
-
Print the Docker Compose definition for all running containers to stdout:
docker-inspect2compose
-
Write the Docker Compose definition to a file for a single container:
docker-inspect2compose [container_id_or_name] --output <output_file>
-
Include the
PATH
environment variable in the output:docker-inspect2compose --include-path-env
-
Add a new service to an existing
docker-compose.yml
file:docker-inspect2compose [container_id_or_name] --add-to <path_to_existing_docker_compose_yml> --output <output_file>
This project is licensed under the MIT License. See the LICENSE
file for details.
- kurron/docker-inspect-to-compose - a Java implementation of a similar idea that worked for me, but outputs a more complex (complete?) service definition.