dockerfile-rails 0.4.9 → 0.5.1

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: 92c7394bd97da528362105514f095458b56872645ce0c3082c748c7bee22c54f
4
- data.tar.gz: e7a158e41126aa6321e5b4461951b6323f7fa950f8eef8eb366ee436454c0057
3
+ metadata.gz: 5146ba972235a473c3018799f3b75bc7856dad4c7c1e1b34a9fc0e87aa0de196
4
+ data.tar.gz: f9848a9d5103550a9fe7e1be7ee3820d4f99cee50ec39cc53933f568ffe29357
5
5
  SHA512:
6
- metadata.gz: 35619e605b9ed5176157c841efb21f4cfec9029c6383732d440bd9a440144719c8cc62e43974124148d5dbc1fb2705bd0160d4581f6829aa8ccdeb0040b665bb
7
- data.tar.gz: 94b595caa9b67e1d814b863f535eae373dca80a5b14d410122a0b0199fdb46955ddb6d7609f3ce58945e9419047da6128864a751f8af36dbb2d8230bfd87f1d8
6
+ metadata.gz: 14b6231959a35395703f0019a2c4e38062dd6b8d37d2c5c936dad2920c120118f7c95ad80a4c4d6591449bc2c79fc86c4ffd35bf3ac107ca6894291906ddd23b
7
+ data.tar.gz: d62ba6ea16c3705de8bc1606d8c1adbca6c91e591ff949d0d132f7f47b315662cc2659919a25febe9628dce5b7a9c34e5215dba0081b79525f6dae99b0fe4c3b
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  ## Overview
2
2
 
3
- Provides Rails generators to produce Dockerfiles and related files. This is being proposed as the generator to be included in Rails 7.1, and a substantial number of pull requests along those lines have already been merged. This repository contains fixes and features beyond those pull requests. Highlights:
3
+ Provides a Rails generator to produce Dockerfiles and related files. This is being proposed as the generator to be included in Rails 7.1, and a substantial number of pull requests along those lines have already been merged. This repository contains fixes and features beyond those pull requests. Highlights:
4
4
 
5
5
  * Supports all [Rails supported releases](https://guides.rubyonrails.org/maintenance_policy.html), not just Rails 7.1, and likely works with a number of previous releases.
6
6
  * Can be customized using flags on the `generate dockerfile` command, and rerun to produce a custom tailored dockerfile based on detecting the actual features used by your application.
@@ -9,7 +9,7 @@ Provides Rails generators to produce Dockerfiles and related files. This is bei
9
9
  ## Usage
10
10
 
11
11
  ```
12
- bundle add dockerfile-rails --group development
12
+ bundle add dockerfile-rails --version ">= 0.5.0" --group development
13
13
  bin/rails generate dockerfile
14
14
  ```
15
15
 
@@ -18,6 +18,7 @@ General options:
18
18
  * `--force` - overwrite existing files
19
19
  * `--ci` - include test gems in deployed image
20
20
  * `--bin-cd` - adjust binstubs to set current working directory
21
+ * `--no-prepare` - omit `db:prepare`. Useful for cloud platforms with [release](https://devcenter.heroku.com/articles/release-phase) phases.
21
22
  * `--platform=s` - specify target platform. See [FROM](https://docs.docker.com/engine/reference/builder/#from) for details.
22
23
  * `--cache` - use build caching to speed up builds
23
24
  * `--parallel` - use multi-stage builds to install gems and node modules in parallel
@@ -34,13 +35,15 @@ additional support may be needed:
34
35
  * `--redis` - add redis libraries
35
36
  * `--sqlite3` - add sqlite3 libraries
36
37
 
37
- Optimizations:
38
+ Runtime Optimizations:
38
39
 
39
40
  * `--fullstaq` - use [fullstaq](https://fullstaqruby.org/) [images](https://github.com/evilmartians/fullstaq-ruby-docker) on [quay.io](https://quay.io/repository/evl.ms/fullstaq-ruby?tab=tags&tag=latest)
40
41
  * `--jemalloc` - use [jemalloc](https://jemalloc.net/) memory allocator
41
42
  * `--yjit` - enable [YJIT](https://github.com/ruby/ruby/blob/master/doc/yjit/yjit.md) optimizing compiler
42
43
  * `--swap=n` - allocate swap space. See [falloc options](https://man7.org/linux/man-pages/man1/fallocate.1.html#OPTIONS) for suffixes
43
44
 
45
+ Options are saved between runs into `config/dockerfile.yml`. To invert a boolean options, add or remove a `no-` prefix from the option name.
46
+
44
47
  ## Testing
45
48
 
46
49
  The current testing strategy is to run `rails new` and `generate dockerfile` with various configurations and compare the generated artifacts with expected results. `ARG` values in `Dockerfiles` are masked before comparison.
@@ -74,3 +77,4 @@ Many of the following links relate to the current development status with respec
74
77
  * [Rails Dockerfile futures](https://discuss.rubyonrails.org/t/rails-dockerfile-futures/82091/1) - rationale for a generator
75
78
  * [Fly Cookbooks](https://fly.io/docs/rails/cookbooks/) - deeper dive into Dockerfile design choices
76
79
  * [app/templates/Dockerfile.tt](https://github.com/rails/rails/blob/main/railties/lib/rails/generators/rails/app/templates/Dockerfile.tt) - current Rails 7.1 template
80
+ * Fly.io [Cut over to Rails Dockerfile Generator on Sunday 29 Jan 2023](https://community.fly.io/t/cut-over-to-rails-dockerfile-generator-on-sunday-29-jan-2023/10350)
@@ -4,7 +4,7 @@ module DockerfileRails
4
4
 
5
5
  ### database ###
6
6
 
7
- database = YAML.load_file('config/database.yml').
7
+ database = YAML.load_file('config/database.yml', aliases: true).
8
8
  dig('production', 'adapter') rescue nil
9
9
 
10
10
  if database == 'sqlite3'
@@ -4,51 +4,87 @@ require_relative '../dockerfile-rails/scanner.rb'
4
4
  class DockerfileGenerator < Rails::Generators::Base
5
5
  include DockerfileRails::Scanner
6
6
 
7
- class_option :ci, type: :boolean, default: false,
7
+ OPTION_DEFAULTS = OpenStruct.new(
8
+ 'bin-cd' => false,
9
+ 'cache' => false,
10
+ 'ci' => false,
11
+ 'compose' => false,
12
+ 'fullstaq' => false,
13
+ 'jemalloc' => false,
14
+ 'mysql' => false,
15
+ 'parallel' => false,
16
+ 'platform' => nil,
17
+ 'postgresql' => false,
18
+ 'prepare' => true,
19
+ 'redis' => false,
20
+ 'swap' => nil,
21
+ 'yjit' => false,
22
+ )
23
+
24
+ # load defaults from config file
25
+ if File.exist? 'config/dockerfile.yml'
26
+ options = YAML.safe_load_file('config/dockerfile.yml', symbolize_names: true)[:options]
27
+ if options
28
+ OPTION_DEFAULTS.to_h.each do |option, value|
29
+ OPTION_DEFAULTS[option] = options[option] if options.include? option
30
+ end
31
+ end
32
+ end
33
+
34
+ class_option :ci, type: :boolean, default: OPTION_DEFAULTS.ci,
8
35
  desc: 'include test gems in bundle'
9
36
 
10
- class_option 'bin-cd', type: :boolean, default: false,
37
+ class_option 'bin-cd', type: :boolean, default: OPTION_DEFAULTS['bin-cd'],
11
38
  desc: 'modify binstubs to set working directory'
12
39
 
13
- class_option :cache, type: :boolean, default: false,
40
+ class_option :cache, type: :boolean, default: OPTION_DEFAULTS.cache,
14
41
  desc: 'use build cache to speed up installs'
15
42
 
16
- class_option :parallel, type: :boolean, default: false,
43
+ class_option :prepare, type: :boolean, default: OPTION_DEFAULTS.prepare,
44
+ desc: 'include db:prepare step'
45
+
46
+ class_option :parallel, type: :boolean, default: OPTION_DEFAULTS.parallel,
17
47
  desc: 'use build stages to install gems and node modules in parallel'
18
48
 
19
- class_option :swap, type: :string, default: nil,
49
+ class_option :swap, type: :string, default: OPTION_DEFAULTS.swap,
20
50
  desc: 'allocate swapspace'
21
51
 
22
- class_option :compose, type: :boolean, default: false,
52
+ class_option :compose, type: :boolean, default: OPTION_DEFAULTS.compose,
23
53
  desc: 'generate a docker-compose.yml file'
24
54
 
25
- class_option :redis, type: :boolean, default: false,
55
+ class_option :redis, type: :boolean, default: OPTION_DEFAULTS.redis,
26
56
  desc: 'include redis libraries'
27
57
 
28
- class_option :sqlite3, aliases: '--sqlite', type: :boolean, default: false,
58
+ class_option :sqlite3, aliases: '--sqlite', type: :boolean, default: OPTION_DEFAULTS.sqlite3,
29
59
  desc: 'include sqlite3 libraries'
30
60
 
31
- class_option :postgresql, aliases: '--postgres', type: :boolean, default: false,
61
+ class_option :postgresql, aliases: '--postgres', type: :boolean, default: OPTION_DEFAULTS.postgresql,
32
62
  desc: 'include postgresql libraries'
33
63
 
34
- class_option :mysql, type: :boolean, default: false,
64
+ class_option :mysql, type: :boolean, default: OPTION_DEFAULTS.mysql,
35
65
  desc: 'include mysql libraries'
36
66
 
37
- class_option :platform, type: :string, default: nil,
67
+ class_option :platform, type: :string, default: OPTION_DEFAULTS.platform,
38
68
  desc: 'image platform (example: linux/arm64)'
39
69
 
40
- class_option :jemalloc, type: :boolean, default: false,
70
+ class_option :jemalloc, type: :boolean, default: OPTION_DEFAULTS.jemalloc,
41
71
  desc: 'use jemalloc alternative malloc implementation'
42
72
 
43
- class_option :fullstaq, type: :boolean, default: false,
73
+ class_option :fullstaq, type: :boolean, default: OPTION_DEFAULTS.fullstaq,
44
74
  descr: 'use Fullstaq Ruby image from Quay.io'
45
75
 
46
- class_option :yjit, type: :boolean, default: false,
76
+ class_option :yjit, type: :boolean, default: OPTION_DEFAULTS.yjit,
47
77
  desc: 'enable YJIT optimizing compiler'
48
78
 
49
79
  def generate_app
50
80
  source_paths.push File.expand_path('./templates', __dir__)
51
81
 
82
+ # gather up options for config file
83
+ @dockerfile_config = OPTION_DEFAULTS.dup.to_h.stringify_keys
84
+ options.to_h.each do |option, value|
85
+ @dockerfile_config[option] = value if @dockerfile_config.include? option
86
+ end
87
+
52
88
  scan_rails_app
53
89
 
54
90
  template 'Dockerfile.erb', 'Dockerfile'
@@ -60,6 +96,8 @@ class DockerfileGenerator < Rails::Generators::Base
60
96
  chmod "bin/docker-entrypoint", 0755 & ~File.umask, verbose: false
61
97
 
62
98
  template 'docker-compose.yml.erb', 'docker-compose.yml' if options.compose
99
+
100
+ template 'dockerfile.yml.erb', 'config/dockerfile.yml'
63
101
  end
64
102
 
65
103
  private
@@ -187,6 +225,31 @@ private
187
225
  packages.sort
188
226
  end
189
227
 
228
+ def deploy_env
229
+ env = []
230
+
231
+ if Rails::VERSION::MAJOR<7 || Rails::VERSION::STRING.start_with?('7.0')
232
+ env << 'RAILS_LOG_TO_STDOUT="1"'
233
+ env << 'RAILS_SERVE_STATIC_FILES="true"'
234
+ end
235
+
236
+ if options.yjit
237
+ env << 'RUBY_YJIT_ENABLE="1"'
238
+ end
239
+
240
+ if options.jemalloc and not options.fullstaq
241
+ if (options.platform || Gem::Platform::local.cpu).include? 'arm'
242
+ env << 'LD_PRELOAD="/usr/lib/aarch64-linux-gnu/libjemalloc.so.2"'
243
+ else
244
+ env << 'LD_PRELOAD="/usr/lib/x86_64-linux-gnu/libjemalloc.so.2"'
245
+ end
246
+
247
+ env << 'MALLOC_CONF="dirty_decay_ms:1000,narenas:2,background_thread:true"'
248
+ end
249
+
250
+ env
251
+ end
252
+
190
253
  def binfile_fixups
191
254
  # binfiles may have OS specific paths to ruby. Normalize them.
192
255
  shebangs = Dir["bin/*"].map { |file| IO.read(file).lines.first }.join
@@ -126,18 +126,16 @@ COPY --from=build /rails /rails
126
126
  COPY --from=client /rails/<%= api_client_dir %>/build /rails/public
127
127
  <% end -%>
128
128
 
129
+ <% unless deploy_env.empty? -%>
129
130
  # Deployment options
130
- ENV RAILS_LOG_TO_STDOUT="1" \
131
- RAILS_SERVE_STATIC_FILES="true"<% if options.yjit %> \
132
- RUBY_YJIT_ENABLE="1"<% end %><% if options.jemalloc and not options.fullstaq %> \
133
- <% if (options.platform || Gem::Platform::local.cpu).include? 'arm' -%>
134
- LD_PRELOAD="/usr/lib/aarch64-linux-gnu/libjemalloc.so.2" \
135
- <% else -%>
136
- LD_PRELOAD="/usr/lib/x86_64-linux-gnu/libjemalloc.so.2" \
137
- <% end -%>
138
- MALLOC_CONF="dirty_decay_ms:1000,narenas:2,background_thread:true"<% end %>
131
+ ENV <%= deploy_env.join(" \\\n ") %>
139
132
 
133
+ <% end -%>
134
+ <% if options.prepare -%>
140
135
  # Entrypoint prepares the database.
136
+ <% else -%>
137
+ # Entrypoint sets up the container.
138
+ <% end -%>
141
139
  ENTRYPOINT ["/rails/bin/docker-entrypoint"]
142
140
 
143
141
  # Start the server by default, this can be overwritten at runtime
@@ -10,9 +10,14 @@ swapon /swapfile
10
10
  echo 1 > /proc/sys/vm/overcommit_memory
11
11
 
12
12
  <% end -%>
13
+ <% if options.prepare -%>
13
14
  # If running the rails server then create or migrate existing database
14
15
  if [ "${*}" == "./bin/rails server" ]; then
15
16
  ./bin/rails <%= dbprep_command %>
16
17
  fi
17
18
 
19
+ <% elsif !options.swap -%>
20
+ # Add any container initialization steps here
21
+
22
+ <% end -%>
18
23
  exec "${@}"
@@ -0,0 +1,3 @@
1
+ # generated by dockerfile-rails
2
+
3
+ <%= YAML.dump('options' => @dockerfile_config) %>
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.4.9
4
+ version: 0.5.1
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-24 00:00:00.000000000 Z
11
+ date: 2023-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -44,6 +44,7 @@ files:
44
44
  - lib/generators/templates/_npm_install.erb
45
45
  - lib/generators/templates/docker-compose.yml.erb
46
46
  - lib/generators/templates/docker-entrypoint.erb
47
+ - lib/generators/templates/dockerfile.yml.erb
47
48
  - lib/generators/templates/dockerignore.erb
48
49
  - lib/generators/templates/node-version.erb
49
50
  homepage: https://github.com/rubys/dockerfile-rails