-
-
Save tsilvs/e52b8612bae3568ca72bd24c0eb6f6fd to your computer and use it in GitHub Desktop.
Docker Inspect Export as Compose
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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:][])" | |
) | |
// . | |
; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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