Rails docker - Bundle with custom path not able to access gems - ruby-on-rails

I have a Dockerfile with below main parts:
FROM ruby:2.2.4
# Create user and group
RUN groupadd myapp --gid 6156 && \
useradd --home /home/myapp --create-home --shell /bin/false --uid 6157 --gid 6156 myapp
# Create and switch to the repo dir
ENV REPO_DIR /home/myapp/repo
RUN mkdir $REPO_DIR
WORKDIR $REPO_DIR
# First install gems
ADD Gemfile $REPO_DIR
ADD Gemfile.lock $REPO_DIR
RUN mkdir -p /var/bundle &&\
chown -R myapp:myapp /var/bundle &&\
mkdir $REPO_DIR/.bundle &&\
chown -R myapp:myapp $REPO_DIR/.bundle
RUN su -c " cd $REPO_DIR && bundle install --deployment --path /var/bundle" -s /bin/bash -l myapp
...
CMD ["rails","s", "-b", "0.0.0.0"]
the main parts are
run as a customer myapp user
bundle install with --deployment and --path /var/bundle arguments
When I do docker-compose up , the image is built properly. However, when I run
docker-compose run --service-ports api /bin/bash
and then,
$ bundle check
says that all the gems are missing..
$ bundle config
says
Settings are listed in order of priority. The top value will be used.
silence_root_warning
Set via BUNDLE_SILENCE_ROOT_WARNING: "1"
app_config
Set via BUNDLE_APP_CONFIG: "/usr/local/bundle"
bin
Set via BUNDLE_BIN: "/usr/local/bundle/bin"
path
Set via BUNDLE_PATH: "/usr/local/bundle"
There is some problem, as the BUNDLE_PATH should be /var/bundle in this case.
Obviously, bundle exec rails says rails not found. Please install all missing gems.
What should I be doing to access the installed gems from /var/bundle?
Bundle install fails in Rails Docker container looks similar, but did not help. All my tests are in local.

Related

Problem Loading Ruby Rails Unicorn in ECS Fargate When Building Image in CircleCI (Works Locally)

I am having issues deploying a Ruby on Rails App to ECS Fargate. When I build the image locally (the same way it is done in the pipeline). I can easily start the web service with this command ["bundle", "exec", "unicorn", "-c", "config/unicorn.rb"]. However, running this same image in Fargate returns this: 2022-07-27 15:46:33bundler: failed to load command: unicorn (/usr/local/bundle/bin/unicorn)
Here is my Docker file:
FROM ruby:2.6.7
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client sudo
WORKDIR /app
ENV RAILS_ENV="staging"
ENV NODE_ENV="staging"
ENV LANG="en_US.UTF-8"
ENV RACK_ENV ="staging"
ENV BUNDLE_WITHOUT='development:test'
ARG GITHUB_TOKEN
RUN gem install bundler -v '2.2.28'
RUN bundle config https://github.com/somename/somerepo someuser:"${GITHUB_TOKEN}"
COPY . /app
RUN rm -rf /app/tmp
RUN mkdir -p /app/tmp
RUN bundle install
RUN bundle exec rails assets:precompile
EXPOSE 3000
The CMD is missing from Docker because its'a added in the task definition. Has anyone run into this issue? I've tried a number of different approaches, but am unsure of what is being changed locally running and running in Fargate.
Update
Looking into this issue further and found some more information that will need to be updated here.
I am using CircleCI to build and push this image to ECR. The issue seems to be that when created in CircleCI, any artifacts created by the bundle install become unreachable on run time and Docker is unable to run any gems because the GEM path is not even accessible to the root user. I pulled the image created by CircleCI Docker, locally, and confirmed the same errors. EXECing into the container and running chmod 755 -R /usr/local/bundle/bin/ and then executing the bundle exec to start the service, properly starts unicorn.
Next Steps
As a result, I attempted to add those changes into the Dockerfile on build and the same behavior still persists.
RUN bundle install
RUN chmod 755 -R /usr/local/bundle/bin/
Then I tried changing permissions in an entrypoint script and the container won't start at all.
Finally figured this out a few days ago. The answer is to add VOLUME arguments at the end of your Dockerfile. This will maintain persistence with any changes you have made. My final Dockerfile:
FROM ruby:2.6.7
ARG NPM_TOKEN
RUN apt-get update -qq && apt-get install -y \
build-essential \
curl \
postgresql-client \
software-properties-common \
sudo
RUN curl -fsSL https://deb.nodesource.com/setup_9.x | sudo -E bash - && \
sudo apt-get install -y nodejs
RUN apt-get install -y \
npm \
yarn
ENV BUNDLE_WITHOUT='development:test'
ENV RAILS_ENV="staging"
ENV RACK_ENV="staging"
ENV NPM_TOKEN="${NPM_TOKEN}"
RUN mkdir -p /dashboard
WORKDIR /dashboard
RUN mkdir -p /dashboard/tmp/pids
COPY Gemfile* ./
COPY gems/rails_admin_history_rollback /dashboard/gems/rails_admin_history_rollback
ARG GITHUB_TOKEN
RUN sudo gem install bundler -v '2.2.28' && \
bundle config https://github.com/some-company/some-repo some-name:"${GITHUB_TOKEN}"
RUN bundle install
COPY . /dashboard
RUN bundle exec rails assets:precompile
RUN chmod +x bin/entrypoint.sh
VOLUME /dashboard/
VOLUME /usr/local/bundle
EXPOSE 3000

app changes not reflecting while runnning docker image

I am running a docker image and whenever I make any new changes inside the app folder such as in controllers or views the changes are not relfected in browser. Everytime I need to create a new image inorder to reflect the changes.
Dockerfile
FROM ruby:3.0
USER root
RUN apt-get update && apt-get install -y build-essential libpq-dev nodejs
RUN rm -rf .idea \
rm -rf .gitignore
ENV LC_ALL=en_US.UTF-8
ENV LANG=en_US.UTF-8
ENV LANGUAGE=en_US.UTF-8
RUN mkdir -p /app
WORKDIR /app
COPY Gemfile Gemfile.lock ./
RUN gem install bundler && bundle install --jobs 20 --retry 5
COPY . ./
# Adding a non root user
RUN groupadd --system non-root-user
RUN useradd -u 1001 --system non-root-user -g non-root-user -d /home/non-root-user -m -s /bin/bash
RUN chown -R non-root-user /app
RUN chmod -R 777 /app
# Switching to non-root user 'non-root-user'
USER non-root-user
# Exposing port 3010
EXPOSE 3010
# Default entry point (i.e. will be appened before every command being executed)
ENTRYPOINT ["sh", "./config/docker/startup.sh"]
startup.sh
#! /bin/sh
echo "Creating tmp/pids..."
mkdir -p tmp/pids/
# Start Application
echo "Starting up puma(app server)..."
bundle exec puma -p 3010 -C ./config/puma.rb
I have updated the below configuration in my development.rb still no success
config.file_watcher = ActiveSupport::FileUpdateChecker
Ruby - 3.0.0
Rails - 6.1
I am running the container with the below command
docker run -it -p 3010:3010 -e RAILS_ENV=development my_image

Could not find ast-2.3.0 in any of the sources

The Dockerfile is deprecated.
Here I am installing bundle install and create a new directory /bundle where the bundle is installed. While running this image as a container, I am mounting the current directory to /code which is the WORKDIR so that all the rakefiles, Gemfile, GEMFile.lock are all available there. But the ENTRYPOINT command "bundle exec rake syntax" fails everytime I try to start the container.
FROM puppet/puppet-agent-alpine
RUN mkdir /code && \
mkdir /bundle
WORKDIR /code
RUN apk update && apk add git
COPY Gemfile Gemfile.lock /code/
RUN gem install --no-ri --no-rdoc bundler && \
bundle install --without linters --path /bundle && \
gem cleanup
ENTRYPOINT ["bundle", "exec","rake", "syntax"]
COPY docker/syntax/Dockerfile /Dockerfile
I tried deleting the Gemfile.lock with no luck.
When I override the entrypoint in runtime, I can login to the container and manually run the entry point command which is bundle exec rake syntax.
It still gives me the same error.
I added path to /bundle.
But this command runs successfully if I manually do bundle install once again after logging into the container.
Any help would be appreciated!
Try:
$ docker-compose build
Images get stale and don't rebuild automatically.

Why does docker overwrite everything I've installed?

I've got a rails proxy app that runs with jupyter notebooks, and I'm trying to get a docker image up and running with supervisor as the entry point.
However, after including a FROM jupyter/notebook, everything I did to set up the rails is gone!
My dockerfile
#Installs Jupyter Notebook and IPython kernel from the current branch
# Another Docker container should inherit with `FROM jupyter/notebook`
# to run actual services.
# For RVM support - ultimately do out own solution here.
FROM tzenderman/docker-rvm:latest
# Update aptitude
RUN apt-get update
# Install software
RUN apt-get install -y git supervisor libgmp3-dev
# Set up loggin for now
RUN mkdir -p /var/log/supervisor
# USING TOKENS ###################################################
ARG DEPLOYMENT_TOKEN
RUN git clone https://$DEPLOYMENT_TOKEN:x-oauth-basic#github.com/proversity-org/edx-api-jupyter.git /tmpapp/
RUN mkdir /home/sifu/
RUN cp -R /tmpapp/* /home/sifu/
RUN cp -R -r /tmpapp/. /home/sifu/
RUN chown root:root -R /home/sifu/
#################################################################
WORKDIR /home/sifu
# Install ruby using RVM
RUN /bin/bash -l -c "rvm install $(cat .ruby-version) --verify-downloads"
RUN /bin/bash -l -c "rvm use $(cat .ruby-version) --default"
RUN /bin/bash -l -c "rvm list"
RUN rvm requirements
# run docker env for ruby apps
RUN /bin/bash -l -c "source .docker-ruby-version"
RUN echo $RUBY-VERSION
ENV GEM_HOME /usr/local
ENV PATH /usr/local/rvm/gems/ruby-2.2.3/bin:$PATH
ENV PATH /usr/local/rvm/rubies/ruby-2.2.3/bin:$PATH
# Install Bundler
RUN ruby --version
RUN gem install bundler
RUN bundle config --global silence_root_warning 1
# Install Sifu gems
RUN bundle install --gemfile=/home/sifu/Gemfile
# Alow for arugments to sifu & notebook (server ip & port etc)
#RUN /bin/bash -l -c "which bundle"
#RUN /bin/bash -l -c "cp $(which ruby) /usr/bin/"
# Set up supervisor config -- move this up later
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
# Set as environment variables
FROM jupyter/notebook
CMD ["/usr/bin/supervisord"]
ENTRYPOINT ["/usr/bin/supervisord", "-c", "/etc/myapp/supervisord.conf"]
All the software I installed is gone, when I use a FROM jupyter/notebook to include the notebooks in the image.
Is there something crucial I am missing?

Issue with docker and bundler

I'm running some issues with docker/docker-compose and bundler. After building my image I can run the rails server correctly without any issue, but, when I try to run a console with rails console I constantly get:
Could not find i18n-0.7.0 in any of the sources
Run `bundle install` to install missing gems.
If I try to run bundle install in the container there is no problem, all seems to be correctly installed.
docker-compose run web bundle install
...
Using spring 1.3.6
Using therubyracer 0.12.2
Using turbolinks 2.5.3
Using uglifier 2.7.2
Using web-console 2.2.1
Updating files in vendor/cache
Bundle complete! 24 Gemfile dependencies, 119 gems now installed.
Bundled gems are installed into /usr/local/bundle.
My docker-compose.yml looks like this:
db:
image: postgres
web:
build: .
command: rails s -p 3000 -b 0.0.0.0
ports:
- "3000:3000"
volumes:
- .:/app
- ./github_rsa:/root/.ssh/id_rsa
links:
- db
I need to mount a volume with the ssh key because there are some gems that need to be pulled from private repositories.
My Dockerfile looks like this:
FROM ruby:2.2.0
RUN apt-get update -qq && apt-get install -y build-essential \
libpq-dev \
libxml2-dev libxslt1-dev \
nodejs
ENV HOME /root
ENV APP_HOME /app
RUN mkdir $APP_HOME
RUN mkdir $APP_HOME/tmp
RUN mkdir $APP_HOME/log
# Copy the Gemfile and Gemfile.lock into the image.
# Temporarily set the working directory to where they are.
WORKDIR /tmp
ADD Gemfile Gemfile
ADD Gemfile.lock Gemfile.lock
# Copy the github key for pulling gems
COPY github_rsa /root/.ssh/id_rsa
RUN \
chown -R root:root /root/.ssh && \
chmod 700 $HOME/.ssh && \
chmod 600 $HOME/.ssh/id_rsa
RUN echo "Host github.com\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config
# Start ssh agent and add keys
RUN eval "$(ssh-agent)" && \
ssh-add && \
ssh-add -l
# Install bundler
RUN gem install bundler -v '~> 1.10'
# Install ruby dependencies
RUN bundle install
# Remove the ssh key now, we don't want to ship secrets on our images
RUN rm /root/.ssh/id_rsa
# Add app to container
ADD . $APP_HOME
# Add container working directory
WORKDIR $APP_HOME
# Expose puma port
EXPOSE 3000
You need to take this image down immediately. The following does not work:
RUN rm /root/.ssh/id_rsa
You cannot remove files like this; they will still exist in previous layers and be accessible to anyone who has the image. At the moment, you are shipping your secrets.
Regarding the actual question, I suspect it's just to do with the working directory and paths. Try moving RUN bundle install to after the WORKDIR instruction.

Resources