Skip to content

Development with docker compose

Pawel edited this page May 15, 2021 · 5 revisions

If you don’t want to install node and/or the other tools needed for working on graphile-build, you can use docker-compose to easily set up a development environment

Setup

  1. Install Docker & docker-compose
  2. Create dir named db.
  3. In db dir create file named Dockerfile:
FROM postgres:13-alpine AS builder

RUN apk add --no-cache --virtual .build-deps gcc git make musl-dev pkgconf clang clang-dev llvm vim
RUN git clone https://github.com/eulerto/wal2json.git
WORKDIR wal2json
RUN USE_PGXS=1 make && make install

FROM postgres:13-alpine
COPY --from=builder /wal2json/wal2json.so /usr/local/lib/postgresql/
  1. In main directory put those two files at the repository root:
# docker-compose.yml
version: "3"
services:
  js: &js
    build: .
    volumes:
      - .:/gb:rw
    working_dir: /gb
    depends_on:
      - db
    environment:
      TEST_DATABASE_URL: postgres://gb@db:5432/gb
      TEST_PG_URL: postgres://gb@db:5432/postgraphile_test
      LDS_TEST_DATABASE_URL: postgres://gb@db:5432/lds_test
  yarn:
    <<: *js
    entrypoint: ["yarn"]
  lerna:
    <<: *js
    entrypoint: ["node_modules/.bin/lerna"]
  npm:
    <<: *js
    entrypoint: ["npm"]
  bash:
    <<: *js
    entrypoint: ["bash"]

  db:
    build: ./db
    command: postgres -c wal_level=logical -c max_wal_senders=10 -c max_replication_slots=10
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: gb
      POSTGRES_HOST_AUTH_METHOD: trust
      POSTGRES_PASSWORD: ""
# Dockerfile
FROM node:10

RUN apt-get update && apt-get install -y postgresql-client

To simplify the command I recommend adding this alias:

# ~/.bash_aliases
alias run="docker-compose run --rm"

Usage

When in the graphile-repository, you have access to all command defined in defined in docker-compose.yml like this:

docker-compose build
docker-compose run --rm yarn
docker-compose run --rm lerna bootstrap
docker-compose run --rm npm run watch

Watch will keep monitoring and compiling the babel files. Then to run the tests in another terminal:

All commands will be executed from the repository root regardless on the folder you’re in in the repository, if you want to change directory you can enter the container environment by running run bash and you then can change directory and run commands as you would without docker.

docker-compose run --rm lerna run test

docker-compose automatically configure and start a postgres server for you to run the tests.

Cleanup

To stop and remove any image and container created with docker-compose, just run docker-compose down

Clone this wiki locally