Skip to content

Instantly share code, notes, and snippets.

@dajester2013
Created September 21, 2018 17:19
Show Gist options
  • Save dajester2013/2f78da2b440eb24a89a0086ca73c1695 to your computer and use it in GitHub Desktop.
Save dajester2013/2f78da2b440eb24a89a0086ca73c1695 to your computer and use it in GitHub Desktop.
Creates a docker image from a data container
#!/bin/bash
usage()
{
echo "usage: ./publish-data-container.sh -c <Data Container> -i <Image name to create/tag> -f <Folder path to backup> -b <Base image to use for backup container - defaults to alpine:latest>"
}
while getopts c:i:f:b: option
do
case "${option}"
in
c) CONTAINER=$OPTARG;;
i) IMAGENAME=$OPTARG;;
f) FOLDERTOBAK=$OPTARG;;
b) BAKIMG=$OPTARG;;
esac
done
if [ -z "$CONTAINER" ] || [ -z "$IMAGENAME" ] || [ -z "$FOLDERTOBAK" ] ; then
usage
exit
fi
if [ -z "$BAKIMG" ] ; then
BAKIMG="alpine:latest"
fi
# Create a temp directory and cd into it
ORIGDIR=$(pwd)
cd "$(mktemp -d)"
TMPDIR=$(pwd)
echo "Creating image '${IMAGENAME}' from data container '${CONTAINER}'..."
TMPCONTAINER="$CONTAINER-bak"
sudo docker run -it --volumes-from $CONTAINER --name $TMPCONTAINER $BAKIMG /bin/sh -c "cd / && tar cvf /bak.tar.gz $FOLDERTOBAK"
sudo docker cp $TMPCONTAINER:/bak.tar.gz $(pwd)/bak.tar.gz
sudo docker rm $TMPCONTAINER -f
cat > $(pwd)/Dockerfile << dockerfile
FROM $BAKIMG
ADD bak.tar.gz /
dockerfile
sudo docker image build -t $IMAGENAME .
sudo docker volume prune
cd $ORIGDIR
echo "Cleaning up files..."
rm -rf $TMPDIR
@dajester2013
Copy link
Author

@martasd
Copy link

martasd commented Dec 20, 2018

Hi. I am curious: How can I then use the image to use (restore) the backed-up volume in a new container? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment