Skip to content

Commit

Permalink
Docker compose improvements
Browse files Browse the repository at this point in the history
* use volumes to persist logs and storage (and possibly db)
* expose Rails' master key as a docker compose secret
* give PostgreSQL and MySQL separate persistence paths
  • Loading branch information
rubys committed Jun 30, 2024
1 parent 177e079 commit 907b303
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 46 deletions.
13 changes: 13 additions & 0 deletions lib/generators/dockerfile_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,19 @@ def more_docker_ignores
more
end

def compose_web_volumes
volumes = %w[ log storage ]

if deploy_database == "sqlite3"
database = YAML.load_file("config/database.yml", aliases: true).dig("production", "database")
if database && database =~ /^\w/
volumes << File.dirname(database)
end
end

volumes.uniq.sort
end

def max_idle
option = options["max-idle"]

Expand Down
23 changes: 14 additions & 9 deletions lib/generators/templates/docker-compose.yml.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
version: "3.8"

services:
web:
<% if all_args.empty? -%>
Expand All @@ -18,11 +19,14 @@ services:
secrets:
- gemserver_credentials
<% end -%>
<% end -%>
volumes:
<% compose_web_volumes.each do |path| -%>
- ./<%= path %>:/rails/<%= path %>
<% end -%>
ports:
- "3000:3000"
environment:
- RAILS_MASTER_KEY=$RAILS_MASTER_KEY
<% if using_redis? -%>
- REDIS_URL=redis://redis-db:6379
<% end -%>
Expand All @@ -35,10 +39,9 @@ services:
- DATABASE_URL=mysql2://root:password@mysql-db/
<% end -%>
<% end -%>
<% if deploy_database == 'sqlite3' -%>
volumes:
- ./db:/rails/db
<% end -%>
secrets:
- source: master_key
target: /rails/config/master.key
<% if using_redis? or deploy_database != 'sqlite3' -%>
depends_on:
<% if using_redis? -%>
Expand All @@ -60,9 +63,7 @@ services:
POSTGRES_USER: root
POSTGRES_PASSWORD: password
volumes:
- ./tmp/db:/var/lib/postgresql/data
ports:
- "5432:5432"
- ./tmp/postgres-db:/var/lib/postgresql/data
healthcheck:
test: pg_isready
interval: 2s
Expand All @@ -77,7 +78,7 @@ services:
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
- ./tmp/db:/var/lib/mysql
- ./tmp/mysql-db:/var/lib/mysql
healthcheck:
test: mysqladmin ping -h 127.0.0.1 -u root --password=password
interval: 2s
Expand Down Expand Up @@ -132,3 +133,7 @@ secrets:
gemserver_credentials:
file: ./GEMSERVER_CREDENTIALS.secret.txt
<% end -%>

secrets:
master_key:
file: ./config/master.key
14 changes: 11 additions & 3 deletions test/results/api/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
version: "3.8"

services:
web:
build:
context: .
args:
UID: ${UID:-1000}
GID: ${GID:-${UID:-1000}}
volumes:
- ./log:/rails/log
- ./storage:/rails/storage
ports:
- "3000:3000"
environment:
- RAILS_MASTER_KEY=$RAILS_MASTER_KEY
volumes:
- ./db:/rails/db
secrets:
- source: master_key
target: /rails/config/master.key

secrets:
master_key:
file: ./config/master.key
14 changes: 11 additions & 3 deletions test/results/bun/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
version: "3.8"

services:
web:
build:
context: .
args:
UID: ${UID:-1000}
GID: ${GID:-${UID:-1000}}
volumes:
- ./log:/rails/log
- ./storage:/rails/storage
ports:
- "3000:3000"
environment:
- RAILS_MASTER_KEY=$RAILS_MASTER_KEY
volumes:
- ./db:/rails/db
secrets:
- source: master_key
target: /rails/config/master.key

secrets:
master_key:
file: ./config/master.key
14 changes: 11 additions & 3 deletions test/results/esbuild/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
version: "3.8"

services:
web:
build:
context: .
args:
UID: ${UID:-1000}
GID: ${GID:-${UID:-1000}}
volumes:
- ./log:/rails/log
- ./storage:/rails/storage
ports:
- "3000:3000"
environment:
- RAILS_MASTER_KEY=$RAILS_MASTER_KEY
volumes:
- ./db:/rails/db
secrets:
- source: master_key
target: /rails/config/master.key

secrets:
master_key:
file: ./config/master.key
14 changes: 12 additions & 2 deletions test/results/mysql/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
version: "3.8"

services:
web:
build:
context: .
args:
UID: ${UID:-1000}
GID: ${GID:-${UID:-1000}}
volumes:
- ./log:/rails/log
- ./storage:/rails/storage
ports:
- "3000:3000"
environment:
- RAILS_MASTER_KEY=$RAILS_MASTER_KEY
- DATABASE_URL=mysql2://root:password@mysql-db/
secrets:
- source: master_key
target: /rails/config/master.key
depends_on:
mysql-db:
condition: service_healthy
Expand All @@ -22,9 +28,13 @@ services:
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
- ./tmp/db:/var/lib/mysql
- ./tmp/mysql-db:/var/lib/mysql
healthcheck:
test: mysqladmin ping -h 127.0.0.1 -u root --password=password
interval: 2s
timeout: 5s
retries: 30

secrets:
master_key:
file: ./config/master.key
16 changes: 12 additions & 4 deletions test/results/postgresql/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
version: "3.8"

services:
web:
build:
context: .
args:
UID: ${UID:-1000}
GID: ${GID:-${UID:-1000}}
volumes:
- ./log:/rails/log
- ./storage:/rails/storage
ports:
- "3000:3000"
environment:
- RAILS_MASTER_KEY=$RAILS_MASTER_KEY
- DATABASE_URL=postgres://root:password@postgres-db/
secrets:
- source: master_key
target: /rails/config/master.key
depends_on:
postgres-db:
condition: service_healthy
Expand All @@ -21,11 +27,13 @@ services:
POSTGRES_USER: root
POSTGRES_PASSWORD: password
volumes:
- ./tmp/db:/var/lib/postgresql/data
ports:
- "5432:5432"
- ./tmp/postgres-db:/var/lib/postgresql/data
healthcheck:
test: pg_isready
interval: 2s
timeout: 5s
retries: 30

secrets:
master_key:
file: ./config/master.key
14 changes: 11 additions & 3 deletions test/results/private_gemserver/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
version: "3.8"

services:
web:
build:
Expand All @@ -8,13 +9,20 @@ services:
GID: ${GID:-${UID:-1000}}
secrets:
- gemserver_credentials
volumes:
- ./log:/rails/log
- ./storage:/rails/storage
ports:
- "3000:3000"
environment:
- RAILS_MASTER_KEY=$RAILS_MASTER_KEY
volumes:
- ./db:/rails/db
secrets:
- source: master_key
target: /rails/config/master.key

secrets:
gemserver_credentials:
file: ./GEMSERVER_CREDENTIALS.secret.txt

secrets:
master_key:
file: ./config/master.key
14 changes: 11 additions & 3 deletions test/results/private_gemserver_with_cache/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
version: "3.8"

services:
web:
build:
Expand All @@ -8,13 +9,20 @@ services:
GID: ${GID:-${UID:-1000}}
secrets:
- gemserver_credentials
volumes:
- ./log:/rails/log
- ./storage:/rails/storage
ports:
- "3000:3000"
environment:
- RAILS_MASTER_KEY=$RAILS_MASTER_KEY
volumes:
- ./db:/rails/db
secrets:
- source: master_key
target: /rails/config/master.key

secrets:
gemserver_credentials:
file: ./GEMSERVER_CREDENTIALS.secret.txt

secrets:
master_key:
file: ./config/master.key
14 changes: 11 additions & 3 deletions test/results/redis/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
version: "3.8"

services:
web:
build:
context: .
args:
UID: ${UID:-1000}
GID: ${GID:-${UID:-1000}}
volumes:
- ./log:/rails/log
- ./storage:/rails/storage
ports:
- "3000:3000"
environment:
- RAILS_MASTER_KEY=$RAILS_MASTER_KEY
- REDIS_URL=redis://redis-db:6379
volumes:
- ./db:/rails/db
secrets:
- source: master_key
target: /rails/config/master.key
depends_on:
redis-db:
condition: service_started

redis-db:
image: redis

secrets:
master_key:
file: ./config/master.key
16 changes: 12 additions & 4 deletions test/results/sidekiq/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
version: "3.8"

services:
web:
build:
context: .
args:
UID: ${UID:-1000}
GID: ${GID:-${UID:-1000}}
volumes:
- ./log:/rails/log
- ./storage:/rails/storage
ports:
- "3000:3000"
environment:
- RAILS_MASTER_KEY=$RAILS_MASTER_KEY
- REDIS_URL=redis://redis-db:6379
- DATABASE_URL=postgres://root:password@postgres-db/
secrets:
- source: master_key
target: /rails/config/master.key
depends_on:
redis-db:
condition: service_started
Expand All @@ -24,9 +30,7 @@ services:
POSTGRES_USER: root
POSTGRES_PASSWORD: password
volumes:
- ./tmp/db:/var/lib/postgresql/data
ports:
- "5432:5432"
- ./tmp/postgres-db:/var/lib/postgresql/data
healthcheck:
test: pg_isready
interval: 2s
Expand All @@ -48,3 +52,7 @@ services:
condition: service_started
postgres-db:
condition: service_healthy

secrets:
master_key:
file: ./config/master.key
Loading

0 comments on commit 907b303

Please sign in to comment.