Skip to content

Instantly share code, notes, and snippets.

@ypresto
Last active March 25, 2021 18:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ypresto/84c600c7bedf3dc4f947f8c5c6ab36b5 to your computer and use it in GitHub Desktop.
Save ypresto/84c600c7bedf3dc4f947f8c5c6ab36b5 to your computer and use it in GitHub Desktop.
Use mutagen with docker-sync like setup.
version: "3.7"
services:
service1:
volumes:
- your-app-mutagen-service1:/var/app:nocopy
service2:
volumes:
- your-app-mutagen-service2:/var/app:nocopy
mutagen-sync:
image: alpine:latest
command: tail -f /dev/null
networks: []
volumes:
- your-app-mutagen-service1:/var/sync/service1:nocopy
- your-app-mutagen-service2:/var/sync/service2:nocopy
volumes:
your-app-mutagen-service1:
driver: local
your-app-mutagen-service2:
driver: local
#!/bin/bash -eu
CONTAINER_UID=2000
CONTAINER_USER=app
CONTAINER_GID=2000
CONTAINER_GROUP=app
export COMPOSE_FILE=docker-compose.yml:docker-compose-mutagen.yml
set-container-name-and-label() {
# Get *existing* container name from compose service name.
# https://github.com/docker/compose/issues/1262#issuecomment-489406386
local -r container_id=$(docker-compose ps -q mutagen-sync)
if [ -z "$container_id" ]; then
container_name=
label=
return
fi
container_name=$(docker inspect -f '{{.Name}}' $container_id | cut -c2-)
label="container_name=$container_name"
}
terminate-session() {
echo Terminating labeled sessions: label $label
mutagen terminate --label-selector $label
}
run-mutagen-create() {
# Put shared options here
mutagen create \
--label $label \
--sync-mode two-way-resolved \
--default-owner-beta id:$CONTAINER_UID \
--default-group-beta id:$CONTAINER_GID \
--ignore-vcs \
--ignore .DS_Store \
"$@"
}
# Ensure mutagen daemon running.
mutagen daemon start
if [ "${1:-}" = "clean" ]; then
set-container-name-and-label
if [ -n "$label" ]; then
terminate-session
else
echo 'Skipping terminating mutagen session because no sync container is found.'
echo 'You might want to cleanup sessions with "mutagen list" by hand.'
fi
docker-compose rm -f mutagen-sync
exit 0
fi
docker-compose rm -f mutagen-sync
docker-compose up -d mutagen-sync
set-container-name-and-label
if [ -z "$label" ]; then
echo Failed to start sync container.
exit 1
fi
# Create user if it does not exists.
docker-compose exec mutagen-sync \
/bin/sh -c "id -u $CONTAINER_USER > /dev/null 2>&1 || ( addgroup -g $CONTAINER_GID $CONTAINER_GROUP && adduser -D -u $CONTAINER_UID -G $CONTAINER_GROUP $CONTAINER_USER )"
terminate-session
# Put your services here
run-mutagen-create \
--ignore YOU_CAN_ALSO_SPECIFY_PER_SERVICE_OPTIONS_HERE \
./service1 docker://${container_name}/var/sync/service1
run-mutagen-create \
./service2 docker://${container_name}/var/sync/service2
# Shortcut to import additional compose file (like docker-sync-stack).
echo 'alias dcm="docker-compose -f docker-compose.yml -f docker-compose-mutagen.yml"' >> ~/.zshrc # or .bashrc, etc.
# Setup
./docker-mutagen-setup.sh # before first dcm up
# Clean
./docker-mutagen-setup.sh clean # before dcm down
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment