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

291 Views Asked by At

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.

1

There are 1 best solutions below

0
Keimille On

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