Skip to content

Instantly share code, notes, and snippets.

@eskerda
Created August 17, 2022 11:20
Show Gist options
  • Save eskerda/954c68e3f2e1d069a96ef4fd254862ef to your computer and use it in GitHub Desktop.
Save eskerda/954c68e3f2e1d069a96ef4fd254862ef to your computer and use it in GitHub Desktop.
# Attach a volume to a service using a set of clever defaults
#
# usage:
# attach-compose.sh <service> <path>
# attach-compose.sh --no-cmd <service> <path>
function _compose {
local flags=()
[[ -f docker-compose.yml ]] && \
flags+=("-f docker-compose.yml")
[[ -f docker-compose.override.yml ]] && \
flags+=("-f docker-compose.override.yml")
docker-compose ${flags[@]} $@
}
function _attach_compose {
local service=$1
local path=$(realpath $2)
local image=$(_compose images $service -q)
local workingdir=$(docker inspect $image -f '{{ .Config.WorkingDir }}')
cat << EOF
services:
$1:
command: tail -f /dev/null
volumes:
- ${path}:${workingdir}/$(basename ${path})
EOF
}
function attach_compose {
local service=$1; local path=$2; shift 2
_compose -f <(_attach_compose $service $path) $@
}
attach_compose $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment