How to use environment variables with Docker

27 views Asked by At

Outside of Docker, I am able to run bundle config gem.fury.io $GEMFURY_API_KEY followed by bundle install to authenticate and download a gem from a private repository on gemfury. However, when I perform the same steps within a Docker container, I get an error message specifying that I need to authenticate:

[+] Building 4.6s (18/20)                                                                                                                                                                                                                                docker:rancher-desktop
 => [web internal] load build definition from Dockerfile                                                                                                                                                                                                                   0.0s
 => => transferring dockerfile: 995B                                                                                                                                                                                                                                       0.0s
 => [web internal] load .dockerignore                                                                                                                                                                                                                                      0.0s
 => => transferring context: 133B                                                                                                                                                                                                                                          0.0s
 => [web internal] load metadata for docker.io/library/ruby:2.5.5-alpine                                                                                                                                                                                                   1.6s
 => [web internal] load metadata for docker.io/library/node:18.18.0-bullseye-slim                                                                                                                                                                                          1.6s
 => [web stage-1  1/12] FROM docker.io/library/ruby:2.5.5-alpine@sha256:1165a3878359e0b9814d5437460d01548ce0d79cb27570b316a4ace44515b241                                                                                                                                   0.0s
 => [web client 1/3] FROM docker.io/library/node:18.18.0-bullseye-slim@sha256:2799dd3bd32deb984133859e6c9dd1ee181968fcafe563f4653e5b3c151af716                                                                                                                             0.0s
 => [web internal] load build context                                                                                                                                                                                                                                      0.6s
 => => transferring context: 1.34MB                                                                                                                                                                                                                                        0.5s
 => CACHED [web stage-1  2/12] WORKDIR /app                                                                                                                                                                                                                                0.0s
 => CACHED [web stage-1  3/12] RUN echo 'gem: --no-ri --no-rdoc' > ~/.gemrc                                                                                                                                                                                                0.0s
 => CACHED [web stage-1  4/12] RUN apk add build-base postgresql-dev git tzdata && rm -rf /var/cache/apk/*                                                                                                                                                                 0.0s
 => CACHED [web stage-1  5/12] COPY . /app                                                                                                                                                                                                                                 0.0s
 => CACHED [web stage-1  6/12] COPY Gemfile Gemfile.lock ./                                                                                                                                                                                                                0.0s
 => CACHED [web stage-1  7/12] RUN gem install bundler -v 2.3.26                                                                                                                                                                                                           0.0s
 => CACHED [web stage-1  8/12] RUN bundle config gem.fury.io $GEMFURY_API_KEY                                                                                                                                                                                     0.0s
 => CACHED [web stage-1  9/12] RUN bundle config                                                                                                                                                                                                                           0.0s
 => CACHED [web client 2/3] COPY client .                                                                                                                                                                                                                                  0.0s
 => CACHED [web client 3/3] RUN yarn install && yarn build                                                                                                                                                                                                                 0.0s
 => ERROR [web stage-1 10/12] RUN bundle install --jobs 20 --retry 5 --full-index                                                                                                                                                                                          2.4s
------
 > [web stage-1 10/12] RUN bundle install --jobs 20 --retry 5 --full-index:
0.459 The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
0.459 Fetching https://github.com/krisleech/wisper-rspec.git
2.312 Warning: NOT CREATING A SIGNED GEM. Could not find signing key: /root/.ssh/gem-private_key.pem
2.320 Authentication is required for gem.fury.io.
2.320 Please supply credentials for this source. You can do this by running:
2.320  bundle config gem.fury.io username:password
------
failed to solve: process "/bin/sh -c bundle install --jobs 20 --retry 5 --full-index" did not complete successfully: exit code: 17

In my docker-compose.yml file, I am using the env_file field to import env vars from .env.local (adjacent to my Dockerfile and docker-compose.yml files), where one of these values is the GEMFURY_API_KEY env var I am passing to bundle config within my Dockerfile:

version: '3'
services:

  web:
    build: .
    command: bundle exec rails s -p 4000 -b '0.0.0.0'
    env_file: .env.local
    volumes:
      - .:/app
      - ~/.aws:/root/.aws:ro
    stdin_open: true
    tty: true
    ports:
      - "4000:4000"

I have a Dockerfile with the following contents:

# ===== STAGE ONE =====
#  Build React Client
# =====================
FROM node:18.18.0-bullseye-slim AS client

# Copy in the react app and build it
COPY client .

ENV NODE_OPTIONS "--max-old-space-size=4096"
RUN yarn install && yarn build

# ===== STAGE TWO =====
# Add rails application
# =====================
FROM ruby:2.5.5-alpine

ENV RAILS_ROOT /app
WORKDIR $RAILS_ROOT

RUN echo 'gem: --no-ri --no-rdoc' > ~/.gemrc

RUN apk add build-base postgresql-dev git tzdata && rm -rf /var/cache/apk/*

COPY . $RAILS_ROOT
COPY Gemfile Gemfile.lock ./
RUN gem install bundler -v 2.3.26
RUN bundle config gem.fury.io $GEMFURY_API_KEY
RUN bundle config
RUN bundle install --jobs 20 --retry 5 --full-index

RUN rm -rf $RAILS_ROOT/client && mkdir -p $RAILS_ROOT/public
COPY --from=client public $RAILS_ROOT/public

ENTRYPOINT ["./docker-entrypoint.sh"]
EXPOSE 4000

CMD ["rails", "s", "-p", "4000", "-b", "0.0.0.0"]

And a Gemfile with this:

# ...

source 'https://gem.fury.io/<private-repository-name>/' do
  gem '<private-gem-name>', '~> 1.0.42'
end

# ...

I have:

  • Verified that when I run bundle config outside of Docker, I can only see the one configuration setting I made earlier with bundle config gem.fury.io $GEMFURY_IO_TOKEN
  • Looked at the ruby:2.5.5-alpine base image on Docker to check if there are any default bundler settings made in it that I have not set locally
  • Verified that the bundler version I have installed locally is the same as that running in my Docker container

The only thing that I can think that might be happening is that my GEMFURY_API_KEY value is not getting loaded within my container for some reason, or that my bundler configuration settings are getting set, but not subsequently loaded when I call bundle install in the next step.

0

There are 0 answers