Skip to content

Commit 98b3183

Browse files
authored
Merge pull request rails#51390 from andrewn617/devcontainer-use-ruby-feature
Devcontainer using ruby image
2 parents cccbedf + 060eb5f commit 98b3183

File tree

7 files changed

+34
-33
lines changed

7 files changed

+34
-33
lines changed

.devcontainer/Dockerfile

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,16 @@
11
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.191.1/containers/ruby/.devcontainer/base.Dockerfile
22

33
# [Choice] Ruby version: 3, 3.3, 3.2, 3.1, 3.0, 2, 2.7, 2.6
4-
ARG VARIANT="3.3"
5-
FROM mcr.microsoft.com/devcontainers/ruby:${VARIANT}
6-
7-
# [Choice] Node.js version: none, lts/*, 16, 14, 12, 10
8-
ARG NODE_VERSION="lts/*"
9-
RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi
4+
ARG VARIANT="3.3.0"
5+
FROM ghcr.io/rails/devcontainer/images/ruby:${VARIANT}
106

117
# [Optional] Uncomment this section to install additional OS packages.
128
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
139
&& apt-get -y install --no-install-recommends \
1410
mariadb-client libmariadb-dev \
1511
postgresql-client postgresql-contrib libpq-dev \
16-
ffmpeg mupdf mupdf-tools libvips poppler-utils
17-
18-
19-
ARG IMAGEMAGICK_VERSION="7.1.0-5"
20-
RUN wget -qO /tmp/im.tar.xz https://imagemagick.org/archive/releases/ImageMagick-$IMAGEMAGICK_VERSION.tar.xz \
21-
&& wget -qO /tmp/im.sig https://imagemagick.org/archive/releases/ImageMagick-$IMAGEMAGICK_VERSION.tar.xz.asc \
22-
&& gpg --batch --keyserver keyserver.ubuntu.com --recv 89AB63D48277377A \
23-
&& gpg --batch --verify /tmp/im.sig /tmp/im.tar.xz \
24-
&& tar xJf /tmp/im.tar.xz -C /tmp \
25-
&& cd /tmp/ImageMagick-$IMAGEMAGICK_VERSION \
26-
&& ./configure --with-rsvg && make -j 9 && make install \
27-
&& ldconfig /usr/local/lib \
28-
&& rm -rf /tmp/*
12+
ffmpeg mupdf mupdf-tools libvips-dev poppler-utils \
13+
libxml2-dev sqlite3 imagemagick
2914

3015
# Add the Rails main Gemfile and install the gems. This means the gem install can be done
3116
# during build instead of on start. When a fork or branch has different gems, we still have an
@@ -44,8 +29,11 @@ COPY activerecord/activerecord.gemspec /tmp/rails/activerecord/
4429
COPY activestorage/activestorage.gemspec /tmp/rails/activestorage/
4530
COPY activesupport/activesupport.gemspec /tmp/rails/activesupport/
4631
COPY railties/railties.gemspec /tmp/rails/railties/
32+
# Docker does not support COPY as users other than root. So we need to chown this dir so we
33+
# can bundle as vscode user and then remove the tmp dir
34+
RUN chown -R vscode:vscode /tmp/rails
35+
USER vscode
4736
RUN cd /tmp/rails \
48-
&& bundle install \
49-
&& yarn install \
37+
&& /home/vscode/.rbenv/shims/bundle install \
5038
&& rm -rf /tmp/rails
51-
RUN chown -R vscode:vscode /usr/local/rvm
39+

.devcontainer/boot.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
bundle install
2-
yarn install
32

4-
sudo chown -R vscode:vscode /usr/local/bundle
3+
. ${NVM_DIR}/nvm.sh && nvm install --lts
4+
yarn install
55

66
cd activerecord
77

.devcontainer/devcontainer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
"features": {
1010
"ghcr.io/devcontainers/features/github-cli:1": {
1111
"version": "latest"
12+
},
13+
"ghcr.io/devcontainers/features/node:1": {
14+
"version": "latest"
1215
}
1316
},
1417

railties/lib/rails/generators/devcontainer.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ module Rails
44
module Generators
55
module Devcontainer
66
private
7-
def devcontainer_ruby_version
8-
gem_ruby_version.to_s.match(/^\d+\.\d+/).to_s
9-
end
10-
117
def devcontainer_dependencies
128
return @devcontainer_dependencies if @devcontainer_dependencies
139

@@ -63,6 +59,13 @@ def db_volume_name_for_devcontainer(database = options[:database])
6359
end
6460
end
6561

62+
def db_package_for_dockerfile(database = options[:database])
63+
case database
64+
when "mysql" then "default-libmysqlclient-dev"
65+
when "postgresql" then "libpq-dev"
66+
end
67+
end
68+
6669
def devcontainer_db_service_yaml(**options)
6770
return unless service = db_service_for_devcontainer
6871

railties/lib/rails/generators/rails/app/templates/.devcontainer/Dockerfile.tt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
2-
ARG RUBY_VERSION=<%= devcontainer_ruby_version %>
3-
FROM mcr.microsoft.com/devcontainers/ruby:1-$RUBY_VERSION-bookworm
2+
ARG RUBY_VERSION=<%= gem_ruby_version %>
3+
FROM ghcr.io/rails/devcontainer/images/ruby:$RUBY_VERSION
44

55
<%- unless options.skip_active_storage -%>
66
# Install packages needed to build gems
77
RUN apt-get update -qq && \
88
apt-get install --no-install-recommends -y \
9-
libvips \
9+
<%= db_package_for_dockerfile %> libvips \
1010
# For video thumbnails
1111
ffmpeg \
1212
# For pdf thumbnails. If you want to use mupdf instead of poppler,

railties/lib/rails/generators/rails/db/system/change/change_generator.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
require "rails/generators/base"
4+
require "yaml"
5+
require "json"
46

57
module Rails
68
module Generators

railties/test/generators/app_generator_test.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,8 +1011,7 @@ def test_inclusion_of_ruby_version
10111011
ruby_version = "#{Gem::Version.new(Gem::VERSION) >= Gem::Version.new("3.3.13") ? Gem.ruby_version : RUBY_VERSION}"
10121012

10131013
assert_file ".devcontainer/Dockerfile" do |content|
1014-
minor_ruby_version = ruby_version.match(/^\d+\.\d+/).to_s
1015-
assert_match(/ARG RUBY_VERSION=#{minor_ruby_version}$/, content)
1014+
assert_match(/ARG RUBY_VERSION=#{ruby_version}$/, content)
10161015
end
10171016
assert_file "Dockerfile" do |content|
10181017
assert_match(/ARG RUBY_VERSION=#{ruby_version}/, content)
@@ -1320,6 +1319,9 @@ def test_devonctainer_postgresql
13201319
assert_file("config/database.yml") do |content|
13211320
assert_match(/host: <%= ENV\["DB_HOST"\] %>/, content)
13221321
end
1322+
assert_file(".devcontainer/Dockerfile") do |content|
1323+
assert_match(/libpq-dev/, content)
1324+
end
13231325
end
13241326

13251327
def test_devonctainer_mysql
@@ -1348,6 +1350,9 @@ def test_devonctainer_mysql
13481350
assert_file("config/database.yml") do |content|
13491351
assert_match(/host: <%= ENV.fetch\("DB_HOST"\) \{ "localhost" } %>/, content)
13501352
end
1353+
assert_file(".devcontainer/Dockerfile") do |content|
1354+
assert_match(/default-libmysqlclient-dev/, content)
1355+
end
13511356
end
13521357

13531358
def test_devonctainer_mariadb

0 commit comments

Comments
 (0)