"Problems" creating an image with Docker - ruby-on-rails

I have a somewhat peculiar situation involving Docker and Ruby on Rails.
I'm creating images of a Ruby on Rails project, the problem would be that the images are getting too big.
The project in question is a monolith that will become a microservice.
I intend to use the images in a Kubernetes cluster and due to the size of the images this can be detrimental to the K8S and the deployment time
Using smaller official Ruby images like Slim-Buster and Alpine, the images created by me were not so small, reaching approximately 600MB.
The main reason is due to the mandatory execution of the bundle install command.
To execute the "bundle exec puma" command, it first requests the execution of the "bundle install".
The /usr directory is the biggest "underdog" after running the bundle install.
I tried to get around this situation by placing the /usr directory with the Gems already installed on my local host and then mounting it in the container. Even so, the message "Install missing gem executables with bundle install" is reported after attempting to execute the" bundle exec puma "command.
Could you give me any tips on how to get around this situation with this project?
I'm avoiding reformulating the whole project to be migrated to microservices at this point, but I need some advice.
Thank you very much in advance

The most obvious advice would be to remove the unnecessary folders like spec (if you don't run tests over your docker image), node_modules and app/assets because you don't need them in production since they are normally precompiled.
Answers could be much more helpful if you post your Dockerfile :)

Related

Deploying Ruby on Rails apps to Railway

I hope you are all well!
For several days now I have been trying to deploy a simple rails app to Railway.app, and failing catastrophically and repeatedly to get it to run.
Here is the github repo:
https://github.com/CaffieneSage/blogApp-rails-
The error I am getting is during the deploy step specifically:
bundler: not executable: bin/rails
I have successfully deployed apps to heroku in the past. I suspect there is something simple that I am missing. I have tried rerolling and deploying the default rails app to simplify things. I have made sure to us postgres instead of SQLite3 as the db. I have spun up an instance of postgres on railway and tried to set my environment variables to point to it. I have had a go within the CLI as well.
Thanks in advance for any advice you may have to offer!
This is my first post on stack overflow, please go easy on me ;]
The issue likely stems from the script bin/rails not having the executable bit on the file.
You can see the file permissions using ls:
ls -l bin/
All of the files will display:
-rw-r—r—
These need to have the executable bit set, so you can run something like:
chmod +x bin/*
After which all the files should have this permission set:
-rwxr-xr-x
Don’t forget to commit the changes.
Read more on file permissions: https://en.wikipedia.org/wiki/File-system_permissions

`rails console` hangs when using vendored gems (running in docker)

I'm running into a strange issue trying to dockerize a (fairly large) rails app. I'm using the official ruby docker image for now, and mounting my code into the app. Using:
docker pull ruby:2.2.4
docker run -it -v $PWD:/code ruby:2.2.4 bash
Then in the container I ran:
cd /code
bundle install
rails c
This works fine (I can't connect to the database obviously since I didn't link it, but rails console otherwise runs normally and loads my app).
However, when I instead run bundle install --deployment, then running rails c just hangs forever.
Any idea what could cause this?
I was hoping to use a local copy of gems, because we're also using a bunch of npm modules (which install locally into node_modules) so I figured keeping gems in the local directory as well is the most straightforward and has the same persistence between docker runs in the development environment.
Nevermind I figured it out - it's not completely hanging just going really really slow thanks to Virtualbox's default type of shared drive. The code is on my OSX filesystem, mounted as a shared drive into Virtualbox (using vagrant), and then mounted as a volume into docker. When the gems are installed into vendor/bundle they all have to be loaded from this slow volume and there are ~150 gems total with all the dependencies.
root#fa575694f86a:/code# time echo puts :ok | rails c
Loading development environment (Rails 4.2.1)
Switch to inspect mode.
irb: warn: can't alias context from irb_context.
puts :ok
ok
nil
real 2m0.937s
user 0m2.574s
sys 0m59.985s
So it takes 2 whole minutes just to launch rails. I changed vagrant to use an NFS shared volume, and that brings the launch time down to ~12 seconds (literally 10x faster, I'd heard of NFS being 2x faster but this is pretty crazy). Using just bundle install to install the gems globally there's no penalty, it's the usual 3-4 second rails load time which is the same thing I get running in OSX.

Booting up Sidekiq with Upstart

My goal is to have sidekiq start when the server boots up (I'm using EC2 with an auto-scaling group). I know there are a few other posts regarding getting sidekiq to start with upstart on boot, but I don't believe mine has been addressed specifically.
I'm using this wiki - https://github.com/mperham/sidekiq/tree/master/examples/upstart/manage-many and have placed the scripts inside /etc/init/sidekiq.conf and /etc/init/sidekiq-manager.conf.
I've made a couple small modifications as directed in /etc/init/sidekiq.conf, changing:
# setuid apps
# setgid apps -> replaced apps with ubuntu in both lines, which is the deployment user.
export HOME=/home/apps to export HOME=/home/ubuntu
I also have a /etc/sidekiq.conf that includes the following line:
/home/ubuntu/app_dir, 2
Otherwise, these scripts are identical to those included in the referenced repo. I'm getting the following errors in my logs (/var/log/upstart)
/bin/bash: line 19: cd: 2: No such file or directory
Could not locate Gemfile
It appears as if it's attempting to change directory somewhere other than /home/ubuntu/app_dir, at which point it's in the wrong directory and cannot find my Gemfile.
Is there somewhere else I need to specify a correct path to my app directory?
Thanks!
You can run sidekiq as an upstart job. Making a sidekiq.conf file in /etc/init/ directory and put the upstart code to run sidekiq.
Here is the complete script and the guide to make sidekiq upstart job.
After making this job, sidekiq start/stop/restart would be easy with sudo service command.

Grab Ruby on Rails files and install on local machine

general question that can be used in many different situations, so I thought it would be interesting to ask.
I'm semi new to Ruby and am learning from Treehouse. I am doing the social media site project, and am about half way through.
I was hoping to set up a separate installation from the source files they give you, of the completed site, to do a simple compare and contrast, and really I am just curious as to what the final product is gonna be like.
My question is, is there an easy way to just grab their files, install all the gems and dependencies and run the rails server. If I just try to run the server on the folder, I get a bunch of errors about Gems not being installed and such, which is expected.
If anyone has a process they use when doing things like migrated entire environments from one location to another, it would be appreciated!
Go to project directory.
Install all of the required Gems by executing
bundle install
Create database by executing
rake db:create
Then migrate the database by executing
rake db:migrate
And finally run the application using
rails s
If you have cloned their repository, or copied the files to a folder, try running
bundle install
from the command line

Rails sprocket Error compiling CSS assest - CHOWN issue

I've been breaking my head over this for over 2 days now, and can't seem to figure out where the problem is. This is mostly a problem related to my setup, but if anyone could help me find a solution to this problem, then I'll be his slave for eternity..!
I'm running Snow leopard(host) and VMware fusion running Ubuntu Lucid server (guest OS). Somehow after a lot of head-banging I managed to get my workspace shared from Mac to Ubuntu. So, essentially I've been able to isolate my dev environment on a VM, which makes it easy for the entire dev team to be on the same environment irrespective of their host OS / setting, etc. While, it certainly has made everything easy, I'm stuck with a specific problem occurring due to sprockets (I guess). Every time I make changes to my stylesheets and reload, I get this error:
Error compiling CSS asset
Errno::EPERM: Operation not permitted - /mnt/hgfs/banjarey/tmp/cache/assets/DCF/780/sprockets%2F5f78b3457def1d02bd3fb75d4e0cfb63
/home/coderboy/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/core_ext/file/atomic.rb:38:in `chown'
The strage thing though is that if I reload the page twice or thrice, the css compiles and everything works alright.
It's a real shame to be reloading the page twice or thrice for every single css change I make. I've played around with memberships and permissions a little bit, but been an amateur I've not ventured deep enough I guess.
Any idea folks ??
I have the same problem. my solution is to modify the File Permission after precompile assets:
#chown apache.root yourapp -R
#chmod 755 yourapp -R
A bit late with this answer, but chmod everytime the app is deploy does not seem to be a good solution. I think the issue is your apache instance and your passenger instance are running as different users. So the solution is to check what user your apache server is running as and set the PassengerDefaultUser,
For example:
PassengerDefaultUser www-data
This worked for me. As Passenger was making files only readable by nobody, weird is that if you just refresh a few times it some how works out, but making sure they are the same user stops this error.
I experienced the same error, but in development!
Solved after clean and recompile assets:
rake assets:clean && rake assets:precompile
Make sure the underlying file system supports the requested operation.
The following commands fixed it for me.
mv public/assets /tmp
ln -s /tmp/assets public/assets

Resources