Skip to content

Commit c5551f2

Browse files
etagwerkerarielj
andauthored
[Feature][DX] Add Docker Configuration for Development (#109)
* Fixes #120 * Add docker configuration for development This makes it easier to run the test suite in your local dev environment * Add a development section for contributors This should make it easier for people to set up the library and run the test suite within Docker * Added ruby service to make it easier to get started with your contribution * Update password to match Docker configuration * Removed outdated docker-compose file * Use older mysql to avoid trilogy error, fix docker compose and instructions, mount code for easier development * Update readme to point to CONTRIBUTE.md file * Run ./bin/setup so that we have the config file in place for tests * Relax dependency * Add a line about the Docker + DX change --------- Co-authored-by: Ariel Juodziukynas <arieljuod@gmail.com>
1 parent 3748bab commit c5551f2

File tree

8 files changed

+107
-11
lines changed

8 files changed

+107
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Provide a 'Changelog' link on Rubygems: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/114
44
* Fix bundling and CONTRIBUTE.md instructions: https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/123
55
* Fix order of arguments in `truncate_tables` expectation https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/124
6+
* Add Docker to make it easier to run tests locally for maintainers and contributors https://github.com/DatabaseCleaner/database_cleaner-active_record/pull/109
67

78
## v2.2.1 2025-05-13
89

CONTRIBUTE.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,28 @@ upstream:
1212

1313
The gem uses Appraisal to configure different Gemfiles to test different Rails versions.
1414

15-
- You can run all the databases through docker if needed with `docker compose up` (you can also have them running on your system, just comment out the ones you don't need from the `docker-compose.yml` file)
16-
- Copy `spec/support/sample.config.yml` to `spec/support/config.yml` and edit it
15+
### Run tests without Docker (or using Docker only for the databases)
16+
17+
- You can run all the databases through docker if needed with `docker compose -f docker-compose.db.yml up` (you can also have them running on your system, just comment out the ones you don't need from the `docker-compose.db.yml` file)
18+
- Copy `spec/support/sample.config.yml` to `spec/support/config.yml` and edit it as needed
1719
- `BUNDLE_GEMFILE=gemfiles/rails_6.1.gemfile bundle install` (change `6.1` with any version from the `gemfiles` directory)
1820
- `BUNDLE_GEMFILE=gemfiles/rails_6.1.gemfile bundle exec rake`
1921

2022
Note that if you don't have all the supported databases installed and running,
2123
some tests will fail.
2224

23-
> Note that you can check the `.github/workflows/ci.yml` file for different combinations of Ruby and Rails that are expected to work
25+
> Check the `.github/workflows/ci.yml` file for different combinations of Ruby and Rails that are expected to work
26+
27+
### Run tests with Docker
28+
29+
- Open `docker-compose.yml` and configure the Ruby version and Gemfile file to use
30+
- Copy `spec/support/sample.docker.config.yml` to `spec/support/config.yml` (not this config file is specific for the Docker setup)
31+
- Run `docker compose up` to start the container, run the tests, and exit
32+
- Run `docker compose run ruby bash` to open `bash` inside the container for more control, run `rake` to run the tests
33+
34+
> Note that the code is mounted inside the docker container, so changes in the container will reflect in the code. There's no need to re-build the container for code changes, but changing the Ruby version or Gemfile in the docker-compose.yml will require a container re-build with `docker compose build --no-cache`
35+
36+
> Check the `.github/workflows/ci.yml` file for different combinations of Ruby and Rails that are expected to work
2437
2538
## 3. Prepare your contribution
2639

Dockerfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
ARG RUBY_VERSION=3.3
2+
FROM ruby:${RUBY_VERSION}
3+
4+
# Set the working directory in the container
5+
WORKDIR /app
6+
7+
# Copy the current directory contents into the container at /app
8+
# This is copied so we can bundle the application, but it's replaced
9+
# by a mounted volume with the current code when executed with docker compose
10+
COPY . /app
11+
12+
ARG BUNDLE_GEMFILE=Gemfile
13+
ENV BUNDLE_GEMFILE=${BUNDLE_GEMFILE}
14+
15+
# Install any needed packages specified in Gemfile
16+
RUN ./bin/setup
17+
18+
# Command to run the application
19+
CMD ["bash"]

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ test:
102102
min_messages: WARNING
103103
</pre>
104104

105+
## Development
106+
107+
Check the CONTRIBUTE.md file for instructions running tests with and withour Docker.
108+
105109
## COPYRIGHT
106110

107111
See [LICENSE](LICENSE) for details.

database_cleaner-active_record.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
1818
spec.executables = []
1919
spec.require_paths = ["lib"]
2020

21-
spec.add_dependency "database_cleaner-core", "~>2.1.0"
21+
spec.add_dependency "database_cleaner-core", "~>2.0"
2222
spec.add_dependency "activerecord", ">= 5.a"
2323

2424
spec.add_development_dependency "bundler"

docker-compose.db.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
services:
2+
postgres:
3+
image: postgres:16 # specify the version needed for a given app
4+
environment:
5+
- POSTGRES_PASSWORD=postgres # this is required
6+
ports:
7+
- "127.0.0.1:5432:5432" # so we can use `localhost` as the host
8+
mysql:
9+
image: mysql:5.7
10+
environment:
11+
- MYSQL_ROOT_PASSWORD=mysql
12+
ports:
13+
- "127.0.0.1:3306:3306"
14+
redis:
15+
image: redis:6.2-alpine
16+
restart: always
17+
ports:
18+
- "127.0.0.1:6379:6379"
19+
command: redis-server --save 20 1 --loglevel warning

docker-compose.yml

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@ services:
33
image: postgres:16 # specify the version needed for a given app
44
environment:
55
- POSTGRES_PASSWORD=postgres # this is required
6-
ports:
7-
- "127.0.0.1:5432:5432" # so we can use `localhost` as the host
86
mysql:
9-
image: mysql:9.3
7+
image: mysql:5.7
108
environment:
119
- MYSQL_ROOT_PASSWORD=mysql
12-
ports:
13-
- "127.0.0.1:3306:3306"
1410
redis:
1511
image: redis:6.2-alpine
1612
restart: always
17-
ports:
18-
- "127.0.0.1:6379:6379"
1913
command: redis-server --save 20 1 --loglevel warning
14+
ruby:
15+
build:
16+
context: .
17+
args:
18+
BUNDLE_GEMFILE: gemfiles/rails_7.2.gemfile # Manually change this based on the desired Rails version
19+
RUBY_VERSION: 3.3 # Manually change this based on the desired Ruby version
20+
volumes:
21+
- ".:/app:delegated"
22+
command: rake
23+
depends_on:
24+
- mysql
25+
- postgres
26+
- redis
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
mysql2:
2+
adapter: mysql2
3+
database: database_cleaner_test
4+
username: root
5+
password: mysql
6+
host: mysql
7+
port: 3306
8+
encoding: utf8
9+
10+
trilogy:
11+
adapter: trilogy
12+
database: database_cleaner_test
13+
username: root
14+
password: mysql
15+
host: mysql
16+
port: 3306
17+
encoding: utf8
18+
19+
postgres:
20+
adapter: postgresql
21+
database: database_cleaner_test
22+
username: postgres
23+
password: postgres
24+
host: postgres
25+
encoding: unicode
26+
template: template0
27+
28+
sqlite3:
29+
adapter: sqlite3
30+
database: tmp/database_cleaner_test.sqlite3
31+
pool: 5
32+
timeout: 5000
33+
encoding: utf8

0 commit comments

Comments
 (0)