dockerfile-rails 0.0.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e00977164abc39c9b3b66c636834cda09f32f939045db5a5f496b51a455ee4f
4
- data.tar.gz: bc3305fa419473f18eace29a65a9461fbd90c423106ff3cf1c5c42d0ed3b58ba
3
+ metadata.gz: b317d73db1dda4043a0cf7ad8bc237cc909cf6fe08550960e57190651fe15f5a
4
+ data.tar.gz: 15454b9d5fee80e253dff57fe4f708f9e2c0bc729a1c1b506e2e96a7f1c13666
5
5
  SHA512:
6
- metadata.gz: d92c231f9d0484dcc196837aab9a3f3c3b6158202d5c8d942a61231ec9d5ec3fa0af8cf54a8daffea5d6f1103241aa5bb88a79cb9ea0a29974a08b579f7b2356
7
- data.tar.gz: 1e65aec6411ddac4d9785f087f16c2b8645e0895701fe6da58d93369d45d2aa8b71c4cb48c777e64737cf805a08ca4fc4da66a44d0ab007fa36f2351065ffc95
6
+ metadata.gz: ff075f1ab57a0a7bf5a6f084bc8c552f5a9a4b2e16a9452fec6d07b5f245dd7e8cc56e74d447af3083906926747fb868e0a76a99da8f0cba20c61273fd7f7010
7
+ data.tar.gz: c6306db9e351812343158cad19c59da387889f1fb449006e73806329d8b375d3ff8534e9f4c358c9b34368f1ea9e98d3e0b23d5833067477ccf6a68fe744bf9b
data/README.md CHANGED
@@ -5,16 +5,17 @@ Provide Rails generators to produce Dockerfiles and related files.
5
5
  ## Usage
6
6
 
7
7
  ```
8
- bundle add dockerfile-rails
8
+ bundle add dockerfile-rails --group development
9
9
  bin/rails generate dockerfile
10
10
  ```
11
11
 
12
12
  General options:
13
13
 
14
14
  * `--force` - overwrite existing files
15
- * `--ci` - include test gems in bundle
15
+ * `--ci` - include test gems in deployed image
16
16
  * `--cache` - use build caching to speed up builds
17
17
  * `--parallel` - use multi-stage builds to install gems and node modules in parallel
18
+ * `--compose` - generate a `docker-compose.yml` file
18
19
 
19
20
  Dependencies:
20
21
 
@@ -25,4 +26,10 @@ additional support may be needed:
25
26
  * `--mysql` - add mysql libraries
26
27
  * `--posgresql` - add posgresql libraries
27
28
  * `--redis` - add redis libraries
28
- * `--sqlite3` - add sqlite3 libraries
29
+ * `--sqlite3` - add sqlite3 libraries
30
+
31
+ Links:
32
+
33
+ * [Demos](./DEMO.md)
34
+ * [Preparations for Rails 7.1](https://community.fly.io/t/preparations-for-rails-7-1/9512)
35
+ * [Rails Dockerfile futures](https://discuss.rubyonrails.org/t/rails-dockerfile-futures/82091/1)
@@ -18,6 +18,7 @@ module DockerfileRails
18
18
  ### ruby gems ###
19
19
 
20
20
  @gemfile = []
21
+ @git = false
21
22
 
22
23
  if File.exist? 'Gemfile.lock'
23
24
  parser = Bundler::LockfileParser.new(Bundler.read_file('Gemfile.lock'))
@@ -25,7 +26,9 @@ module DockerfileRails
25
26
  end
26
27
 
27
28
  if File.exist? 'Gemfile'
28
- @gemfile += Bundler::Definition.build('Gemfile', nil, []).dependencies.map(&:name)
29
+ gemfile_definition = Bundler::Definition.build('Gemfile', nil, [])
30
+ @gemfile += gemfile_definition.dependencies.map(&:name)
31
+ @git = !gemfile_definition.spec_git_paths.empty?
29
32
  end
30
33
 
31
34
  @sidekiq = @gemfile.include? 'sidekiq'
@@ -10,6 +10,9 @@ class DockerfileGenerator < Rails::Generators::Base
10
10
  class_option :parallel, type: :boolean, default: false,
11
11
  desc: 'use build stages to install gems and node modules in parallel'
12
12
 
13
+ class_option :compose, type: :boolean, default: false,
14
+ desc: 'generate a docker-compose.yml file'
15
+
13
16
  class_option :redit, type: :boolean, default: false,
14
17
  desc: 'include redis libraries'
15
18
 
@@ -34,6 +37,8 @@ class DockerfileGenerator < Rails::Generators::Base
34
37
 
35
38
  template 'docker-entrypoint.erb', 'bin/docker-entrypoint'
36
39
  chmod "bin/docker-entrypoint", 0755 & ~File.umask, verbose: false
40
+
41
+ template 'docker-compose.yml.erb', 'docker-compose.yml'
37
42
  end
38
43
 
39
44
  private
@@ -43,6 +48,10 @@ private
43
48
  @using_node = File.exist? 'package.json'
44
49
  end
45
50
 
51
+ def using_redis?
52
+ options.redis? or @redis
53
+ end
54
+
46
55
  def parallel?
47
56
  using_node? && options.parallel
48
57
  end
@@ -61,8 +70,11 @@ private
61
70
  packages << 'libpq-dev' if options.postgresql? or @postgresql
62
71
  packages << 'default-libmysqlclient-dev' if options.mysql or @mysql
63
72
 
73
+ # add git if needed to install gems
74
+ packages << 'git' if @git
75
+
64
76
  # add redis in case Action Cable, caching, or sidekiq are added later
65
- packages << "redis" if options.redis? or @redis
77
+ packages << "redis" if using_redis?
66
78
 
67
79
  # ActiveStorage preview support
68
80
  packages << "libvips" if @gemfile.include? 'ruby-vips'
@@ -103,7 +115,7 @@ private
103
115
  packages << 'default-mysql-client' if options.mysql or @mysql
104
116
 
105
117
  # add redis in case Action Cable, caching, or sidekiq are added later
106
- packages << "redis" if options.redis? or @redis
118
+ packages << "redis" if using_redis?
107
119
 
108
120
  # ActiveStorage preview support
109
121
  packages << "libvips" if @gemfile.include? 'ruby-vips'
@@ -140,14 +152,24 @@ private
140
152
  binfixups
141
153
  end
142
154
 
155
+ def deploy_database
156
+ if options.postgresql? or @postgresql
157
+ 'postgresql'
158
+ elsif options.mysql or @mysql
159
+ 'mysql'
160
+ else
161
+ 'sqlite3'
162
+ end
163
+ end
164
+
143
165
  def node_version
144
- using_node? and `node --version`[/\d+\.\d+\.\d+/]
166
+ `node --version`[/\d+\.\d+\.\d+/]
145
167
  rescue
146
168
  "lts"
147
169
  end
148
170
 
149
171
  def yarn_version
150
- using_node? and `yarn --version`[/\d+\.\d+\.\d+/]
172
+ `yarn --version`[/\d+\.\d+\.\d+/]
151
173
  rescue
152
174
  "latest"
153
175
  end
@@ -160,6 +182,23 @@ private
160
182
  Rails.application.config.api_only
161
183
  end
162
184
 
185
+ def api_client_dir
186
+ return unless api_only?
187
+
188
+ file = Dir['*/package.json'].find do |file|
189
+ JSON.load_file(file).dig('scripts', 'build')
190
+ end
191
+
192
+ file && File.dirname(file)
193
+ end
194
+
195
+ def api_client_files
196
+ client = api_client_dir
197
+ return unless client
198
+
199
+ Dir["#{client}/{package.json,package-lock.json,yarn.lock}"]
200
+ end
201
+
163
202
  def dbprep_command
164
203
  if Rails::VERSION::MAJOR >= 6
165
204
  'db:prepare'
@@ -1,7 +1,40 @@
1
1
  # syntax = docker/dockerfile:1
2
2
 
3
- # Make sure it matches the Ruby version in .ruby-version and Gemfile
3
+ # Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
4
4
  ARG RUBY_VERSION=<%= Gem.ruby_version %>
5
+ <% if api_client_dir -%>
6
+ ARG NODE_VERSION=<%= node_version %>
7
+
8
+ FROM node:$NODE_VERSION-slim as client
9
+
10
+ WORKDIR /rails/<%= api_client_dir %>
11
+
12
+ ENV NODE_ENV=production
13
+
14
+ # Install node modules
15
+ COPY <%= api_client_files.join(' ') %> .
16
+ <% if api_client_files.join.include? 'yarn' -%>
17
+ <% if options.cache? -%>
18
+ RUN --mount=type=cache,id=bld-yarn-cache,target=/root/.yarn \
19
+ YARN_CACHE_FOLDER=/root/.yarn yarn install
20
+ <% else -%>
21
+ RUN yarn install
22
+ <% end -%>
23
+ <% else -%>
24
+ <% if options.cache? -%>
25
+ RUN --mount=type=cache,id=bld-yarn-cache,target=/root/.npm \
26
+ npm install
27
+ <% else -%>
28
+ RUN npm install
29
+ <% end -%>
30
+ <% end -%>
31
+
32
+ # build client application
33
+ COPY <%= api_client_dir %> .
34
+ RUN npm run build
35
+
36
+
37
+ <% end -%>
5
38
  FROM ruby:$RUBY_VERSION-slim as base
6
39
 
7
40
  # Rails app lives here
@@ -122,11 +155,16 @@ RUN apt-get update -qq && \
122
155
  <% end -%>
123
156
  <% end -%>
124
157
 
125
- # Copy built application from second stage
158
+ # Copy built application from previous stage
126
159
  <% if options.ci? -%>
127
160
  COPY --from=build /usr/local/bundle /usr/local/bundle
128
161
  <% end -%>
129
162
  COPY --from=build /rails /rails
163
+ <% if api_client_dir -%>
164
+
165
+ # Copy built client
166
+ COPY --from=client /rails/<%= api_client_dir %>/build /rails/public
167
+ <% end -%>
130
168
 
131
169
  # Deployment options
132
170
  ENV RAILS_LOG_TO_STDOUT="1" \
@@ -0,0 +1,71 @@
1
+ version: "3.8"
2
+ services:
3
+ web:
4
+ build: .
5
+ ports:
6
+ - "3000:3000"
7
+ environment:
8
+ - RAILS_MASTER_KEY=$RAILS_MASTER_KEY
9
+ <% if using_redis? -%>
10
+ - REDIS_URL=redis://redis-db:6379
11
+ <% end -%>
12
+ <% if deploy_database == 'postgresql' -%>
13
+ - DATABASE_URL=postgres://root:password@postgres-db/
14
+ <% elsif deploy_database == 'mysql' -%>
15
+ - DATABASE_URL=mysql2://root:password@mysql-db/
16
+ <% end -%>
17
+ <% if deploy_database == 'sqlite3' -%>
18
+ volumes:
19
+ - ./db:/rails/db
20
+ <% end -%>
21
+ <% if using_redis? or deploy_database != 'sqlite3' -%>
22
+ depends_on:
23
+ <% if using_redis? -%>
24
+ redis-db:
25
+ condition: service_started
26
+ <% end -%>
27
+ <% if deploy_database == 'postgresql' -%>
28
+ postgres-db:
29
+ condition: service_healthy
30
+ <% elsif deploy_database == 'mysql' -%>
31
+ mysql-db:
32
+ condition: service_healthy
33
+ <% end -%>
34
+ <% if deploy_database == 'postgresql' -%>
35
+
36
+ postgres-db:
37
+ image: postgres
38
+ environment:
39
+ POSTGRES_USER: root
40
+ POSTGRES_PASSWORD: password
41
+ volumes:
42
+ - ./tmp/db:/var/lib/postgresql/data
43
+ ports:
44
+ - "5432:5432"
45
+ healthcheck:
46
+ test: pg_isready
47
+ interval: 2s
48
+ timeout: 5s
49
+ retries: 30
50
+ <% elsif deploy_database == 'mysql' -%>
51
+
52
+ mysql-db:
53
+ image: mysql
54
+ command:
55
+ - --default-authentication-plugin=mysql_native_password
56
+ environment:
57
+ MYSQL_ROOT_PASSWORD: password
58
+ volumes:
59
+ - ./tmp/db:/var/lib/mysql
60
+ healthcheck:
61
+ test: mysqladmin ping -h 127.0.0.1 -u root --password=password
62
+ interval: 2s
63
+ timeout: 5s
64
+ retries: 30
65
+ <% end -%>
66
+ <% end -%>
67
+ <% if using_redis? -%>
68
+
69
+ redis-db:
70
+ image: redis
71
+ <% end -%>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dockerfile-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Ruby
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-14 00:00:00.000000000 Z
11
+ date: 2023-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -37,6 +37,7 @@ files:
37
37
  - lib/dockerfile-rails/scanner.rb
38
38
  - lib/generators/dockerfile_generator.rb
39
39
  - lib/generators/templates/Dockerfile.erb
40
+ - lib/generators/templates/docker-compose.yml.erb
40
41
  - lib/generators/templates/docker-entrypoint.erb
41
42
  - lib/generators/templates/dockerignore.erb
42
43
  - lib/generators/templates/node-version.erb