Skip to content

Commit

Permalink
backend/docker-compose: add pg dependency, optional persistence
Browse files Browse the repository at this point in the history
This change adds a dependency to the (nebraska) "server" service on the
"postgres" service in docker-compose.test.yaml.
Motivation of the change is to prevent a race condition where the
"server" container starts faster than "postgres", fails to connect to
the database, and shuts down.

Additionally, an optional persistent storage for postgres is added.
DB contents can be persisted across docker-compose runs to aid manual,
interactive testing. Usage instructions have been added to the YAML
files as comments. The local directory used for persiting postgres data
has been added to .gitignore to prevent accidental commits.

Signed-off-by: Thilo Fromm <thilofromm@microsoft.com>
  • Loading branch information
t-lo committed Nov 25, 2022
1 parent 0fc8c4b commit 78ca8a4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
*.swp
*sublime*
backend/__postgres_testing_volume/
8 changes: 8 additions & 0 deletions backend/docker-compose.pg-persist.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Persistent postgres storage drop-in for interactive testing.
# This drop-in will persist postgres data in $(pwd)/__postgres_testing_volume
# across restarts of docker-compose.

services:
postgres:
volumes:
- ./__postgres_testing_volume:/var/lib/postgresql/data
22 changes: 19 additions & 3 deletions backend/docker-compose.test.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Docker-compose file for interactive and for automated testing.
#
# Build docker image from local sources:
# docker-compose --file docker-compose.test.yaml build
#
# Run:
# docker-compose --file docker-compose.test.yaml run
# Run with persistent storage:
# docker-compose --file docker-compose.test.yaml --file docker-compose.test-db-persist.yaml up
#
# See docker-compose.pg-persist.yaml for more informatino on persistent storage.

version: "3.9"

services:
Expand All @@ -21,11 +33,15 @@ services:
context: ../
dockerfile: Dockerfile
network: host
ports:
- "8002:8000"
# The "long form" depends_on silently fails in some scenarios (we've seen this with podman)
# so the short form is added for compatibility.
depends_on:
postgres:
- postgres
depends_on:
- postgres:
condition: service_healthy
ports:
- "8002:8000"
environment:
- NEBRASKA_DB_URL=postgres://postgres:nebraska@postgres:5432/nebraska_tests?sslmode=disable&connect_timeout=10
command: sh -c "/nebraska/nebraska --auth-mode=noop --http-static-dir=/nebraska/static --api-endpoint-suffix=/"

0 comments on commit 78ca8a4

Please sign in to comment.