Skip to content

Instantly share code, notes, and snippets.

@kballenegger
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kballenegger/e76f10cc1e4ab9dc833e to your computer and use it in GitHub Desktop.
Save kballenegger/e76f10cc1e4ab9dc833e to your computer and use it in GitHub Desktop.
Commit (image) a Docker data container.
#!/bin/bash
# this script does not work on OS X
[[ "$(uname -s)" == Darwin ]] && echo "This script cannot run under OS X" && exit 1
container_name=treasury_dbdata_1
container_id="$(docker inspect treasury_dbdata_1 | jq -r '.[0].Id')"
volume_path=/var/lib/mysql
tag=data
set -e
# 1st, make sure all containers that depend on the data container are stopped
docker inspect $(docker ps -q) | jq -r "$(cat <<JQ
. | map ( select (
1 == (
.HostConfig.VolumesFrom | arrays
| map(
select(
. == "$container_id"
)
) | length
)
)) | .[].Id
JQ
)" | xargs docker stop
# 2nd, find the local path for the volume
local_volume_path="$(docker inspect "$container_id" |
jq -r '.[0].Volumes."'"$volume_path"'"')"
# 3rd, create a fake context for image building:
context_dir=$(mktemp -d)
trap "rm -rf $context_dir" EXIT
# - hardlink volume
cp -al "$local_volume_path" $context_dir/data
# - write dockerfile
>$context_dir/Dockerfile <<DOCKERFILE
FROM mysql:latest
COPY data/ $volume_path
DOCKERFILE
# finally, the moment of truth. we actually build the image
docker build -t $tag $context_dir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment