Skip to content

Instantly share code, notes, and snippets.

@tsilvs
Last active April 27, 2025 12:58
Show Gist options
  • Save tsilvs/e52b8612bae3568ca72bd24c0eb6f6fd to your computer and use it in GitHub Desktop.
Save tsilvs/e52b8612bae3568ca72bd24c0eb6f6fd to your computer and use it in GitHub Desktop.
Docker Inspect Export as Compose
# Put in ~/.jq file
def yamlify2:
(objects | to_entries | (map(.key | length) | max + 2) as $w |
.[] | (.value | type) as $type |
if $type == "array" then
"\(.key):", (.value | yamlify2)
elif $type == "object" then
"\(.key):", " \(.value | yamlify2)"
else
"\(.key):\(" " * (.key | $w - length))\(.value)"
end
)
// (arrays | select(length > 0)[] | [yamlify2] |
" - \(.[0])", " \(.[1:][])"
)
// .
;
#!/bin/bash
# Usage: docker-compose-export.sh <container_name>
sudo docker inspect --format json "$1" | jq -r '{
services: .[] | {
(.Name | ltrimstr("/")): {
container_name: (.Name | ltrimstr("/")),
image: (.Config.Image),
ports: (
.HostConfig.PortBindings
| to_entries
| map(
.key as $container_port
| .value[]
| "\(.HostPort):\($container_port)"
)
),
environment: (.Config.Env | map(split("=") | {(.[0]): (.[1:] | join("="))} | select(.[] != "")) | add),
volumes: (.HostConfig.Binds),
restart: (.HostConfig.RestartPolicy.Name)
}
}
}' | jq -r yamlify2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment