Skip to content

Commit

Permalink
Use health checks & multi-stage builds in docker compose
Browse files Browse the repository at this point in the history
* add base/dev/prod stage rather than Dockerfile vs Dockerfile.prod
* use docker compose health checks instead of wait-for
  • Loading branch information
aelkiss committed May 22, 2024
1 parent e668a21 commit d084064
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 172 deletions.
1 change: 0 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ jobs:
uses: hathitrust/github_actions/[email protected]
with:
image: ghcr.io/hathitrust/holdings-client-unstable
dockerfile: Dockerfile.prod
img_tag: ${{ inputs.img_tag }}
tag: ${{ inputs.ref }}
push_latest: ${{ inputs.push_latest}}
Expand Down
8 changes: 3 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ jobs:
run: ./bin/setup/setup_test.sh

- name: Run standardrb
run: docker compose run --rm dev bundle exec standardrb
run: docker compose run --rm test bundle exec standardrb

- name: Run tests
run: docker compose run --rm -e MONGOID_ENV=test dev bin/setup/wait-for mariadb:3306 pushgateway:9091 redis:6379 -- bundle exec rspec
run: docker compose run --rm test

- name: Report to Coveralls
uses: coverallsapp/[email protected]
with:
github-token: ${{ secrets.github_token }}
uses: coverallsapp/github-action@v2
26 changes: 21 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
FROM ruby:3.3
ARG UNAME=holdings
ARG UID=1000
ARG GID=1000
FROM ruby:3.3 AS base

RUN apt-get update -yqq && apt-get install -yqq --no-install-recommends \
nodejs netcat-openbsd rclone less entr uchardet
nodejs rclone uchardet

WORKDIR /usr/src/app
ENV BUNDLE_PATH /gems
ENV RUBYLIB /usr/src/app/lib
RUN gem install bundler

FROM base AS dev
RUN apt-get install -yqq --no-install-recommends less entr

FROM base AS prod
LABEL org.opencontainers.image.source https://github.com/hathitrust/holdings-backend

ARG UNAME=holdings
ARG UID=1000
ARG GID=1000

RUN groupadd -g $GID -o $UNAME
RUN useradd -m -d /usr/src/app -u $UID -g $GID -o -s /bin/bash $UNAME
RUN mkdir -p /gems && chown $UID:$GID /gems
USER $UNAME

COPY --chown=$UID:$GID Gemfile* /usr/src/app/
RUN bundle install
COPY --chown=$UID:$GID . /usr/src/app
22 changes: 0 additions & 22 deletions Dockerfile.prod

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ bash bin/setup/setup_dev.sh

## Running the tests

`docker-compose run --rm dev bundle exec rspec`
`docker-compose run --rm test`

## Clearing out/resetting the data
For resetting everything (cleaning up containers & their persistent volumes):
Expand Down
5 changes: 2 additions & 3 deletions bin/setup/setup_dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

$(dirname ${BASH_SOURCE[0]})/setup_test.sh

docker compose up -d mongo_dev pushgateway redis
docker compose run --rm dev bin/setup/wait-for mongo_dev:27017 -- echo "mongo is ready"
docker compose up --wait mongo_dev
docker compose exec -T mongo_dev bash /tmp/bin/setup/rs_initiate.sh mongo_dev
docker compose run --rm -e MONGOID_ENV=development dev bundle exec ruby lib/tasks/build_database.rb
docker compose run --rm dev bundle exec ruby lib/tasks/build_database.rb

5 changes: 2 additions & 3 deletions bin/setup/setup_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

docker compose build
docker compose run --rm dev bundle install
docker compose up -d mongo_test mariadb pushgateway redis
docker compose run --rm -e MONGOID_ENV=test dev bin/setup/wait-for mongo_test:27017 -- echo "mongo is ready"
docker compose up --wait mongo_test
docker compose exec -T mongo_test bash /tmp/bin/setup/rs_initiate.sh mongo_test
docker compose run --rm -e MONGOID_ENV=test dev bundle exec ruby lib/tasks/build_database.rb
docker compose run --rm -e test bundle exec ruby lib/tasks/build_database.rb
104 changes: 0 additions & 104 deletions bin/setup/wait-for

This file was deleted.

85 changes: 57 additions & 28 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,74 +1,100 @@
version: '3'
---

x-condition-healthy: &healthy
condition: service_healthy

x-healthcheck-defaults: &healthcheck-defaults
interval: 5s
timeout: 10s
start_period: 10s
retries: 5

x-holdings-container: &holdings-container-defaults
build:
context: .
target: dev
volumes:
- .:/usr/src/app
- gem_cache:/gems
command: bundle exec rspec
environment: &holdings-default-environment
MYSQL_CONNECTION_STRING: "mysql2://ht_repository:ht_repository@mariadb/ht_repository"
PUSHGATEWAY: http://pushgateway:9091
MONGOID_ENV: development
depends_on:
mariadb: *healthy
redis: *healthy
mongo_dev: *healthy

services:

dev:
build: .
volumes:
- .:/usr/src/app
- gem_cache:/gems
environment:
MYSQL_CONNECTION_STRING: "mysql2://ht_repository:ht_repository@mariadb/ht_repository"
PUSHGATEWAY: http://pushgateway:9091
test:
<<: *holdings-container-defaults
command: bundle exec rspec
environment:
<<: *holdings-default-environment
MONGOID_ENV: test
depends_on:
mongo_test: *healthy
mariadb: *healthy
redis: *healthy

phctl:
build: .
volumes:
- .:/usr/src/app
- gem_cache:/gems
environment:
MYSQL_CONNECTION_STRING: "mysql2://ht_repository:ht_repository@mariadb/ht_repository"
PUSHGATEWAY: http://pushgateway:9091
dev: *holdings-container-defaults

phctl:
<<: *holdings-container-defaults
entrypoint: bundle exec ruby bin/phctl.rb

processor:
build: .
<<: *holdings-container-defaults
restart: always
volumes:
- .:/usr/src/app
- gem_cache:/gems
- ./example/datasets:/tmp/datasets
command: bundle exec sidekiq -c 1 -r ./lib/sidekiq_jobs.rb
environment:
MYSQL_CONNECTION_STRING: "mysql2://ht_repository:ht_repository@mariadb/ht_repository"
PUSHGATEWAY: http://pushgateway:9091

mongo_dev:
image: mongo:6.0.2
command: --replSet rs0 --bind_ip localhost,mongo_dev
volumes:
- data_db:/data/db
- ./bin:/tmp/bin
healthcheck: &mongo-healthcheck
<<: *healthcheck-defaults
test: [ "CMD", "mongosh", "--quiet", "--eval", 'db.runCommand("ping").ok']

mongo_test:
image: mongo:6.0.2
command: --replSet rs0 --bind_ip localhost,mongo_test
volumes:
- ./bin:/tmp/bin
healthcheck: *mongo-healthcheck

mariadb:
image: mariadb
environment:
MYSQL_RANDOM_ROOT_PASSWORD: 1
volumes:
- ./sql:/docker-entrypoint-initdb.d/
healthcheck:
<<: *healthcheck-defaults
test: [ "CMD", "healthcheck.sh", "--su-mysql", "--connect", "--innodb_initialized" ]

pushgateway:
image: prom/pushgateway
command:
- --web.enable-admin-api
ports:
- 9091:9091
healthcheck:
<<: *healthcheck-defaults
test: [ "CMD", "wget", "--quiet", "--tries=1", "-O", "/dev/null", "pushgateway:9091/-/healthy" ]

sidekiq_web:
build: .
<<: *holdings-container-defaults
restart: always
volumes:
- .:/usr/src/app
- gem_cache:/gems
command: bundle exec puma bin/sidekiq_web.ru
depends_on:
- redis
redis: *healthy
ports:
- 9292:9292
environment:
Expand All @@ -77,6 +103,9 @@ services:
redis:
image: redis
restart: always
healthcheck:
<<: *healthcheck-defaults
test: ["CMD", "redis-cli","ping"]

volumes:
gem_cache:
Expand Down

0 comments on commit d084064

Please sign in to comment.