Skip to content

Instantly share code, notes, and snippets.

@avivrosenberg
Created July 24, 2016 07:34
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 avivrosenberg/99a11cf798b2c657aaffbc2edf0254e6 to your computer and use it in GitHub Desktop.
Save avivrosenberg/99a11cf798b2c657aaffbc2edf0254e6 to your computer and use it in GitHub Desktop.
Postgres-based image that doesn't run on Docker for mac beta
#!/bin/bash
set -e
####
## Settings:
DOCKER=${DOCKER:-"docker"} # How to run docker in this environment
CONTAINER_TEMP=test_db_bug # Name of a continer we'll create that will be used to load the new data into
CONTAINER_FINAL=test_db_bug_final # Name of a second container we'll create to copy the final data from the first one
IMAGE_FINAL=test-db-bug # Name of final image we'll create
# Cleanup pre-existing containers
preexisting_containers=($($DOCKER rm -fv $CONTAINER_TEMP $CONTAINER_FINAL 2>/dev/null || true))
if [[ ${#preexisting_containers[@]} -gt 0 ]]; then
echo "Removed pre-existing container(s): ${preexisting_containers[@]}"
fi
set -x
# Start a new container based on the official postgres 9.5.3. Use a non-volume data directory.
$DOCKER run -d -P \
-e "PGDATA=/var/lib/postgresql/test-data" \
--name $CONTAINER_TEMP \
postgres:9.5.3
# Wait for DB to initialize
sleep 10
# Gracefully shut down the database so everything is persisted to disk
$DOCKER exec -u postgres $CONTAINER_TEMP pg_ctl stop -m fast -w || true
# Create (but don't start) a new contianer based on an empty DB image.
# We modify PGDATA so that it's not a volume, since we're trying to create a pre-seeded DB image used for CI tests
# so we don't want our data to end up in a volume but in a regular directory saved within the image.
$DOCKER create -P \
-e "PGDATA=/var/lib/postgresql/test-data" \
--name $CONTAINER_FINAL \
postgres:9.5.3
# Copy DB data from the full container to the new one.
$DOCKER cp $CONTAINER_TEMP:/var/lib/postgresql/test-data - | $DOCKER cp - $CONTAINER_FINAL:/var/lib/postgresql
# Commit the new container as a new image.
$DOCKER commit $CONTAINER_FINAL $IMAGE_FINAL
# Remove DB container and it's data (it was saved to the new image)
$DOCKER rm -v $CONTAINER_TEMP $CONTAINER_FINAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment