Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kamal exec rails console no such file or directory #728

Closed
randohinn opened this issue Mar 19, 2024 · 3 comments
Closed

kamal exec rails console no such file or directory #728

randohinn opened this issue Mar 19, 2024 · 3 comments

Comments

@randohinn
Copy link

randohinn commented Mar 19, 2024

This might be a kamal on windows powershell specific thing... Here is the output when trying to launch a console

 kamal app exec -i "bin/rails c"       
Get most recent version available as an image...
Launching interactive command with version latest via SSH from new container on SERVER IP...
bash: line 1: docker run -it --rm --env-file .kamal/env/roles/IMAGENAME.env COMPANYNAME/IMAGENAME:latest bin/rails c: No such file or directory
Connection to SERVER IP closed.

And the dockerfile of this app

# syntax = docker/dockerfile:1

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.3.0
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base

# Rails app lives here
WORKDIR /rails

# Set production environment
ENV RAILS_ENV="production" \
    BUNDLE_DEPLOYMENT="1" \
    BUNDLE_PATH="/usr/local/bundle" \
    BUNDLE_WITHOUT="development"


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

# Install packages needed to build gems
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y build-essential git libvips pkg-config libmariadb-dev

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

# Copy application code
COPY . .

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


# Adjust binfiles to be executable on Linux
RUN chmod +x bin/* && \
    sed -i "s/\r$//g" bin/* && \
    sed -i 's/ruby\.exe$/ruby/' bin/*

# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails tailwindcss:build


# Final stage for app image
FROM base

# Install packages needed for deployment
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y curl libsqlite3-0 libvips libmariadb-dev cron && \
    rm -rf /var/lib/apt/lists /var/cache/apt/archives && \
    chmod u+s /usr/sbin/cron


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

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
    usermod -aG crontab rails && \
    chown -R rails:rails db log storage tmp &&\
    chmod u+s /usr/sbin/cron

USER rails:rails

# 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"]

How do I get a working rails console?

@ttilberg
Copy link

Does the app run otherwise?

It’s hard to tell if the “command not found” is coming from the host trying by to execute docker, or the container trying to execute bin/rails, but consider that it could be either. Ensure the first host defined is your rails app, and not an accessory or other image.

Finally, I recall having issues in my windows days when a file would have Windows line endings (\r\n) being executed on a Linux host, similar to what you’ve described. You might try checking your files to make sure they are consistently ending with Linux endings, particularly the .ENV file.

Actually, the more I think about how easy it would be to do this in your setup, and what effects line endings of \r\n would have in Linux, I think that’s the first thing you should do. Open your .env file in an editor that makes it easy to convert to Linux line endings, and then kamal env push.

Good luck friend!

@randohinn
Copy link
Author

The app works flawlessly otherwise. And if i run docker run -it --rm --env-file .kamal/env/roles/IMAGENAME.env COMPANYNAME/IMAGENAME:latest bin/rails c directly on the host, it works too

@djmb
Copy link
Collaborator

djmb commented May 2, 2024

@randohinn - if you try to run kamal app exec -i bin/rails does it give you a No such file or directory error, or does it run ok (giving a usage summary).

Or what happens if you re-use an existing container - kamal app exec -i --reuse "bin/rails c"?

@dhh dhh closed this as completed May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants