Skip to content

Instantly share code, notes, and snippets.

@zhashkevych
Last active April 27, 2024 03:37
Show Gist options
  • Save zhashkevych/2742682ab57b5670a15291864658625b to your computer and use it in GitHub Desktop.
Save zhashkevych/2742682ab57b5670a15291864658625b to your computer and use it in GitHub Desktop.
Wait for Postgres initialization in Docker-Compose
version: '3.8'
services:
todo-app:
build: ./
command: ./wait-for-postgres.sh db ./todo-app
ports:
- 8000:8000
depends_on:
- db
environment:
- DB_PASSWORD=qwerty
db:
restart: always
image: postgres:latest
volumes:
- ./.database/postgres/data:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=qwerty
ports:
- 5436:5432
FROM golang:1.14-buster
RUN go version
ENV GOPATH=/
COPY ./ ./
# install psql
RUN apt-get update
RUN apt-get -y install postgresql-client
# make wait-for-postgres.sh executable
RUN chmod +x wait-for-postgres.sh
# build go app
RUN go mod download
RUN go build -o todo-app ./cmd/main.go
CMD ["./todo-app"]
#!/bin/sh
# wait-for-postgres.sh
set -e
host="$1"
shift
cmd="$@"
until PGPASSWORD=$DB_PASSWORD psql -h "$host" -U "postgres" -c '\q'; do
>&2 echo "Postgres is unavailable - sleeping"
sleep 1
done
>&2 echo "Postgres is up - executing command"
exec $cmd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment