mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-11-22 00:31:01 +00:00
Exit if dockerized postgres is already running (#86)
This creates the postgres container with a "postgres_<timestamp>" name. This lets us check for an existing, running, postgres container and exit early if it already exists. It's a little more clear/explicit than the "port 5432 already in use" error message that results from rerunning the script, and it addresses the gotcha in footnote 30
This commit is contained in:
parent
537f2e783e
commit
ac90bcbb79
1 changed files with 9 additions and 0 deletions
|
@ -14,12 +14,21 @@ DB_PORT="${POSTGRES_PORT:=5432}"
|
|||
# Allow to skip Docker if a dockerized Postgres database is already running
|
||||
if [[ -z "${SKIP_DOCKER}" ]]
|
||||
then
|
||||
# if a postgres container is running, print instructions to kill it and exit
|
||||
RUNNING_POSTGRES_CONTAINER=$(docker ps --filter 'name=postgres' --format '{{.ID}}')
|
||||
if [[ -n $RUNNING_POSTGRES_CONTAINER ]]; then
|
||||
echo >&2 "there is a postgres container already running, kill it with"
|
||||
echo >&2 " docker kill ${RUNNING_POSTGRES_CONTAINER}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Launch postgres using Docker
|
||||
docker run \
|
||||
-e POSTGRES_USER=${DB_USER} \
|
||||
-e POSTGRES_PASSWORD=${DB_PASSWORD} \
|
||||
-e POSTGRES_DB=${DB_NAME} \
|
||||
-p "${DB_PORT}":5432 \
|
||||
--name "postgres_$(date '+%s')" \
|
||||
-d postgres \
|
||||
postgres -N 1000
|
||||
# ^ Increased maximum number of connections for testing purposes
|
||||
|
|
Loading…
Reference in a new issue