dockerfile-rails 0.4.1 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7267f137214c8c519555cc4b3775f844eb9f4028a01522b1fee0952301f488a5
4
- data.tar.gz: a27afadb1ccc74385cc12ddd4c6dea9445487035c48babd26eaab3c4712d539d
3
+ metadata.gz: 68a853a1d2a0a40c1912a99dd77fe1664abe12bf1678eb4dc07a17873259bc39
4
+ data.tar.gz: 90bcd80ae9f8a6c8c27cc806235bdec6b9bf308ca3a3ebf10712304f7a52a10e
5
5
  SHA512:
6
- metadata.gz: 5ee1cf151c6521cdc3b29e13008716fa392e49649e6bc9614f095c911805c153e235c75704a3d0c09a6f1ffab9cb5bb3d9db92dac32fd441a7e350418bcee3f7
7
- data.tar.gz: 993df0bde29fffa8b637cbf142e28403a02143c697f2df5c95021721b0ef4ba85e8a251d6196d99a1156b07e2f9dd82511c009df9641884892d64ec0ec3052bf
6
+ metadata.gz: 693b078d6d664fdd7366c036dbbb9f95732c79b0f058a2bfe7300c886c479885b33ba075d9d598a1266b2a030bf6bfc588aee6c9074a570e6bd9720749765d28
7
+ data.tar.gz: a74d6fe97db12495762f5b6dfcd5ff3cf9c32f2807f9402f9e159f485a5e5c8562347811d12fbdf7601ce6755592c7413ebb6402b2c8c606a90c7c0d5e43148e
data/README.md CHANGED
@@ -17,6 +17,7 @@ General options:
17
17
 
18
18
  * `--force` - overwrite existing files
19
19
  * `--ci` - include test gems in deployed image
20
+ * `--platform=s` - specify target platform. See [FROM](https://docs.docker.com/engine/reference/builder/#from) for details.
20
21
  * `--cache` - use build caching to speed up builds
21
22
  * `--parallel` - use multi-stage builds to install gems and node modules in parallel
22
23
  * `--compose` - generate a `docker-compose.yml` file
@@ -36,7 +37,8 @@ Optimizations:
36
37
 
37
38
  * `--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)
38
39
  * `--jemalloc` - use [jemalloc](https://jemalloc.net/) memory allocator
39
- * `--yjit` - enable [YJIT](https://github.com/ruby/ruby/blob/master/doc/yjit/yjit.md) optimizing compiler.
40
+ * `--yjit` - enable [YJIT](https://github.com/ruby/ruby/blob/master/doc/yjit/yjit.md) optimizing compiler
41
+ * `--swap=n` - allocate swap space. See [falloc options](https://man7.org/linux/man-pages/man1/fallocate.1.html#OPTIONS) for suffixes
40
42
 
41
43
  Links:
42
44
 
@@ -44,3 +46,4 @@ Links:
44
46
  * [Preparations for Rails 7.1](https://community.fly.io/t/preparations-for-rails-7-1/9512)
45
47
  * [Rails Dockerfile futures](https://discuss.rubyonrails.org/t/rails-dockerfile-futures/82091/1)
46
48
  * [Fly Cookbooks](https://fly.io/docs/rails/cookbooks/)
49
+ * [app/templates/Dockerfile.tt](https://github.com/rails/rails/blob/main/railties/lib/rails/generators/rails/app/templates/Dockerfile.tt)
@@ -12,6 +12,9 @@ class DockerfileGenerator < Rails::Generators::Base
12
12
  class_option :parallel, type: :boolean, default: false,
13
13
  desc: 'use build stages to install gems and node modules in parallel'
14
14
 
15
+ class_option :swap, type: :string, default: nil,
16
+ desc: 'allocate swapspace'
17
+
15
18
  class_option :compose, type: :boolean, default: false,
16
19
  desc: 'generate a docker-compose.yml file'
17
20
 
@@ -27,6 +30,9 @@ class DockerfileGenerator < Rails::Generators::Base
27
30
  class_option :mysql, type: :boolean, default: false,
28
31
  desc: 'include mysql libraries'
29
32
 
33
+ class_option :platform, type: :string, default: nil,
34
+ desc: 'image platform (example: linux/arm64)'
35
+
30
36
  class_option :jemalloc, type: :boolean, default: false,
31
37
  desc: 'use jemalloc alternative malloc implementation'
32
38
 
@@ -75,7 +81,15 @@ private
75
81
  end).new(self, options[:locals] || {})
76
82
 
77
83
  template = IO.read(File.join(source_paths.last, "_#{options[:partial]}.erb"))
78
- ERB.new(template.strip, trim_mode: '-').result(scope.get_binding)
84
+ ERB.new(template, trim_mode: '-').result(scope.get_binding).strip
85
+ end
86
+
87
+ def platform
88
+ if options.platform
89
+ "--platform #{options.platform} "
90
+ else
91
+ ""
92
+ end
79
93
  end
80
94
 
81
95
  def using_node?
@@ -87,6 +101,10 @@ private
87
101
  options.redis? or @redis
88
102
  end
89
103
 
104
+ def using_execjs?
105
+ @gemfile.include? 'execjs'
106
+ end
107
+
90
108
  def parallel?
91
109
  using_node? && options.parallel
92
110
  end
@@ -114,9 +132,13 @@ private
114
132
  # ActiveStorage preview support
115
133
  packages << "libvips" if @gemfile.include? 'ruby-vips'
116
134
 
135
+ # Rmagick gem
136
+ packages += %w[pkg-config libmagickwand-dev] if @gemfile.include? 'rmagick'
137
+
117
138
  # node support, including support for building native modules
118
139
  if using_node?
119
- packages += %w(curl unzip node-gyp pkg-config)
140
+ packages += %w(node-gyp pkg-config)
141
+ packages += %w(curl unzip) unless using_execjs?
120
142
 
121
143
  # module build process depends on Python, and debian changed
122
144
  # how python is installed with the bullseye release. Below
@@ -155,6 +177,9 @@ private
155
177
  # ActiveStorage preview support
156
178
  packages << "libvips" if @gemfile.include? 'ruby-vips'
157
179
 
180
+ # Rmagick gem
181
+ packages << 'imagemagick' if @gemfile.include? 'rmagick'
182
+
158
183
  packages.sort
159
184
  end
160
185
 
@@ -8,9 +8,9 @@ ARG RUBY_VERSION=<%= RUBY_VERSION %>
8
8
 
9
9
  <% end -%>
10
10
  <% if options.fullstaq -%>
11
- FROM quay.io/evl.ms/fullstaq-ruby:${RUBY_VERSION}-<%= @options.jemalloc ? 'jemalloc-' : '' %>slim as base
11
+ FROM <%= platform %>quay.io/evl.ms/fullstaq-ruby:${RUBY_VERSION}-<%= @options.jemalloc ? 'jemalloc-' : '' %>slim as base
12
12
  <% else -%>
13
- FROM ruby:$RUBY_VERSION-slim as base
13
+ FROM <%= platform %>ruby:$RUBY_VERSION-slim as base
14
14
  <% end -%>
15
15
 
16
16
  # Rails app lives here
@@ -21,23 +21,24 @@ ENV RAILS_ENV="production" \
21
21
  BUNDLE_PATH="vendor/bundle" \
22
22
  BUNDLE_WITHOUT="<%= options.ci? ? 'test' : 'development:test' %>"
23
23
 
24
+ # Update gems and preinstall the desired version of bundler
24
25
  ARG BUNDLER_VERSION=<%= Bundler::VERSION %>
25
26
  RUN gem update --system --no-document && \
26
27
  gem install -N bundler -v ${BUNDLER_VERSION}
27
28
 
29
+ <% if using_execjs? -%>
30
+ # Install packages needed to install nodejs
31
+ <%= render partial: 'apt_install', locals: {packages: %w(curl unzip), clean: true} %>
32
+
33
+ <%= render partial: 'install_node', locals: {yarn_version: nil} %>
34
+
35
+ <% end -%>
28
36
 
29
37
  # Throw-away build stage<%= parallel? ? 's' : '' %> to reduce size of final image
30
38
  FROM base as <%= parallel? ? 'pre' : '' %>build
31
39
 
32
- # Install packages need to build gems<%= using_node? ? " and node modules" : "" %>
33
- <% if options.cache? %>
34
- RUN --mount=type=cache,id=dev-apt-cache,sharing=locked,target=/var/cache/apt \
35
- --mount=type=cache,id=dev-apt-lib,sharing=locked,target=/var/lib/apt \
36
- apt-get update -qq && \
37
- <% else -%>
38
- RUN apt-get update -qq && \
39
- <% end -%>
40
- apt-get install -y <%= build_packages.join(" ") %>
40
+ # Install packages needed to build gems<%= using_node? ? " and node modules" : "" %>
41
+ <%= render partial: 'apt_install', locals: {packages: build_packages, clean: false} %>
41
42
 
42
43
  <% if parallel? -%>
43
44
 
@@ -45,24 +46,11 @@ FROM prebuild as node
45
46
 
46
47
  <% end -%>
47
48
  <% if using_node? -%>
48
- # Install JavaScript dependencies
49
- ARG NODE_VERSION=<%= node_version %>
50
- ARG YARN_VERSION=<%= yarn_version %>
51
- RUN curl -fsSL https://fnm.vercel.app/install | bash && \
52
- /root/.local/share/fnm/fnm install $NODE_VERSION
53
- ENV PATH=/root/.local/share/fnm/aliases/default/bin/:$PATH
54
- RUN npm install -g yarn@$YARN_VERSION
49
+ <%= render partial: 'install_node', locals: {node_version: using_execjs? ? nil : node_version} %>
55
50
 
56
51
  <% end -%>
57
52
  <% if parallel? -%>
58
- # Install node modules
59
- COPY package.json yarn.lock ./
60
- <% if options.cache? -%>
61
- RUN --mount=type=cache,id=bld-yarn-cache,target=/root/.yarn \
62
- YARN_CACHE_FOLDER=/root/.yarn yarn install
63
- <% else -%>
64
- RUN yarn install
65
- <% end -%>
53
+ <%= render partial: 'npm_install', locals: {sources: %w(package.json yarn.lock)} %>
66
54
 
67
55
 
68
56
  FROM prebuild as build
@@ -91,14 +79,7 @@ RUN bundle _${BUNDLER_VERSION}_ install<% if depend_on_bootsnap? -%> && \
91
79
  <% if parallel? -%>
92
80
  asdf
93
81
  <% elsif using_node? -%>
94
- # Install node modules
95
- COPY package.json yarn.lock ./
96
- <% if options.cache? -%>
97
- RUN --mount=type=cache,id=bld-yarn-cache,target=/root/.yarn \
98
- YARN_CACHE_FOLDER=/root/.yarn yarn install
99
- <% else -%>
100
- RUN yarn install
101
- <% end -%>
82
+ <%= render partial: 'npm_install', locals: {sources: %w(package.json yarn.lock)} %>
102
83
 
103
84
  <% end -%>
104
85
  # Copy application code
@@ -124,17 +105,8 @@ RUN SECRET_KEY_BASE<%= Rails::VERSION::MAJOR<7 || Rails::VERSION::STRING.start_w
124
105
  FROM base
125
106
  <% unless deploy_packages.empty? -%>
126
107
 
127
- # Install packages need for deployment
128
- <% if options.cache? -%>
129
- RUN --mount=type=cache,id=dev-apt-cache,sharing=locked,target=/var/cache/apt \
130
- --mount=type=cache,id=dev-apt-lib,sharing=locked,target=/var/lib/apt \
131
- apt-get update -qq && \
132
- apt-get install --no-install-recommends -y <%= deploy_packages.join(" ") %>
133
- <% else -%>
134
- RUN apt-get update -qq && \
135
- apt-get install --no-install-recommends -y <%= deploy_packages.join(" ") %> && \
136
- rm -rf /var/lib/apt/lists /var/cache/apt/archives
137
- <% end -%>
108
+ # Install packages needed for deployment
109
+ <%= render partial: 'apt_install', locals: {packages: deploy_packages, clean: true} %>
138
110
  <% end -%>
139
111
 
140
112
  # Copy built application from previous stage
@@ -152,7 +124,11 @@ COPY --from=client /rails/<%= api_client_dir %>/build /rails/public
152
124
  ENV RAILS_LOG_TO_STDOUT="1" \
153
125
  RAILS_SERVE_STATIC_FILES="true"<% if options.yjit %> \
154
126
  RUBY_YJIT_ENABLE="1"<% end %><% if options.jemalloc and not options.fullstaq %> \
127
+ <% if (options.platform || Gem::Platform::local.cpu).include? 'arm' -%>
128
+ LD_PRELOAD="/usr/lib/aarch64-linux-gnu/libjemalloc.so.2" \
129
+ <% else -%>
155
130
  LD_PRELOAD="/usr/lib/x86_64-linux-gnu/libjemalloc.so.2" \
131
+ <% end -%>
156
132
  MALLOC_CONF="dirty_decay_ms:1000,narenas:2,background_thread:true"<% end %>
157
133
 
158
134
  # Entrypoint prepares the database.
@@ -0,0 +1,10 @@
1
+ <% if options.cache? -%>
2
+ RUN --mount=type=cache,id=dev-apt-cache,sharing=locked,target=/var/cache/apt \
3
+ --mount=type=cache,id=dev-apt-lib,sharing=locked,target=/var/lib/apt \
4
+ apt-get update -qq && \
5
+ apt-get install --no-install-recommends -y <%= packages.join(" ") %>
6
+ <% else -%>
7
+ RUN apt-get update -qq && \
8
+ apt-get install --no-install-recommends -y <%= packages.join(" ") %><% if clean %> && \
9
+ rm -rf /var/lib/apt/lists /var/cache/apt/archives<% end %>
10
+ <% end -%>
@@ -0,0 +1,21 @@
1
+ <% if node_version and yarn_version -%>
2
+ # Install JavaScript dependencies
3
+ <% elsif node_version -%>
4
+ # Install Node.js
5
+ <% elsif yarn_version -%>
6
+ # Install yarn
7
+ <% end -%>
8
+ <% if node_version -%>
9
+ ARG NODE_VERSION=<%= node_version %>
10
+ <% end -%>
11
+ <% if yarn_version -%>
12
+ ARG YARN_VERSION=<%= yarn_version %>
13
+ <% end -%>
14
+ <% if node_version -%>
15
+ RUN curl -fsSL https://fnm.vercel.app/install | bash && \
16
+ /root/.local/share/fnm/fnm install $NODE_VERSION
17
+ ENV PATH=/root/.local/share/fnm/aliases/default/bin/:$PATH
18
+ <% end -%>
19
+ <% if yarn_version -%>
20
+ RUN npm install -g yarn@$YARN_VERSION
21
+ <% end -%>
@@ -1,28 +1,12 @@
1
1
  ARG NODE_VERSION=<%= node_version %>
2
2
 
3
- FROM node:$NODE_VERSION-slim as client
3
+ FROM <%= platform %>node:$NODE_VERSION-slim as client
4
4
 
5
5
  WORKDIR /rails/<%= api_client_dir %>
6
6
 
7
7
  ENV NODE_ENV=production
8
8
 
9
- # Install node modules
10
- COPY <%= api_client_files.join(' ') %> .
11
- <% if api_client_files.join.include? 'yarn' -%>
12
- <% if options.cache? -%>
13
- RUN --mount=type=cache,id=bld-yarn-cache,target=/root/.yarn \
14
- YARN_CACHE_FOLDER=/root/.yarn yarn install
15
- <% else -%>
16
- RUN yarn install
17
- <% end -%>
18
- <% else -%>
19
- <% if options.cache? -%>
20
- RUN --mount=type=cache,id=bld-yarn-cache,target=/root/.npm \
21
- npm install
22
- <% else -%>
23
- RUN npm install
24
- <% end -%>
25
- <% end -%>
9
+ <%= render partial: 'npm_install', locals: {sources: api_client_files} %>
26
10
 
27
11
  # build client application
28
12
  COPY <%= api_client_dir %> .
@@ -0,0 +1,17 @@
1
+ # Install node modules
2
+ COPY <%=sources.join(' ') %> .
3
+ <% if sources.join.include? 'yarn' -%>
4
+ <% if options.cache? -%>
5
+ RUN --mount=type=cache,id=bld-yarn-cache,target=/root/.yarn \
6
+ YARN_CACHE_FOLDER=/root/.yarn yarn install
7
+ <% else -%>
8
+ RUN yarn install
9
+ <% end -%>
10
+ <% else -%>
11
+ <% if options.cache? -%>
12
+ RUN --mount=type=cache,id=bld-npm-cache,target=/root/.npm \
13
+ npm install
14
+ <% else -%>
15
+ RUN npm install
16
+ <% end -%>
17
+ <% end -%>
@@ -1,5 +1,15 @@
1
1
  #!/bin/bash
2
2
 
3
+ <% if options.swap -%>
4
+ # allocate swap space
5
+ fallocate -l <%= options.swap %> /swapfile
6
+ chmod 0600 /swapfile
7
+ mkswap /swapfile
8
+ echo 10 > /proc/sys/vm/swappiness
9
+ swapon /swapfile
10
+ echo 1 > /proc/sys/vm/overcommit_memory
11
+
12
+ <% end -%>
3
13
  # If running the rails server then create or migrate existing database
4
14
  if [ "${*}" == "./bin/rails server" ]; then
5
15
  ./bin/rails <%= dbprep_command %>
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.1
4
+ version: 0.4.3
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-16 00:00:00.000000000 Z
11
+ date: 2023-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -36,10 +36,12 @@ files:
36
36
  - Rakefile
37
37
  - lib/dockerfile-rails.rb
38
38
  - lib/dockerfile-rails/scanner.rb
39
- - lib/erbtest.rb
40
39
  - lib/generators/dockerfile_generator.rb
41
40
  - lib/generators/templates/Dockerfile.erb
41
+ - lib/generators/templates/_apt_install.erb
42
+ - lib/generators/templates/_install_node.erb
42
43
  - lib/generators/templates/_node_client.erb
44
+ - lib/generators/templates/_npm_install.erb
43
45
  - lib/generators/templates/docker-compose.yml.erb
44
46
  - lib/generators/templates/docker-entrypoint.erb
45
47
  - lib/generators/templates/dockerignore.erb
data/lib/erbtest.rb DELETED
@@ -1,56 +0,0 @@
1
- TEMPLATE = <<-EOF
2
- value is <%= five %>
3
- other is <%= three %>
4
- EOF
5
-
6
- require 'delegate'
7
- require 'forwardable'
8
- require 'ostruct'
9
- class Scope < SimpleDelegator
10
- end
11
-
12
- require 'erb'
13
- class ShoppingList
14
- def five
15
- 'FiVe'
16
- end
17
-
18
- def initialize
19
- renderer = ERB.new(TEMPLATE)
20
- puts '***'
21
-
22
- values = {one: 'One', three: 'tHrEe'}
23
-
24
- scope = (Class.new do
25
- def initialize(obj, locals)
26
- @_obj = obj
27
- @_locals = OpenStruct.new(locals)
28
- end
29
-
30
- def method_missing(method, *args, &block)
31
- if @_locals.respond_to? method
32
- @_locals.send method, *args, &block
33
- else
34
- @_obj.send method, *args, &block
35
- end
36
- end
37
-
38
- def get_binding
39
- binding
40
- end
41
-
42
- def three
43
- 3
44
- end
45
- end).new(self, values)
46
-
47
- puts renderer.result(scope.get_binding)
48
- end
49
-
50
- def get_binding
51
- binding
52
- end
53
- end
54
-
55
- puts '?'
56
- test = ShoppingList.new