Zero to Up and Running a Rails Project only using Docker
Disclaimer: this post is based on Quickstart: Compose and Rails — Docker the main difference is that I will run everything with my own user and cache the gems installations.
First, create the directory for your project:
% mkdir ~/my-blog
% cd ~/my-blog
Now create the Dockerfile
:
And the docker-compose.yml
:
Build it:
% docker-compose build
Create the Gemfile
:
Install your gems:
% docker-compose run --rm -u root web bash -c "mkdir -p /bundle/vendor && chown -R railsuser /bundle/vendor"
% docker-compose run --rm web bundle install --path /bundle/vendor
Build the project:
% docker-compose run web bundle exec rails new . --force --skip-bundle
Run bundle install
again:
% docker-compose run --rm web bundle install
Bonus: Change the Project to use PostgreSQL
Edit Gemfile
removing the gem sqlite3 and adding pg.
Run bundle install
to install the gem pg:
docker-compose run --rm web bundle install
Change the Dockerfile
to use PostgreSQL:
Build it again:
% docker-compose build
Change docker-compose.yml
to also run PostgreSQL:
Update the configuration for the database editing the file config/database.yml
like this:
Create your database, migrate and/or seed it:
docker-compose run --rm web bundle exec rake db:create
docker-compose run --rm web bundle exec rake db:migrate
docker-compose run --rm web bundle exec rake db:seed
#protip: you can execute rails db:setup
to run altogether.
Now your project is ready to run your app with PostgreSQL.
At last but not least important: Document your up and running process
Remember: the process is now partially automated due to the bundler cache, so everyone will need to run these commands to setup the project:
% docker-compose run --rm -u root web bash -c "mkdir -p /bundle/vendor && chown railsuser /bundle/vendor"
% docker-compose run --rm web bundle install
And, of course:
% docker-compose run --rm web bundle exec rake db:setup
And to run:
% docker-compose up web
You can create a README like this:
Or even a bin/setup
:
Conclusion
Like I said in How to install Docker and docker-compose I have nothing in my machine itself.
But this is only one way to solve this problem. If you know any other way to use it (or if you prefer another) please comment below :)