app changes not reflecting while runnning docker image - ruby-on-rails

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

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

Cannot COPY from previous stage in DockerFIle

This looks like a common issue so I checked a few SO posts but none of them solved my problem.
Here is my Dockerfile:
# MkDocs container
FROM python:3-alpine AS build-env
RUN apk add bash
RUN pip install --upgrade pip
RUN pip install pymdown-extensions \
&& pip install mkdocs \
&& pip install mkdocs-material \
&& pip install mkdocs-rtd-dropdown \
&& pip install mkdocs-git-revision-date-plugin \
&& pip install mkdocs-git-revision-date-localized-plugin \
&& pip install mkdocs-redirects
# executed at ~/Developer/MkDocs
RUN mkdir -p /home/mkdocs/
WORKDIR /home/mkdocs/
COPY . .
RUN mkdocs build -s
WORKDIR /
# Nginx container
FROM nginx:1.21.6-alpine
RUN apk add bash
EXPOSE 80
RUN cat /etc/nginx/nginx.conf
COPY nginx.conf /etc/nginx/nginx.conf
WORKDIR /
RUN mkdir -p /home/mkdocs
COPY --from=build-env /home/mkdocs/site/ /home/mkdocs/
RUN mv /home/mkdocs/* /usr/share/nginx/html/
RUN chown nginx:nginx /usr/share/nginx/html/*
USER nginx:nginx
And here is the command to run docker:
docker run -it --name mkdocs -p 8789:80 nginx
Running localhost:8789 only shows the default nginx homepage, not the built one of MkDocs. I also run docker exec -it --user root <PID> bash to check the directory /usr/share/nginx/html/ but the copied files are not there.
My other checks:
First, I'm 100% sure that the files built in the first stage works and exists
Second, this is what completely frustrated me out. If I run the docker using docker run -it --entrypoint=/bin/bash mkdocs:v1, I can actually see the built MkDocs files:
bash-5.1$ ls /usr/share/nginx/html
404.html 50x.html assets index.html search sitemap.xml sitemap.xml.gz
bash-5.1$

Sphinx/Searchd: Supervisord autorestart not working

We are running sphinx through supervisord inside docker. We are trying to enable autorestart, but it does work. We are trying to verify this by manually killing the searchd process.
Below is the configuration
[program:sphinx]
command=searchd --pidfile --config "config/sphinx.conf"
autostart=true
autorestart=unexpected
startsecs=5
startretries=3
exitcodes=0
We also attempted other configuration
program:sphinx]
command= rake ts:stop
command= rake ts:configure
command= rake ts:start
autostart=true
exitcodes=0,2
autorestart=unexpected
Are we missing something?
Dockerfile
FROM ruby:2.3
RUN apt-get update
#Install Sphinx
RUN wget -P /tmp http://sphinxsearch.com/files/sphinx-2.3.2-beta.tar.gz
RUN mkdir /opt/sphinx_src
RUN tar -xzvf /tmp/sphinx-2.3.2-beta.tar.gz -C /opt/sphinx_src
WORKDIR /opt/sphinx_src/sphinx-2.3.2-beta
RUN ./configure --with-pgsql --with-mysql
RUN make
RUN make install
RUN apt-get install -q -y supervisor cron
ADD supervisor-cron.conf /etc/supervisor/conf.d/cron.conf
RUN service supervisor stop
RUN apt-get install -y net-tools telnet
WORKDIR /opt/app
ADD start.sh /usr/local/sbin/start
RUN chmod 755 /usr/local/sbin/start
EXPOSE 9312
CMD ["/usr/local/sbin/start"]
start.sh
#!/bin/sh
export set BUNDLE_GEMFILE=Gemfile
cd /opt/app
bundle install
echo "About to perform Sphinx Start"
cp /opt/app/supervisor-sphinx.conf /etc/supervisor/conf.d/sphinx.conf
supervisord -n

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?

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

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.

Resources