Skip to content

Commit f721a4b

Browse files
authored
Merge pull request #64 from blocknotes/dev/docker-setup-3
Dev: update Docker setup (3)
2 parents 3440047 + bff9ac9 commit f721a4b

11 files changed

Lines changed: 152 additions & 85 deletions

File tree

.dockerignore

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1-
Makefile
2-
MIT-LICENSE
3-
README.md
1+
/Makefile
2+
/MIT-LICENSE
3+
/Gemfile.lock
4+
/extra/init.sh
5+
6+
/app
7+
/bin
8+
/config
9+
/coverage
10+
/db
11+
/gemfiles
12+
/spec/dummy/db/*.sqlite3
13+
/spec/dummy/log
14+
/spec/dummy/tmp
15+
/spec/dummy/**/*_spec.rb
16+
17+
/**/*.md

Makefile

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,29 @@
1-
include .env
1+
include extra/.env
22

33
help:
4-
@echo "Main targets: build / up / specs / console / shell"
4+
@echo -e "Usage (with DB one value in sqlite / mysql / mssql / postgres):\n DB=sqlite make up # to start the base service\n DB=sqlite make shell # to enter in a shell\nCheck the Makefile for other commands"
55

6-
# Docker commands
76
build:
87
@rm -f Gemfile.lock
9-
@docker compose build
10-
11-
down:
12-
@docker compose down
8+
@docker compose -f extra/docker-compose.yml build --build-arg DB_TEST=${DB} app_with_${DB}
139

1410
up: build
15-
@docker compose up
16-
17-
cleanup:
18-
docker compose rm -f
19-
docker image rm -f ${COMPOSE_PROJECT_NAME}-app
11+
@docker compose -f extra/docker-compose.yml up app_with_${DB}
2012

21-
# App commands
22-
server: build
23-
COMMAND="bin/rails s -b 0.0.0.0 -p 4000" docker compose up
13+
shell:
14+
@docker compose -f extra/docker-compose.yml exec app_with_${DB} bash
2415

25-
specs: build
26-
COMMAND="bin/rspec" docker compose up
16+
specs:
17+
@docker compose -f extra/docker-compose.yml exec app_with_${DB} bin/rspec --fail-fast
2718

28-
appraisal_update: build
29-
COMMAND="bin/appraisal update" docker compose up
19+
server:
20+
@docker compose -f extra/docker-compose.yml exec app_with_${DB} bin/rails s -b 0.0.0.0 -p ${SERVER_PORT}
3021

31-
lint: build
32-
COMMAND="bin/rubocop" docker compose up
22+
appraisal_update:
23+
@docker compose -f extra/docker-compose.yml exec app_with_${DB} bin/appraisal update
3324

34-
console:
35-
docker compose exec -e "PAGER=more" app bin/rails console
25+
lint:
26+
@docker compose -f extra/docker-compose.yml exec app_with_${DB} bin/rubocop
3627

37-
shell:
38-
docker compose exec -e "PAGER=more" app bash
28+
cleanup:
29+
@docker compose -f extra/docker-compose.yml down -v --rmi local --remove-orphans

bin/rails

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
#!/usr/bin/env ruby
2-
# This command will automatically be run when you run "rails" with Rails gems
3-
# installed from the root of your application.
42

5-
ENV['DB_TEST'] ||= 'postgres'
6-
ENV['RAILS_ENV'] ||= 'test'
3+
ENV['DB_TEST'] ||= 'sqlite'
4+
ENV['RAILS_ENV'] ||= 'development'
75

86
ENGINE_ROOT = File.expand_path('..', __dir__)
97
ENGINE_PATH = File.expand_path('../lib/active_storage_db/engine', __dir__)
108
APP_PATH = File.expand_path("../spec/dummy/config/application", __dir__)
119

12-
# Set up gems listed in the Gemfile.
1310
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
1411
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
1512

docker-compose.yml

Lines changed: 0 additions & 21 deletions
This file was deleted.

.env renamed to extra/.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ COMPOSE_PROJECT_NAME=active_storage_db
22

33
UID=1000
44
GID=1000
5+
6+
SERVER_PORT=4000

extra/Dockerfile

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
11
ARG RUBY_IMAGE=ruby:3
22
FROM ${RUBY_IMAGE}
33

4-
ARG DB_TEST=sqlite
5-
ARG RAILS_VERSION
6-
ARG UID
7-
8-
ENV DB_TEST=$DB_TEST
9-
ENV DEVEL=1
10-
ENV PROJECT_PATH=/app
11-
ENV RAILS_ENV=development
12-
ENV RAILS_VERSION=$RAILS_VERSION
13-
144
ENV DEBIAN_FRONTEND=noninteractive
5+
ENV DEVEL=1
156
ENV LANG=C.UTF-8
167

178
RUN apt-get update -qq
18-
RUN apt-get install -yqq --no-install-recommends build-essential nano pkg-config
9+
RUN apt-get install -yqq --no-install-recommends build-essential nano netcat-traditional pkg-config
1910
RUN apt-get install -yqq --no-install-recommends freetds-dev libmariadb-dev libpq-dev libvips42 libyaml-dev sqlite3
2011

2112
RUN gem install bundler -v 2.5.23
2213
RUN echo 'gem: --no-document' > /etc/gemrc
2314

15+
ARG UID
2416
RUN useradd -u $UID --shell /bin/bash app
25-
RUN mkdir /app_deps && mkdir -p /home/app
17+
18+
RUN mkdir -p /home/app
2619
RUN chown -R app:app /home/app
2720
RUN chown -R app /usr/local/bundle
2821

22+
ARG RAILS_VERSION
23+
ENV RAILS_VERSION=$RAILS_VERSION
24+
2925
WORKDIR /app
3026
COPY . /app
3127
RUN bundle install
32-
RUN bin/rails db:create db:migrate db:test:prepare
3328
RUN chown -R app:app /app/spec/dummy/db
34-
35-
ENTRYPOINT ["extra/entrypoint.sh"]

extra/development.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@ There 3 ways to interact with this project:
55
1) Using Docker:
66

77
```sh
8-
# Run build the dummy app image/container:
9-
make build
10-
# Run rails server on the dummy app:
8+
# Set the test DB driver (mysql / mssql / postgres / sqlite)
9+
export DB=sqlite
10+
# Start the base service (in the test dummy app):
11+
make up
12+
# With the base service started, start the Rails server (default port 4000):
1113
make server
12-
# Enter in a Rails console (with the dummy app started):
13-
make console
14-
# Enter in a shell (with the dummy app started):
14+
# With the base service started, enter in a shell:
1515
make shell
1616

17-
# Run the test suite:
17+
# With the base service started, run the test suite:
1818
make specs
19-
# Run the linter on the project:
19+
# With the base service started, run the linter on the project:
2020
make lint
2121

2222
# Remove container and image:
2323
make cleanup
2424
# For more commands please check the Makefile
2525

26-
# To use a different Ruby version:
27-
RUBY=3.0 make build
26+
# To use a different db, Ruby and Rails:
27+
DB=postgres RUBY=3.4 RAILS=7.2 make up
2828
```
2929

3030
2) Using Appraisal:

extra/docker-compose.yml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
services:
2+
app: &app
3+
build:
4+
context: ..
5+
dockerfile: extra/Dockerfile
6+
args:
7+
# Debian-based Ruby image:
8+
RUBY_IMAGE: ruby:${RUBY:-3.2}-slim
9+
RAILS_VERSION: ${RAILS:-}
10+
UID: ${UID}
11+
user: ${UID}:${GID}
12+
ports:
13+
- ${SERVER_PORT}:${SERVER_PORT}
14+
working_dir: /app
15+
volumes:
16+
- ..:/app
17+
stdin_open: true
18+
tty: true
19+
command: ${COMMAND:-extra/init.sh}
20+
21+
mssql:
22+
image: mcr.microsoft.com/mssql/server:2022-latest
23+
platform: linux/amd64
24+
environment:
25+
ACCEPT_EULA: Y
26+
SA_PASSWORD: Pa%%w0rd
27+
28+
mysql:
29+
image: mysql
30+
platform: linux/amd64
31+
environment:
32+
MYSQL_ROOT_PASSWORD: password
33+
34+
postgres:
35+
image: postgres
36+
environment:
37+
POSTGRES_PASSWORD: password
38+
39+
app_with_mysql:
40+
<<: *app
41+
environment:
42+
DB_TEST: mysql
43+
DB_PORT: 3306
44+
MYSQL_DB_HOST: mysql
45+
MYSQL_DB_NAME: test_db
46+
MYSQL_DB_USERNAME: root
47+
MYSQL_DB_PASSWORD: password
48+
depends_on:
49+
- mysql
50+
51+
app_with_mssql:
52+
<<: *app
53+
environment:
54+
DB_TEST: mssql
55+
DB_PORT: 1433
56+
MSSQL_DB_HOST: mssql
57+
MSSQL_DB_NAME: test_db
58+
MSSQL_DB_USERNAME: sa
59+
MSSQL_DB_PASSWORD: Pa%%w0rd
60+
depends_on:
61+
- mssql
62+
63+
app_with_postgres:
64+
<<: *app
65+
environment:
66+
DB_TEST: postgres
67+
DB_PORT: 5432
68+
PG_DB_HOST: postgres
69+
PG_DB_NAME: test_db
70+
PG_DB_USERNAME: postgres
71+
PG_DB_PASSWORD: password
72+
depends_on:
73+
- postgres
74+
75+
app_with_sqlite:
76+
<<: *app
77+
environment:
78+
DB_TEST: sqlite
79+
volumes:
80+
- ..:/app
81+
- /app/spec/dummy/db

extra/entrypoint.sh

Lines changed: 0 additions & 4 deletions
This file was deleted.

extra/init.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
3+
# Wait the DB
4+
if [ "$DB_TEST" != "sqlite" ]; then
5+
while ! nc -z $DB_TEST $DB_PORT </dev/null
6+
do echo "Waiting for DB ($DB_TEST)..." && sleep 3; done
7+
echo "DB is now available!"
8+
fi
9+
10+
# Setup database
11+
bin/rails db:reset db:test:prepare
12+
13+
#
14+
sh

0 commit comments

Comments
 (0)