Skip to content

Instantly share code, notes, and snippets.

@josmardias
Last active August 29, 2015 14:22
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 josmardias/43dc5af52351367e36e5 to your computer and use it in GitHub Desktop.
Save josmardias/43dc5af52351367e36e5 to your computer and use it in GitHub Desktop.
create postgres docker container
#!/usr/bin/env bash
VOLUME_NAME="test-postgres-data"
CONTAINER_NAME="test-postgres9.4"
PGPASSWORD="password"
DATABASE="test"
(
echo -e "\ncreating persistent volume...\n";
docker create -v /var/lib/postgresql/data --name $VOLUME_NAME busybox
) &&
(
echo -e "\ncreating named container...\n";
docker run --name $CONTAINER_NAME -p 5432:5432 -e POSTGRES_PASSWORD=$PGPASSWORD -d --volumes-from $VOLUME_NAME docker.io/postgres
) &&
(
echo -e "\ncreating database...\n"
docker run -e "PGPASSWORD=$PGPASSWORD" -e "DATABASE=$DATABASE" -it --link $CONTAINER_NAME:postgres --rm docker.io/postgres sh -c 'exec psql -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U postgres --command "CREATE DATABASE $DATABASE;"'
) &&
echo "container $CONTAINER_NAME created and running"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment