Last active
April 27, 2024 03:37
-
-
Save zhashkevych/2742682ab57b5670a15291864658625b to your computer and use it in GitHub Desktop.
Wait for Postgres initialization in Docker-Compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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