fly-apps / dockerfile-rails

Provides a Rails generator to produce Dockerfiles and related files.
MIT License
455 stars 38 forks source link

Getting error `/usr/bin/env: 'ruby\r': no such file or directory` in Windows #32

Closed lautarograc closed 1 year ago

lautarograc commented 1 year ago

I'm building a project using a template available in this repo

Now, someone tried running the project in Windows using docker desktop and they keep getting this error:

#17 110.5 Installing sprockets-rails 3.4.2 #17 110.6 Installing actionmailer 7.0.4.3 #17 110.7 Installing activestorage 7.0.4.3 #17 110.7 Installing railties 7.0.4.3 #17 111.1 Fetching actionmailbox 7.0.4.3 #17 111.1 Fetching actiontext 7.0.4.3 #17 111.3 Installing actionmailbox 7.0.4.3 #17 111.3 Installing actiontext 7.0.4.3 #17 112.1 Fetching cssbundling-rails 1.1.2 #17 112.1 Fetching jsbundling-rails 1.1.1 #17 112.1 Fetching turbo-rails 1.4.0
#17 112.1 Fetching stimulus-rails 1.2.1 #17 112.3 Installing cssbundling-rails 1.1.2 #17 112.3 Installing jsbundling-rails 1.1.1 #17 112.4 Installing stimulus-rails 1.2.1
#17 112.5 Installing turbo-rails 1.4.0
#17 114.7 Fetching actioncable 7.0.4.3
#17 114.7 Fetching puma 5.6.5
#17 114.9 Installing actioncable 7.0.4.3
#17 115.0 Installing puma 5.6.5 with native extensions
#17 115.1 Fetching rails 7.0.4.3
#17 115.3 Installing rails 7.0.4.3
#17 116.3 Fetching bootsnap 1.16.0
#17 116.4 Installing bootsnap 1.16.0 with native extensions
#17 127.0 Bundle complete! 18 Gemfile dependencies, 57 gems now installed.
#17 127.0 Gems in the groups 'development' and 'test' were not installed.
#17 127.0 Bundled gems are installed into `./vendor/bundle`
#17 DONE 130.2s
#18 [build 5/9] COPY --link package.json yarn.lock ./
# 18 DONE 0.1s
#19 [build 6/9] RUN yarn install --frozen-lockfile
#19 1.546 yarn install v1.22.19
#19 1.657 [1/4] Resolving packages...
#19 1.809 [2/4] Fetching packages...
#19 27.33 [3/4] Linking dependencies...
# 19 29.95 [4/4] Building fresh packages...
# 19 30.50 Done in 28.96s.
#19 DONE 30.7s
#20 [build 7/9] COPY --link . .
#20 DONE 0.2s
#21 [build 8/9] RUN bundle exec bootsnap precompile app/lib/
# 21 DONE 1.7s
#22 [build 9/9] RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets: precompile
#22 0.692 /usr/bin/env: 'ruby\r': No such file or directory
#22 ERROR: executor failed running [/bin/sh -c SECRET_KEY_BASE=DUMMY ./bin/rails assets: precompile]: exit code: 127
> [build 9/9] RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets: precompile:
#22 0.692 /usr/bin/env: 'ruby\r': No such file or directory
failed to solve: executor failed running [/bin/sh -c SECRET_KEY_BASE=DUMMY ./bin/rails assets: precompile]: exit code: 127 $

(sorry that I can't offer a more complete backtrace, it's what the person receiving the error sent me)

Here's the Dockerfile:

# syntax = docker/dockerfile:1

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.2.0
FROM ruby:$RUBY_VERSION-slim as base

# Rails app lives here
WORKDIR /rails

# Set production environment
ENV RAILS_ENV="production" \
    BUNDLE_WITHOUT="development:test" \
    BUNDLE_DEPLOYMENT="1"

# Update gems and bundler
RUN gem update --system --no-document && \
    gem install -N bundler

# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build gems and node modules
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y build-essential curl libpq-dev node-gyp pkg-config python-is-python3

# Install JavaScript dependencies
ARG NODE_VERSION=14.20.1
ARG YARN_VERSION=1.22.19
ENV PATH=/usr/local/node/bin:$PATH
RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \
    /tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
    npm install -g yarn@$YARN_VERSION && \
    rm -rf /tmp/node-build-master

# Install application gems
COPY --link Gemfile Gemfile.lock ./
RUN bundle install && \
    bundle exec bootsnap precompile --gemfile && \
    rm -rf ~/.bundle/ $BUNDLE_PATH/ruby/*/cache $BUNDLE_PATH/ruby/*/bundler/gems/*/.git

# Install node modules
COPY --link package.json yarn.lock ./
RUN yarn install --frozen-lockfile

# Copy application code
COPY --link . .

# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/

# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile

# Final stage for app image
FROM base

# Install packages needed for deployment
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y postgresql-client && \
    rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Run and own the application files as a non-root user for security
ARG UID=1000 \
    GID=1000
RUN groupadd -f -g $GID rails && \
    useradd -u $UID -g $GID rails --home /rails --shell /bin/bash
USER rails:rails

# Copy built artifacts: gems, application
COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build --chown=rails:rails /rails /rails

# Deployment options
ENV RAILS_LOG_TO_STDOUT="1" \
    RAILS_SERVE_STATIC_FILES="true"

# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "server"]

the docker-compose.yml

version: "3.8"
services:
  web:
    build:
      context: .
      args:
        UID: ${UID:-1000}
        GID: ${GID:-${UID:-1000}}
    ports:
      - "3000:3000"
    environment:
      - RAILS_MASTER_KEY=$RAILS_MASTER_KEY
      - DATABASE_URL=postgres://root:password@postgres-db/
    depends_on:
      postgres-db:
        condition: service_healthy

  postgres-db:
    image: postgres
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: password
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    healthcheck:
      test: pg_isready
      interval: 2s
      timeout: 5s
      retries: 30

(we're using docker compose to build and run the images)

The problem is that I don't really have any windows environment to test it. It runs perfectly in different Linux environments, and I've read that the problem is directly related to windows line endings. The other problem, not a minor one, is that the user trying to run the project is not really technical, so I vastly prefer to understand how to make it work out-of-the-box instead of offering some solution that the affected person has to run on they end.

rubys commented 1 year ago

Would it be possible for the person who is getting this error (on Windows) to rerun:

bin/rails generate dockerfile

... and then commit the results?

When the dockerfile is generated this gem will scan for this exact problem and insert extra code to correct it. That code is not necessary for non-Windows developers. I suspect what is happening is that the Windows user git config is set up to adjust CRLF.

Alternately, suggest that the windows developer adjust their git settings.