dockerfile-rails 0.0.1 → 0.0.2

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: 7730245e044329345b271524027cd640794a3577c4b49d2a2ce47a3cf21c94c4
4
- data.tar.gz: 88fe182e5b8ab19d9725391cfaf1c0010e9d89d7cf44a9e3618f680bbe700315
3
+ metadata.gz: 8e00977164abc39c9b3b66c636834cda09f32f939045db5a5f496b51a455ee4f
4
+ data.tar.gz: bc3305fa419473f18eace29a65a9461fbd90c423106ff3cf1c5c42d0ed3b58ba
5
5
  SHA512:
6
- metadata.gz: 411539b040956ad9a1c437437aa36d2ed96a1769354162a2d8e57244c6a5b7dbf7fb1ff45efbd40f86da990b4741c01c6a871c84ac6be735104c4ab8ff39aa17
7
- data.tar.gz: 06b7e3ac3225bca70269b8aae26dcb9e2f9846b911416a09f4af6f0be82137eebd7f365bf815e0ff1a6c0783a7b8d082cf1bf5856f9913e90bb349408868d60f
6
+ metadata.gz: d92c231f9d0484dcc196837aab9a3f3c3b6158202d5c8d942a61231ec9d5ec3fa0af8cf54a8daffea5d6f1103241aa5bb88a79cb9ea0a29974a08b579f7b2356
7
+ data.tar.gz: 1e65aec6411ddac4d9785f087f16c2b8645e0895701fe6da58d93369d45d2aa8b71c4cb48c777e64737cf805a08ca4fc4da66a44d0ab007fa36f2351065ffc95
data/README.md CHANGED
@@ -1,3 +1,28 @@
1
1
  ## Purpose
2
2
 
3
- Provide Rails generators to produce Dockerfiles and related files.
3
+ Provide Rails generators to produce Dockerfiles and related files.
4
+
5
+ ## Usage
6
+
7
+ ```
8
+ bundle add dockerfile-rails
9
+ bin/rails generate dockerfile
10
+ ```
11
+
12
+ General options:
13
+
14
+ * `--force` - overwrite existing files
15
+ * `--ci` - include test gems in bundle
16
+ * `--cache` - use build caching to speed up builds
17
+ * `--parallel` - use multi-stage builds to install gems and node modules in parallel
18
+
19
+ Dependencies:
20
+
21
+ Generally the dockerfile generator will be able to determine what dependencies you
22
+ are actually using. But should you be using DATABASE_URL, for example, at runtime
23
+ additional support may be needed:
24
+
25
+ * `--mysql` - add mysql libraries
26
+ * `--posgresql` - add posgresql libraries
27
+ * `--redis` - add redis libraries
28
+ * `--sqlite3` - add sqlite3 libraries
@@ -62,14 +62,6 @@ module DockerfileRails
62
62
  end
63
63
 
64
64
  @redis = @redis_cable || @redis_cache || @sidekiq
65
-
66
- ### api-only ###
67
-
68
- @apionly = Rails.application.config.api_only
69
-
70
- ### db:prepare ###
71
-
72
- @dbprep = (Rails::VERSION::MAJOR >= 6) ? 'db:prepare' : 'db:migrate'
73
65
  end
74
66
  end
75
67
  end
@@ -1,6 +1,27 @@
1
1
  class DockerfileGenerator < Rails::Generators::Base
2
2
  include DockerfileRails::Scanner
3
3
 
4
+ class_option :ci, type: :boolean, default: false,
5
+ desc: 'include test gems in bundle'
6
+
7
+ class_option :cache, type: :boolean, default: false,
8
+ desc: 'use build cache to speed up installs'
9
+
10
+ class_option :parallel, type: :boolean, default: false,
11
+ desc: 'use build stages to install gems and node modules in parallel'
12
+
13
+ class_option :redit, type: :boolean, default: false,
14
+ desc: 'include redis libraries'
15
+
16
+ class_option :sqlite3, aliases: '--sqlite', type: :boolean, default: false,
17
+ desc: 'include sqlite3 libraries'
18
+
19
+ class_option :postgresql, aliases: '--postgres', type: :boolean, default: false,
20
+ desc: 'include postgresql libraries'
21
+
22
+ class_option :mysql, type: :boolean, default: false,
23
+ desc: 'include mysql libraries'
24
+
4
25
  def generate_app
5
26
  source_paths.push File.expand_path('./templates', __dir__)
6
27
 
@@ -18,7 +39,12 @@ class DockerfileGenerator < Rails::Generators::Base
18
39
  private
19
40
 
20
41
  def using_node?
21
- File.exist? 'package.json'
42
+ return @using_node if @using_node != nil
43
+ @using_node = File.exist? 'package.json'
44
+ end
45
+
46
+ def parallel?
47
+ using_node? && options.parallel
22
48
  end
23
49
 
24
50
  def keeps?
@@ -31,17 +57,19 @@ private
31
57
  packages = %w(build-essential)
32
58
 
33
59
  # add databases: sqlite3, postgres, mysql
34
- packages += %w(pkg-config libpq-dev default-libmysqlclient-dev)
60
+ packages << 'pkg-config' if options.sqlite3? or @sqlite3
61
+ packages << 'libpq-dev' if options.postgresql? or @postgresql
62
+ packages << 'default-libmysqlclient-dev' if options.mysql or @mysql
35
63
 
36
64
  # add redis in case Action Cable, caching, or sidekiq are added later
37
- packages << "redis"
65
+ packages << "redis" if options.redis? or @redis
38
66
 
39
67
  # ActiveStorage preview support
40
68
  packages << "libvips" if @gemfile.include? 'ruby-vips'
41
69
 
42
70
  # node support, including support for building native modules
43
71
  if using_node?
44
- packages += %w(curl node-gyp) # pkg-config already listed above
72
+ packages += %w(curl node-gyp pkg-config)
45
73
 
46
74
  # module build process depends on Python, and debian changed
47
75
  # how python is installed with the bullseye release. Below
@@ -63,15 +91,19 @@ private
63
91
  end
64
92
  end
65
93
 
66
- packages.sort
94
+ packages.sort.uniq
67
95
  end
68
96
 
69
97
  def deploy_packages
98
+ packages = []
99
+
70
100
  # start with databases: sqlite3, postgres, mysql
71
- packages = %w(libsqlite3-0 postgresql-client default-mysql-client)
101
+ packages << 'libsqlite3-0' if options.sqlite3? or @sqlite3
102
+ packages << 'postgresql-client' if options.postgresql? or @postgresql
103
+ packages << 'default-mysql-client' if options.mysql or @mysql
72
104
 
73
105
  # add redis in case Action Cable, caching, or sidekiq are added later
74
- packages << "redis"
106
+ packages << "redis" if options.redis? or @redis
75
107
 
76
108
  # ActiveStorage preview support
77
109
  packages << "libvips" if @gemfile.include? 'ruby-vips'
@@ -127,4 +159,12 @@ private
127
159
  def api_only?
128
160
  Rails.application.config.api_only
129
161
  end
162
+
163
+ def dbprep_command
164
+ if Rails::VERSION::MAJOR >= 6
165
+ 'db:prepare'
166
+ else
167
+ 'db:migrate'
168
+ end
169
+ end
130
170
  end
@@ -10,16 +10,31 @@ WORKDIR /rails
10
10
  # Set production environment
11
11
  ENV RAILS_ENV="production" \
12
12
  BUNDLE_PATH="vendor/bundle" \
13
- BUNDLE_WITHOUT="development:test"
13
+ BUNDLE_WITHOUT="<%= options.ci? ? 'test' : 'development:test' %>"
14
14
 
15
+ ARG BUNDLER_VERSION=<%= Bundler::VERSION %>
16
+ RUN gem update --system --no-document && \
17
+ gem install -N bundler -v ${BUNDLER_VERSION}
15
18
 
16
- # Throw-away build stage to reduce size of final image
17
- FROM base as build
19
+
20
+ # Throw-away build stage<%= parallel? ? 's' : '' %> to reduce size of final image
21
+ FROM base as <%= parallel? ? 'pre' : '' %>build
18
22
 
19
23
  # Install packages need to build gems<%= using_node? ? " and node modules" : "" %>
24
+ <% if options.cache? %>
25
+ RUN --mount=type=cache,id=dev-apt-cache,sharing=locked,target=/var/cache/apt \
26
+ --mount=type=cache,id=dev-apt-lib,sharing=locked,target=/var/lib/apt \
27
+ apt-get update -qq && \
28
+ <% else -%>
20
29
  RUN apt-get update -qq && \
30
+ <% end -%>
21
31
  apt-get install -y <%= build_packages.join(" ") %>
22
32
 
33
+ <% if parallel? -%>
34
+
35
+ FROM prebuild as node
36
+
37
+ <% end -%>
23
38
  <% if using_node? -%>
24
39
  # Install JavaScript dependencies
25
40
  ARG NODE_VERSION=<%= node_version %>
@@ -28,15 +43,47 @@ ENV VOLTA_HOME="/usr/local"
28
43
  RUN curl https://get.volta.sh | bash && \
29
44
  volta install node@$NODE_VERSION yarn@$YARN_VERSION
30
45
 
46
+ <% end -%>
47
+ <% if parallel? -%>
48
+ # Install node modules
49
+ COPY package.json yarn.lock ./
50
+ <% if options.cache? -%>
51
+ RUN --mount=type=cache,id=bld-yarn-cache,target=/root/.yarn \
52
+ YARN_CACHE_FOLDER=/root/.yarn yarn install
53
+ <% else -%>
54
+ RUN yarn install
55
+ <% end -%>
56
+
57
+
58
+ FROM prebuild as build
59
+
31
60
  <% end -%>
32
61
  # Install application gems
33
62
  COPY Gemfile Gemfile.lock ./
34
- RUN bundle install
63
+ <% if options.cache? -%>
64
+ RUN --mount=type=cache,id=bld-gem-cache,sharing=locked,target=/srv/vendor \
65
+ bundle config set app_config .bundle && \
66
+ bundle config set path /srv/vendor && \
67
+ bundle _${BUNDLER_VERSION}_ install && \
68
+ bundle clean && \
69
+ mkdir -p vendor && \
70
+ bundle config set path vendor && \
71
+ cp -ar /srv/vendor .
72
+ <% else -%>
73
+ RUN bundle _${BUNDLER_VERSION}_ install
74
+ <% end -%>
35
75
 
36
- <% if using_node? -%>
76
+ <% if parallel? -%>
77
+ asdf
78
+ <% elsif using_node? -%>
37
79
  # Install node modules
38
80
  COPY package.json yarn.lock ./
81
+ <% if options.cache? -%>
82
+ RUN --mount=type=cache,id=bld-yarn-cache,target=/root/.yarn \
83
+ YARN_CACHE_FOLDER=/root/.yarn yarn install
84
+ <% else -%>
39
85
  RUN yarn install
86
+ <% end -%>
40
87
 
41
88
  <% end -%>
42
89
  # Copy application code
@@ -54,19 +101,31 @@ RUN bundle exec bootsnap precompile --gemfile app/ lib/
54
101
  <% end -%>
55
102
  <% unless api_only? -%>
56
103
  # Precompiling assets for production without requiring secret RAILS_MASTER_KEY
57
- RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
104
+ RUN SECRET_KEY_BASE<%= Rails::VERSION::MAJOR<7 || Rails::VERSION::STRING.start_with?('7.0') ? '=DUMMY' : '_DUMMY=1' %> ./bin/rails assets:precompile
58
105
 
59
106
  <% end -%>
60
107
 
61
108
  # Final stage for app image
62
109
  FROM base
110
+ <% unless deploy_packages.empty? -%>
63
111
 
64
112
  # Install packages need for deployment
113
+ <% if options.cache? -%>
114
+ RUN --mount=type=cache,id=dev-apt-cache,sharing=locked,target=/var/cache/apt \
115
+ --mount=type=cache,id=dev-apt-lib,sharing=locked,target=/var/lib/apt \
116
+ apt-get update -qq && \
117
+ apt-get install --no-install-recommends -y <%= deploy_packages.join(" ") %>
118
+ <% else -%>
65
119
  RUN apt-get update -qq && \
66
120
  apt-get install --no-install-recommends -y <%= deploy_packages.join(" ") %> && \
67
121
  rm -rf /var/lib/apt/lists /var/cache/apt/archives
122
+ <% end -%>
123
+ <% end -%>
68
124
 
69
125
  # Copy built application from second stage
126
+ <% if options.ci? -%>
127
+ COPY --from=build /usr/local/bundle /usr/local/bundle
128
+ <% end -%>
70
129
  COPY --from=build /rails /rails
71
130
 
72
131
  # Deployment options
@@ -2,7 +2,7 @@
2
2
 
3
3
  # If running the rails server then create or migrate existing database
4
4
  if [ "${*}" == "./bin/rails server" ]; then
5
- ./bin/rails db:prepare
5
+ ./bin/rails <%= dbprep_command %>
6
6
  fi
7
7
 
8
8
  exec "${@}"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dockerfile-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Ruby