Gem package versions are not pinned

Installing gems without versions can change the packages included in a Docker build.

Description

When gem install specifies only a gem name, the version installed on the next build can change with the repository.

Potential impact

A deployment image built with a different gem version from the one tested can encounter compatibility problems or runtime errors.

Remediation

  • Supply a version for each gem you install directly, using gem install name:version.
  • Test the new version before updating the pin.

Examples

These installation excerpts assume an image with Ruby and RubyGems available. The versions are historical syntax examples, not production recommendations. Choose reviewed, supported versions and manage transitive dependencies with a lock file.

Before

dockerfile
RUN gem install bundler
RUN ["gem", "install", "blunder"]

After

dockerfile
RUN gem install bundler:2.0.2
ENV GRPC_VERSION 1.0.0
RUN gem install grpc:${GRPC_VERSION} grpc-tools:${GRPC_VERSION}

Explanation:

  • Before: Both the shell and JSON forms specify only the gem name.
  • After: bundler:2.0.2 specifies a literal version. grpc and grpc-tools both use GRPC_VERSION.

References