How to generate Rails migration with Docker - ruby-on-rails

I'm trying to generate a migration for my Rails 4 project running within Docker.
What I've tried so far is
docker-compose run web rails g migration migration_name
docker-compose exec web rails g migration migration_name
Terminal keeps saying me that everything is ok, the migration was successfully created and all. But i don't see migration file in my project.
However if I check the project files in Docker
docker-compose exec web bash
ls -l db/migrate/
I see that the file is actually there.
I'm on Mac OS X if it could help

You should look into making your root app directory a volume on your host machine. Documentation on volumes.
In your docker-compose.yml, you can add your root volume using:
volumes:
- .:/YOUR-APP-NAME

When mounting properly it still took a minute or two to show up in my IDE

Related

With Docker, why does it seem like a container's settings are saved despite deleting the container?

Please bear in mind, that I'm new to Docker.
I'm using a Mac.
When I followed this official documentation https://github.com/docker/awesome-compose/tree/master/official-documentation-samples/rails/, it all seemed to work, and so after testing that, I created a new directory and followed the example while changing things to incorporate other things. I was making changes to the official documentation with some things shown in this link, https://tihandev.com/how-to-integrate-elasticsearch-with-ruby-on-rails/
But this time when I try to run
docker compose run --no-deps web rails new . --force --database=postgresql
It gave me the error
No Rakefile found
So I was trying to debug what change would cause this and kept backtracking the changes, and even when I backtracked to make all the files be the same as the official documentation that had worked, I still get the "No Rakefile found" error.
Funny thing is that even after I delete Gemfile.lock and recreate it, the Gemfile.lock file gets populated instantly without any console logs about installing dependencies. So clearly, this information is saved somewhere else that I don't know.
Even after I go to the "Docker Destop" GUI client, and click "Delete" for that container among the containers listed, and then delete the Gemfile.lock file, and then rerun that command, it gives the same output as if some previous erroneous state is stored somewhere else.
Can someone please help me with what is going on here?
For a clarification on this question, even though I really appreciate answers on why doing a docker compose run wouldn't work for the first time because that is an information I need as well, my main confusion here is on why even after deleting problematic sections and after returning to what had worked before, I still get the same problem.
Edit 1: Steps to reproduce
These are the steps to reproduce my case.
Follow exactly the same as https://github.com/docker/awesome-compose/tree/master/official-documentation-samples/rails/ except for entrypoint.sh. Modify entrypoint.sh as below
#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid
bundle check || bundle install
bundle exec rake db:create
bundle exec rake db:migrate
bundle exec rake db:seed
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$#"
Run docker compose run --no-deps web rails new . --force --database=postgresql
This would produce a message that says "No Rakefile found"
Change the entrypoint.sh file to be the same as how it was in the link.
Delete Gemfile.lock and touch Gemfile.lock
(At this point I doublecheck that every file is the same as how it is in the link)
Run docker compose run --no-deps web rails new . --force --database=postgresql
You still get "No Rakefile found"
Go to Docker Desktop and press on the trash bin icon to delete the container that had been created due to calling docker compose run
Run docker compose run --no-deps web rails new . --force --database=postgresql again, and still get No Rakefile found.
Again, note that if I had followed the link exactly from the beginning, it runs fine.
I really wish that you had been clear about the change that you made to entrypoint.sh in your original post. I spent a lot of time attempting to answer your original question -- before the edit -- with an answer that was 500 lines long with detailed steps. I could have answered your question in 15 minutes if you had been clear about what changes you made.
But it turns out that the problem is that you're editing entrypoint.sh and telling it "you should expect to find a Rails app in this directory" before you've created a Rails app in that directory.
These commands are the problem:
bundle exec rake db:create
bundle exec rake db:migrate
bundle exec rake db:seed
These commands expect:
There is a bundler-managed Ruby app in this directory
Its dependencies include the rake gem
There is an accompanying Rakefile in the directory with a configuration that can be understood by bundle exec rake
There are additional rake tasks defined by the Ruby app in this directory that explain how to execute the commands db:create db:migrate db:seed
None of that exists until after you create the Rails app.
Follow the instructions in the repository that you linked to first. Don't edit entrypoint.sh before you've completed those steps because it cannot and will not work the way you expect for the reasons I've described.

Run `rails c` on GCloud instace with appengine gem

I have a Rails 6.0.0.rc1 application (with the appengine gem install) that I deployed to GCP. Is there a way to log into a remote rails console on the instance that runs the application? I tried this:
bundle exec rake appengine:exec -- bundle exec rails c
which gives the following output:
...
---------- EXECUTE COMMAND ----------
bundle exec rails c
Loading production environment (Rails 6.0.0.rc1)
Switch to inspect mode.
...
so apparently it executed the command, but closes the connection right after.
Is there an easy way to do this?
As reference: On Heroku this would simply be:
heroku run rails c --app my-application
There's a few steps involved:
https://gist.github.com/kyptin/e5da270a54abafac2fbfcd9b52cafb61
If you're running a Rails app in Google App Engine's flexible environment, it takes a bit of setup to get to a rails console attached to your deployed environment. I wanted to document the steps for my own reference and also as an aid to others.
Open the Google App Engine -> instances section of the Google Cloud Platform (GCP) console.
Select the "SSH" drop-down for a running instance. (Which instance? Both of my instances are in the same cluster, and both are running Rails, so it didn't matter for me. YMMV.) You have a choice about how to connect via ssh.
Choose "Open in browser window" to open a web-based SSH session, which is convenient but potentially awkward.
Choose "View gcloud command" to view and copy a gcloud command that you can use from a terminal, which lets you use your favorite terminal app but may require the extra steps of installing the gcloud command and authenticating the gcloud command with GCP.
When you're in the SSH session of your choice, run sudo docker ps to see what docker containers are presently running.
Identify the container of your app. Here's what my output looked like (abbreviated for easier reading). My app's container was the first one.
jeff#aef-default-425eaf...hvj:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND NAMES
38e......552 us.gcr.io/my-project/appengine/default... "/bin/sh -c 'exec bun" gaeapp
8c0......0ab gcr.io/google_appengine/cloud-sql-proxy "/cloud_sql_proxy -di" focused_lalande
855......f92 gcr.io/google_appengine/api-proxy "/proxy" api
7ce......0ce gcr.io/google_appengine/nginx-proxy "/var/lib/nginx/bin/s" nginx_proxy
25f......bb8 gcr.io/google_appengine/fluentd-logger "/opt/google-fluentd/" fluentd_logger
Note the container name of your app (gaeapp in my case), and run container_exec bash.
Add ruby and node to your environment: export PATH=$PATH:/rbenv/versions/2.3.4/bin:/rbenv/bin:/nodejs/bin
cd /app to get to your application code.
Add any necessary environment variables that your Rails application expects to your environment. For example: export DATABASE_URL='...'
If you don't know what your app needs, you can view the full environment of the app with cat app.yaml.
bin/rails console production to start a Rails console in the Rails production environment.

Rails app accidentally installed in root folder

I accidentally created a Rails app in my root directory. I ran the following command and got an error:
rails new test_app
Can't initialize a new Rails application within the directory of another, please change to a non-Rails directory first.
So I tried running
bundle exec rails server
and it looks like the server is listening on port 3000.
How do I remove this app from my root directory?
Just delete the folder or you can execute this command
rm -rf test_app
Hoping you're on *nix based system.
Stop the running server using:
kill -9 $(lsof -t -i :3000)
Change working directory to root using:
cd /
or wherever you're defining your root as.
Remove the accidentally created Rails application directory using:
rm -fr rails_project_in_root_dir
where rails_project_in_root_dir is the Rails application directory in your root directory.
Okay, I'm not sure how it got installed in the root directory, but I ended up just manually deleting all the folders and files that get created when you run
rails new app
Here's where I found the list of files:
http://guides.rubyonrails.org/getting_started.html
That ended up resolving the issue. Thanks for the help guys.

Use zeus with Rails 3 and docker

We have a Rails 3.2.9 app, and recently switched to Docker in development. By now, I've always used zeus local on my machine to preload my codebase and execute tests with Rspec faster.
But how would you achieve this with docker? When I try to install zeus inside my container with gem install zeus and start it with zeus start I get
Unable to accept socket connection.
It looks like Zeus is already running. If not, remove .zeus.sock and try again.
And there is a .zeus.soc (notice the missing k at the end) left in my filesystem.
Has anybody got this working with Docker?
Apparently zeus is not able to create the .zeus.sock file on the vboxsf filesystem used by VirtualBox for sharing a volume with the host. So a solution is to tell Zeus explicitely to create the file somewhere else by setting the ZEUSSOCK environment variable. This is discussed here: https://github.com/burke/zeus/issues/488

Passing commands to deployed Ruby on Rails App in Bluemix

Finally, with the help of SO member, JeffSloyer, I was able to deploy my RoR app on to bluemix. There seems to be an additional problem with the RoR app. I cant login as admin in this app.
http://csw-events.mybluemix.net/sign_in
The question here is not about the app itself, I have found a solution from the forum dedicated to this RoR app(Currently, in-active) -> SOLUTION.
The question is
1: Can I pass commands to an already deployed app on Bluemix using CF something like this
cf -c "User.last.update_attribute(:admin, true)"
If not, What are the alternatives for passing such commands
As for this eg it is
bundle exec rails console
User.last.update_attribute(:admin, true)
You can not pass commands to an already running CF application.
You can have the buildpack run commands for you at the start of the application by creating a manifest.yml file at the root of your application and specifying the command.
Sample manifest.yml:
---
applications:
- name: my-rails-app
command: bundle exec rake cf:on_first_instance db:migrate && bundle exec rails s -p $PORT -e $RAILS_ENV
Tips for Ruby Developers
You could also push the app again and add a command with the -c option:
cf push -f manifest.yml -c "User.last.update_attribute(:admin, true)"
It would mean some downtime but only a very limited amount of time.
If the app does not run anymore with a consequent push, run the same cf command with -c "null", Cloud Foundry is a bit buggy that way.
If it is only a once off command you want to pass this would be the recommended way, instead of putting it in the manifest file.

Resources