Skip to content

Instantly share code, notes, and snippets.

@alister
Created January 27, 2015 21:46
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save alister/ed664cb51c21bc801e9a to your computer and use it in GitHub Desktop.
Save alister/ed664cb51c21bc801e9a to your computer and use it in GitHub Desktop.
Example of creating a Docker data volume, exporting it, and re-importing it to a completely fresh container
# Create the container - we run 'echo' in the container, and reuse the same as we will be running later
docker run -d -v /data --name redis.data dockerfile/redis echo Data-only container for Redis
# the data container doesn't show up - nothing is running
docker ps
# will show 'redis.data', but as stopped
docker ps -a
# start redis, attach to 'redis.data'
docker run -d -p 6379:6379 --volumes-from redis.data --name ca.redis.1 dockerfile/redis
docker ps # shows as running
redis-cli ping
#> PONG
redis-cli set alister hello
#> OK
redis-cli get alister
#> "hello"
redis-cli save
> OK
docker stop ca.redis.1
# Now, we destroyed the running Redis
########################
# Pull the data out of the data-container
# -rm: remove the container when it exits
# --volumes-from DATA: attach to the volumes shared by the DATA container
# -v $(pwd):/backup: bind mount the current directory into the container; to write the tar file to
# busybox: a small simpler image - good for quick maintenance
# tar cvf /backup/ca.redis.dump.tar /data: creates an uncompressed tar file of all the files in the /data directory
docker run --rm --volumes-from redis.data -v $(pwd):/backup busybox tar cvf /backup/ca.redis.dump.tar /data
# kill all the containers we have - we will rebuild from 'ca.redis.dump.tar'
docker ps -a
# get the container IDs that make up redis.data & ca.redis.1
#docker stop ....
#docker rm ....
# now the list shoudl be empty (of redis-related containers)
docker ps -a
###
# OK, so we are back at nothing, but we have a ca.redis.dump.tar file with a /data/dump.rdb file in it.
# Recreate the container - we only run 'echo' in the container, and reuse the same as we will be running
docker run -d -v /data --name ca.redis-dumps dockerfile/redis echo "Data-only container for Redis"
# data should not be there yet
docker run --rm --volumes-from ca.redis-dumps busybox ls -l /data
# put the data into the container from the backup
docker run --rm --volumes-from ca.redis-dumps -v $(pwd):/backup busybox tar xvf /backup/ca.redis.dump.tar
# verify it is there now
docker run --rm --volumes-from ca.redis-dumps busybox ls -l /data
# should show data/ & data/dump.rdb
# Now we run the redis container, with access to the /data/ directory
docker run -d -p 6379:6379 --volumes-from ca.redis-dumps --name ca.redis.8 dockerfile/redis
# and we see what is now in redis
redis-cli KEYS \*
#> "alister"
redis-cli get alister
#> "hello"
# It worked. We created/saved the contents of a data container, and then brought it back, via a tarball.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment