{{ def is_alpine: env.variant | startswith("alpine") ; def is_slim: env.variant | startswith("slim-") ; def clean_apt: # TODO once bookworm is EOL, remove this and just hard-code "apt-get dist-clean" instead if env.variant | contains("bookworm") then "rm -rf /var/lib/apt/lists/*" else "apt-get dist-clean" end -}} {{ if is_alpine then ( -}} FROM alpine:{{ env.variant | ltrimstr("alpine") }} {{ ) elif is_slim then ( -}} FROM debian:{{ env.variant | ltrimstr("slim-") }}-slim {{ ) else ( -}} FROM buildpack-deps:{{ env.variant }} {{ ) end -}} {{ if is_slim then ( -}} RUN set -eux; \ apt-get update; \ apt-get install -y --no-install-recommends \ ca-certificates \ ; \ {{ clean_apt }} {{ ) else "" end -}} # skip installing gem documentation with `gem install`/`gem update` RUN set -eux; \ mkdir -p /usr/local/etc; \ echo 'gem: --no-document' >> /usr/local/etc/gemrc ENV LANG C.UTF-8 # https://www.ruby-lang.org/{{ .post | ltrimstr("/") }} ENV RUBY_VERSION {{ .version }} ENV RUBY_DOWNLOAD_URL {{ .url.xz }} ENV RUBY_DOWNLOAD_SHA256 {{ .sha256.xz }} # some of ruby's build scripts are written in ruby # we purge system ruby later to make sure our final image uses what we just built RUN set -eux; \ \ {{ if is_alpine then ( -}} apk add --no-cache --virtual .ruby-builddeps \ autoconf \ bzip2 \ bzip2-dev \ ca-certificates \ coreutils \ dpkg-dev dpkg \ g++ \ gcc \ gdbm-dev \ glib-dev \ gmp-dev \ libc-dev \ libffi-dev \ libxml2-dev \ libxslt-dev \ linux-headers \ make \ ncurses-dev \ openssl \ openssl-dev \ patch \ procps \ yaml-dev \ zlib-dev \ ruby \ tar \ xz \ yaml-dev \ zlib-dev \ ; \ {{ ) else ( -}} savedAptMark="$(apt-mark showmanual)"; \ apt-get update; \ apt-get install -y --no-install-recommends \ dpkg-dev \ libgdbm-dev \ ruby \ {{ if is_slim then ( -}} autoconf \ bzip2 \ g++ \ gcc \ libbz2-dev \ libffi-dev \ libgdbm-compat-dev \ libglib2.0-dev \ libgmp-dev \ libncurses-dev \ libssl-dev \ libxml2-dev \ libxslt-dev \ libyaml-dev \ make \ wget \ xz-utils \ zlib1g-dev \ {{ ) else "" end -}} ; \ {{ ) end -}} \ rustArch=; \ {{ def archVar: if is_alpine then "apkArch" else "dpkgArch" end -}} {{ archVar }}="$({{ if is_alpine then "apk --print-arch" else "dpkg --print-architecture" end }})"; \ case "${{ archVar }}" in \ {{ ( .rustup.arches | (if is_alpine then "musl" else "glibc" end) as $libcKey | to_entries | map( .key as $bashbrewArch | ( if is_alpine then { amd64: "x86_64", arm32v6: "armhf", arm32v7: "armv7", arm64v8: "aarch64", i386: "x86", ppc64le: "ppc64le", riscv64: "riscv64", s390x: "s390x", } else { amd64: "amd64", arm32v5: "armel", arm32v7: "armhf", arm64v8: "arm64", i386: "i386", mips64le: "mips64el", ppc64le: "ppc64el", riscv64: "riscv64", s390x: "s390x", } end )[$bashbrewArch] as $distroArch | select($distroArch) | .value | select(has($libcKey)) | .[$libcKey] | ( -}} {{ $distroArch | @sh }}) rustArch={{ .arch | @sh }}; rustupUrl={{ .url | @sh }}; rustupSha256={{ .sha256 | @sh }} ;; \ {{ ) ) | join("") ) -}} esac; \ \ if [ -n "$rustArch" ]; then \ mkdir -p /tmp/rust; \ \ wget -O /tmp/rust/rustup-init "$rustupUrl"; \ echo "$rustupSha256 */tmp/rust/rustup-init" | sha256sum --check --strict; \ chmod +x /tmp/rust/rustup-init; \ \ export RUSTUP_HOME='/tmp/rust/rustup' CARGO_HOME='/tmp/rust/cargo'; \ export PATH="$CARGO_HOME/bin:$PATH"; \ /tmp/rust/rustup-init -y --no-modify-path --profile minimal --default-toolchain {{ .rust.version | @sh }} --default-host "$rustArch"; \ \ rustc --version; \ cargo --version; \ fi; \ \ wget -O ruby.tar.xz "$RUBY_DOWNLOAD_URL"; \ echo "$RUBY_DOWNLOAD_SHA256 *ruby.tar.xz" | sha256sum --check --strict; \ \ mkdir -p /usr/src/ruby; \ tar -xJf ruby.tar.xz -C /usr/src/ruby --strip-components=1; \ rm ruby.tar.xz; \ \ cd /usr/src/ruby; \ \ {{ if is_alpine and (IN(env.version; "3.3", "3.4", "4.0") or .version == "4.1.0-preview1") then ( -}} # https://github.com/docker-library/ruby/issues/196 # https://bugs.ruby-lang.org/issues/14387#note-13 (patch source) # https://bugs.ruby-lang.org/issues/14387#note-16 ("Therefore ncopa's patch looks good for me in general." -- only breaks glibc which doesn't matter here) wget -O 'thread-stack-fix.patch' 'https://bugs.ruby-lang.org/attachments/download/7081/0001-thread_pthread.c-make-get_main_stack-portable-on-lin.patch'; \ echo '3ab628a51d92fdf0d2b5835e93564857aea73e0c1de00313864a94a6255cb645 *thread-stack-fix.patch' | sha256sum --check --strict; \ patch -p1 -i thread-stack-fix.patch; \ rm thread-stack-fix.patch; \ \ {{ ) else "" end -}} autoconf; \ gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)"; \ # full RELRO: "-z now" disables lazy binding so the loader can map the GOT read-only at startup # https://github.com/docker-library/ruby/pull/529 # https://salsa.debian.org/ruby-team/ruby/-/blob/8a55ab83e2c1783d80d0117fecbda533ac75f3e3/debian/rules#L63-67 (Debian's "hardening=+bindnow" produces the same flags) export LDFLAGS='-Wl,-z,relro -Wl,-z,now'; \ ./configure \ --build="$gnuArch" \ --disable-install-doc \ --enable-shared \ ${rustArch:+--enable-yjit} \ {{ if env.version | IN("3.3", "3.4") then "" else ( -}} ${rustArch:+--enable-zjit} \ {{ ) end -}} ; \ make -j "$(nproc)"; \ make install; \ \ rm -rf /tmp/rust; \ {{ if is_alpine then ( -}} runDeps="$( \ scanelf --needed --nobanner --format '%n#p' --recursive /usr/local \ | tr ',' '\n' \ | sort -u \ | awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \ )"; \ apk add --no-network --virtual .ruby-rundeps $runDeps; \ apk del --no-network .ruby-builddeps; \ {{ ) else ( -}} apt-mark auto '.*' > /dev/null; \ apt-mark manual $savedAptMark > /dev/null; \ find /usr/local -type f -executable -not \( -name '*tkinter*' \) -exec ldd '{}' ';' \ | awk '/=>/ { so = $(NF-1); if (index(so, "/usr/local/") == 1) { next }; gsub("^/(usr/)?", "", so); printf "*%s\n", so }' \ | sort -u \ | xargs -r dpkg-query --search \ # https://manpages.debian.org/bookworm/dpkg/dpkg-query.1.en.html#S (we ignore diversions and it'll be really unusual for more than one package to provide any given .so file) | awk 'sub(":$", "", $1) { print $1 }' \ | sort -u \ | xargs -r apt-mark manual \ ; \ apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \ {{ clean_apt }}; \ {{ ) end -}} \ cd /; \ rm -r /usr/src/ruby; \ # verify we have no "ruby" packages installed {{ if is_alpine then ( -}} if \ apk --no-network list --installed \ | grep -v '^[.]ruby-' \ | grep -i ruby \ ; then \ exit 1; \ fi; \ {{ ) else ( -}} if dpkg -l | grep -i ruby; then exit 1; fi; \ {{ ) end -}} [ "$(command -v ruby)" = '/usr/local/bin/ruby' ]; \ # rough smoke test ruby --version; \ gem --version; \ bundle --version # don't create ".bundle" in all our apps ENV GEM_HOME /usr/local/bundle ENV BUNDLE_SILENCE_ROOT_WARNING=1 \ BUNDLE_APP_CONFIG="$GEM_HOME" ENV PATH $GEM_HOME/bin:$PATH RUN set -eux; \ mkdir "$GEM_HOME"; \ # adjust permissions of GEM_HOME for running "gem install" as an arbitrary user chmod 1777 "$GEM_HOME" CMD [ "irb" ]