Imagine you’ve just started working on a rails project with the following docker-compose file:
(Note: if you’re still getting started with docker check out this guide on how to get started on a rails project using docker)
To get started we’d need to run:
docker-compose run --rm web bundle install
docker-compose run --rm web rake db:create
docker-compose run --rm web rake db:migrateThat seems a bit repetitive and not very noob-friendly.
This doesn’t stop with the project setup, if we want to use the rails generator we have to do it like this:
docker-compose run --rm web rails generate resource FooOne solution I’ve been using to make it easier is to create a binstub for bundler or any other bins I need to use that rely on docker. And this is how you can do it.
Create a folder in your project called docker-bins/ and put a file named rails with the following <sidenote> I initially had it in the bin/ folder but that broke an app in heroku, if you’re in that scenario and rather use bin/ you can just add that folder to you’re .slugignore file </sidenote>:
Give it run permissions with chmod +x docker-bins/bundle and voila.
Now you can run:
docker-bins/bundle installAnd it will spin up a docker image run bundle installcommand and then destroy the instance.
If you want to make it feel even more natural you can just add docker-bins to your $PATHand run:
bundle installExtra: If you’re sure everyone in the project is familiar enough with docker you can use docker-compose execinstead of docker-compose run, this will run the command in an already running image which is faster, but you have to make sure the images are already running.
Have you used docker before? Did you find it annoying to have to run a whole line in the command line for a simple bundle install? Comment below!
At Runtime Revolution we use docker to setup our devolopment enviroments on new machines to make the onboarding on new projects qucik and painless.
Runtime RevolutionWe are Rails, mobile and product development experts. We can build your product or work with you on your project.www.runtime-revolution.com