2

I need to run multiple Ruby commands inside docker. What I normally do from SSH is:

docker exec -it containername bundle exec rails c

And then run my command. In this case:

SharedAccess.create(room_id: 56, user_id: 2)

The thing is I need to run multiple commands with different user_id's.

SharedAccess.create(room_id: 56, user_id: 3)
SharedAccess.create(room_id: 56, user_id: 4)
SharedAccess.create(room_id: 56, user_id: 5)

What I normally would do in shell is creating a script, pasting all the lines one below the other in there and then just run it. I want to find a way to create something like a script with all the lines in there and then run it from rake (inside docker).

3
  • 4
    you can just put the code lines you want to run inside a rake task, there doesn't need to be another script. Commented Aug 4, 2020 at 22:10
  • Ok that actually works! Thank you. And what about making it a script? Commented Aug 5, 2020 at 4:05
  • 1
    Definitely put it in a task and run like docker exec -it containername bundle exec rake migrate:shared_access . Commented Aug 5, 2020 at 7:26

1 Answer 1

3

create a file somefile.rake in your lib/tasks directory:

namespace :migrate do
  desc "create some SharedAccess"
  task shared_access: :environment do
    SharedAccess.create(room_id: 56, user_id: 3)
    SharedAccess.create(room_id: 56, user_id: 4)
    SharedAccess.create(room_id: 56, user_id: 5)
  end
end

and now you can run docker exec containername bundle exec rake migrate:shared_access

Sign up to request clarification or add additional context in comments.

3 Comments

Rake tasks usually have a .rake extension even if they are just Ruby. This one should probably be called migrate.rake so it makes sense which task(s) it defines.
Thank you @Spikie but I am very new into Ruby / Docker. How should I create the somefile.rake in lib/tasks inside Docker?
There are a lot of ways to do this. 1.The simplest way is get a bash shell in container: docker exec -it containername sh and run vim lib/tasks/somefile.rb. 2.If your container is generated by Dockerfile.you can create somefile.rake in your project,and run docker build . in app root directory,and then somefile.rake should exist in your container.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.