Skip to content

Commit 383b741

Browse files
committed
healthchecks! move to compose spec version!
1 parent 69c11f9 commit 383b741

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

docker-compose.yml

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
version: "3"
1+
# version is now using "compose spec"
2+
# v2 and v3 are now combined!
3+
# docker-compose v1.27+ required
24

35
services:
46
vote:
57
build: ./vote
8+
# use python rather than gunicorn for local dev
69
command: python app.py
10+
depends_on:
11+
redis:
12+
condition: service_healthy
713
volumes:
814
- ./vote:/app
915
ports:
@@ -14,7 +20,11 @@ services:
1420

1521
result:
1622
build: ./result
23+
# use nodemon rather than node for local dev
1724
command: nodemon server.js
25+
depends_on:
26+
db:
27+
condition: service_healthy
1828
volumes:
1929
- ./result:/app
2030
ports:
@@ -28,26 +38,35 @@ services:
2838
build:
2939
context: ./worker
3040
depends_on:
31-
- "redis"
32-
- "db"
41+
redis:
42+
condition: service_healthy
43+
db:
44+
condition: service_healthy
3345
networks:
3446
- back-tier
3547

3648
redis:
3749
image: redis:5.0-alpine3.10
38-
container_name: redis
50+
volumes:
51+
- "./healthchecks:/healthchecks"
52+
healthcheck:
53+
test: /healthchecks/redis.sh
54+
interval: "5s"
3955
ports: ["6379"]
4056
networks:
4157
- back-tier
4258

4359
db:
4460
image: postgres:9.4
45-
container_name: db
4661
environment:
4762
POSTGRES_USER: "postgres"
4863
POSTGRES_PASSWORD: "postgres"
4964
volumes:
5065
- "db-data:/var/lib/postgresql/data"
66+
- "./healthchecks:/healthchecks"
67+
healthcheck:
68+
test: /healthchecks/postgres.sh
69+
interval: "5s"
5170
networks:
5271
- back-tier
5372

healthchecks/postgres.sh

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
set -eo pipefail
3+
4+
host="$(hostname -i || echo '127.0.0.1')"
5+
user="${POSTGRES_USER:-postgres}"
6+
db="${POSTGRES_DB:-$POSTGRES_USER}"
7+
export PGPASSWORD="${POSTGRES_PASSWORD:-}"
8+
9+
args=(
10+
# force postgres to not use the local unix socket (test "external" connectibility)
11+
--host "$host"
12+
--username "$user"
13+
--dbname "$db"
14+
--quiet --no-align --tuples-only
15+
)
16+
17+
if select="$(echo 'SELECT 1' | psql "${args[@]}")" && [ "$select" = '1' ]; then
18+
exit 0
19+
fi
20+
21+
exit 1

healthchecks/redis.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
set -eo pipefail
3+
4+
host="$(hostname -i || echo '127.0.0.1')"
5+
6+
if ping="$(redis-cli -h "$host" ping)" && [ "$ping" = 'PONG' ]; then
7+
exit 0
8+
fi
9+
10+
exit 1

0 commit comments

Comments
 (0)