Rails in Docker
Why install ruby locally?
- tags
- rails
- docker
- transient
Contents
In leveraging disposability for exploration we looked at how to build software without having it installed on your local computer. Lets go through how to setup and develop a rails application with this process.
docker-compose.yml all the way down
We're going to create our app by adding things to a docker-compose.yml
file as needed. Lets create the first one, which will contain our
rails container as well as a volume for keeping track of all the gems.
We are going to have a few sections:
argswhere we pass in the user_id and group_id of the user that the docker container is going to use. This should be the same as the user id in the host operation system, so files that are created by in the container in the bound volume have the right owners.volumeswhere we mount thegratitudedirectory into the container, and agratitude-gemsvolume that we use to cache the bundled gems outside of the container. This makes upgrading gems that much faster when you are rebuilding the docker container so you don't need to download them everytime.portsto expose the rails server.
docker-compose.yml:
| |
Now we build a Dockerfile to run the rails app. We base it off of the
ruby:2.7 image, add the user id, install node, and then install rails
and bundler. All other dependancies will be specified with the
Gemfile inside of the project once we create it.
| |
Now we can bring all this up by doing:
| |
This will give the error that Could not locate Gemfile or .bundle/
directory which makes sense since there's no source code. So lets
make it:
| |
This will take a bit of time to build all of the native versions.
Once this is done though, ctrl-d to exit out of the shell and try
docker-compose up again.
Go to http://localhost:3000
"Yay!", it says, "You're on Rails!"
ctrl-c to exit out.
Add a environment file
The next thing that we'll want to do is to add an environment file of
somekind. Right now we're only going to use it to store the
RAILS_MASTER_KEY, which is what is used to decrypt
config/credentials.yml.enc. We will then remove the config/master.key
file from the repo.
Look into config/master.key to find your value!
Create a file called .env:
| |
Keep this save! You should them make sure that you don't accidently check this file into the repository. Keep it safe in a password manager.
| |
Now we need to adjust the docker-compose.yml file to include this
environment variable:
| |
You can then delete the file gratitude/config/master.key.
Changing that landing page
Running commands with docker-compose run gratitude is a bit wordy, so
lets create a small bash script that will do it for us. Call it r or
something.
| |
And then a quick chmod +x r and you should be good to go.
| |
And then we can update the config/routes.rb file to use this:
| |
Adding postgres and pgadmin
Let's write up postgres into the system and create out first model.
First we need to add a couple of sections to the docker-compose.yml
file.
- Add a
postgresservice. - Add a
pgadminservice. - Make the
gratitudeservice depend uponpostgres - Add a volume to keep the database around and the
pgadminstuff around.
| |
Add the pg gem:
| |
And finally we need to tell rails where to find that database. First
we add to our .env file:
| |
Now we can create a simple model
| |
And then we can set it up and start it up:
| |
And see the glory that is http://localhost:3000/projects
Adding in redis and sidekiq
Another common set of things in the environment is redis and sidekiq.
These are both additions to the docker-compose.yml file. One is an
entry for the redis service (and it's added volume) and the other is a
another container, with the same Dockerfile as the rails app, but with
a slightly different command. Lets look at adding that now.
First we need to add some gems
| |
Lets configure sidekiq and the redis cache in config/initializers/sidekiq.rb:
| |
And in our good old .env, point to our new fancy redis server:
| |
And the add everything to docker-compose.yml:
| |
And if you want to have a nice sidekiq admin, add the following to your config/routes.rb file:
| |
Finally
And when you are done with whatever you are doing:
| |
Everything but the volumes are removed. If you really want to get
aggressive you can docker system df -v which will show you everything
that's on your system, and you can blow everything away (less the
volumes) but using docker system prune --all – be sure to read the
documentation first!.
Previously
Next