Docker web image does now work with rspec gem - ruby-on-rails

I'll jump straight into the problem.
I have installed a rspec gem for testing(add rspec gem, then bundle install and rails g rspec:install). However when I run docker-compose up to run my app with docker a web image does not work. Here is a log message returned from that image:
web_1 | Could not find diff-lcs-1.4.4 in any of the sources
web_1 | Run `bundle install` to install missing gems.
My Dockerfile.web is defined bellow:
FROM ruby:2.6.5
ENV BUNDLER_VERSION=2.1.4
# Set Rails to run in production
ENV RAILS_ENV production
ENV RACK_ENV production
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client libpq-dev yarn
RUN gem install bundler -v 2.1.4
RUN mkdir /my_app
WORKDIR /my_app
COPY Gemfile /my_app/Gemfile
COPY Gemfile.lock /my_app/Gemfile.lock
RUN bundle install --without development test
COPY . /my_app
# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
# Start the main process.
RUN bundle exec rake SECRET_KEY_BASE=<token> assets:precompile
CMD bundle exec puma -C config/puma.rb
Dockerfile:
FROM ruby:2.6.5
ENV BUNDLER_VERSION=2.1.4
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client libpq-dev
RUN gem install bundler -v 2.1.4
RUN mkdir /my_app
WORKDIR /my_app
COPY Gemfile /my_app/Gemfile
COPY Gemfile.lock /my_app/Gemfile.lock
RUN bundle install
COPY . /my_app
# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
RUN bundle exec rake assets:precompile
CMD ["rails", "server", "-b", "0.0.0.0"]
I have diff-lcs gem installed so I don't know why I get this error.If I remove rspec gem then all docker images work fine.
I would appreciate any help.
Thanks in advance.

Rebuild that image, It will run bundle install and all will work fine.

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

Different conditionals on the same Dockerfile

I have a Dockerfile with some commands I would like to use conditionally:
FROM + image_name (I have a M1 chip MacOS so I need to add --platform=linux/amd64 to it but I want to deploy in a AWS EC2 linux instance that doesn't need it)
On production I would like to run my project with nginx so I want the Dockerfile to end with this RUN mkdir -p tmp/sockets. But for testing, I have no need of the nginx so I would like my Dockerfile to end with this
# Expose port
EXPOSE 3000
# Start rails
CMD ["rails", "server", "-b", "0.0.0.0"]
I thought of using the multi stage dockerfile to solve the FROM image problem but the Dockerfile resulting is quite lengthy since it is basically the same except for the FROM image part.
For the nginx part I wanted to use a shell script but I am not sure how to write the exposing port and final command to start rails.
These are the files:
run_dockerfile.sh
#!/bin/bash
if [ ${RUN_DOCKERFILE} = "PROD" ]; then
mkdir -p tmp/sockets
else
????
fi
My Dockerfilelooks like this:
# Start from the official ruby image
# To run Dockerfile with arm64 architecture (M1 chip MacOS for example)
FROM --platform=linux/amd64 ruby:2.6.6 AS ARM64
# Set environment
ARG BUILD_DEVELOPMENT
# if --build-arg BUILD_DEVELOPMENT=1, set RAILS_ENV to 'development' or set to null otherwise.
ENV RAILS_ENV=${BUILD_DEVELOPMENT:+development}
# if RAILS_ENV is null, set it to 'production' (or leave as is otherwise).
ENV RAILS_ENV=${RAILS_ENV:-production}
# Update and install JS & DB
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
# Create a directory for the application and use it
RUN mkdir /myapp
WORKDIR /myapp
# Gemfile and lock file need to be present, they'll be overwritten immediately
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
# Install gem dependencies
RUN gem install bundler:2.2.32
RUN bundle install
RUN curl https://deb.nodesource.com/setup_12.x | bash
ADD https://dl.yarnpkg.com/debian/pubkey.gpg /tmp/yarn-pubkey.gpg
RUN apt-key add /tmp/yarn-pubkey.gpg && rm /tmp/yarn-pubkey.gpg
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update -qq && apt-get install -y yarn && apt-get install -y npm
RUN yarn add bootstrap
COPY . /myapp
# So that webpacker compiles
RUN yarn config set ignore-engines true
RUN rm -rf bin/webpack*
RUN rails webpacker:install
RUN bundle exec rails webpacker:compile
RUN bundle exec rake assets:precompile
# This script runs every time the container is created, necessary for rails
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
# Run run_dockerfile.sh
COPY run_dockerfile.sh run_dockerfile.sh
RUN chmod u+x run_dockerfile.sh && ./run_dockerfile.sh
##################################################
# Start from the official ruby image
# To run Dockerfile without arm64 architecture
FROM ruby:2.6.6 AS AMD64
# Set environment
ARG BUILD_DEVELOPMENT
# if --build-arg BUILD_DEVELOPMENT=1, set RAILS_ENV to 'development' or set to null otherwise.
ENV RAILS_ENV=${BUILD_DEVELOPMENT:+development}
# if RAILS_ENV is null, set it to 'production' (or leave as is otherwise).
ENV RAILS_ENV=${RAILS_ENV:-production}
# Update and install JS & DB
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
# Create a directory for the application and use it
RUN mkdir /myapp
WORKDIR /myapp
# Gemfile and lock file need to be present, they'll be overwritten immediately
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
# Install gem dependencies
RUN gem install bundler:2.2.32
RUN bundle install
RUN curl https://deb.nodesource.com/setup_12.x | bash
ADD https://dl.yarnpkg.com/debian/pubkey.gpg /tmp/yarn-pubkey.gpg
RUN apt-key add /tmp/yarn-pubkey.gpg && rm /tmp/yarn-pubkey.gpg
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update -qq && apt-get install -y yarn && apt-get install -y npm
RUN yarn add bootstrap
COPY . /myapp
# So that webpacker compiles
RUN yarn config set ignore-engines true
RUN rm -rf bin/webpack*
RUN rails webpacker:install
RUN bundle exec rails webpacker:compile
RUN bundle exec rake assets:precompile
# This script runs every time the container is created, necessary for rails
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
# Run run_dockerfile.sh
COPY run_dockerfile.sh run_dockerfile.sh
RUN chmod u+x run_dockerfile.sh && ./run_dockerfile.sh
Is there any way I could do the .sh or are there any recommendations on the proper way to do it? Thank you!
From the way you've described the problem, you don't really need very many special cases at all.
The one important detail is that it's very easy to override the image's CMD when you run a container. If you have two Compose files, for example, you can just set the service's command:
# docker-compose.yml
version: '3.8'
services:
myapp:
image: registry.example.com/myapp:${MYAPP_TAG:-latest}
ports: ['3000:80']
# docker-compose.override.yml
# for developer use
version: '3.8'
services:
myapp:
build: .
command: rails server -b 0.0.0.0 -p 80
The other variations you list shouldn't matter. You should get consistent results if you build your image FROM --platform=linux/amd64 on an x86-64 host, explicitly specifying the native platform; RUN mkdir a directory you won't use is harmless. The one inconsistency seems to be the container port, but you can explicitly tell rails server which port to use so it matches. I'd use the same image in all environments.
FROM --platform=linux/amd64 ruby:2.6.6 # even on an Intel/AMD host system
...
RUN mkdir tmp/sockets # even if it's unused
CMD ["nginx", "-g", "daemon off;"] # can be overridden when the container runs

Rails run with puma in dockerfile and enable https

This is an example of a Dockerfile for ruby on rails project.
I want to run it with ssl key and cert
RUN apt-get update -yqq && apt-get install -y npm && npm install -g yarn
RUN apt-get install -y ruby-full
RUN apt install libsqlite3-dev
RUN gem install rails --version=6.1
RUN mkdir /var/app
WORKDIR /var/app
COPY . .
RUN bundle install --without development test
RUN npm install --force
RUN export SECRET_KEY_BASE=...............................
EXPOSE 3000
EXPOSE 80
EXPOSE 443
EXPOSE 8443
RUN rails webpacker:install
#RUN rake assets:clean
RUN rake assets:precompile
CMD rails s -b 0.0.0.0 -e production
FROM ruby:3.0.2
RUN apt-get update -yqq && apt-get install -y npm && npm install -g yarn
RUN apt-get install -y ruby-full
RUN apt install libsqlite3-dev
RUN gem install rails --version=6.1
RUN mkdir /var/app
WORKDIR /var/app
COPY . .
RUN bundle install --without development test
RUN npm install --force
RUN export SECRET_KEY_BASE=...............................
EXPOSE 3000
EXPOSE 80
EXPOSE 443
EXPOSE 8443
RUN rails webpacker:install
#RUN rake assets:clean
RUN rake assets:precompile
CMD puma -b 'ssl://0.0.0.0:8443?key=yourkey.key&cert=yourcertificat.pem&verify_mode=none' -b 'tcp://0.0.0.0:80' -e production
In config/environment/production.rb add: config.force_ssl=true
You can you freessl to get ssl key and certificat for your domain;
https://freessl.org/
To run it on you virtual machine or instance: docker run -d -p 443:443 -p 80:80 yourimage:tag

docker compose exec user process caused: no such file or directory

I have dockerized rails application. My environment is Windows 10 and i have already instlall docker. I am trying to build the project and it is fine. But when i am trying to
docker-compose run api rails db:create
it caused error like this
standard_init_linux.go:219: exec user process caused: no such file or directory
i already try to change from CLRF to EOL by using vscode.
but the error still same.
i already use dos2unix but the problem still same
this is my dockerfile
FROM ruby:2.6.5
RUN apt-get update -qq && apt-get install -y nodejs libmagickwand-dev
RUN apt-get install -y imagemagick --fix-missing
RUN mkdir /nectico
WORKDIR /nectico
COPY Gemfile /nectico/Gemfile
COPY Gemfile.lock /nectico/Gemfile.lock
RUN gem update --system && gem install bundler -v 1.17.3 && bundle install
COPY . /nectico
# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]
how to solve this?
thanks
maybe someone will need it.
stop and remove the container and i delete the project
type git config --global core.eol lf
type git config --global core.autocrlf input
clone the project again
build the image again
You copy sh file to /usr/bin but set workdir /nectico. So it try to find in that path. Just use ENTRYPOINT ["/usr/bin/entrypoint.sh"] and use docker run commands.

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.

Resources