This builds a Docker image that is used by my book, Sustainable Web Development with Ruby on Rails.
If you just want to follow along, I suggest you pull the image from Docker Hub, set up the docker-compose.yml
file as directed and go from there.
This repo is how that image is built, and you can use this to make your own.
- Clone this repo
- Make sure you have Docker installed
- Edit
bin/vars
. In particular, you will need to modify:ACCOUNT
- account name on Docker Hub (used for naming the image only, but if you plan to push, make sure this value is accurate)REPO
- repo name for Docker Hub (again, only used for naming the image, but if you are going to push, make sure you have created this repo on Docker Hub. It does not need to be public)TAG
- tag for the image. Recommend you come up with a good scheme to avoid confusing locally.
- Edit
Dockerfile.template
anddocker-compose.yml.template
as needed to change stuff. DO NOT editDockerfile
ordocker-compose.yml
directly asbin/build
will be usingbin/vars
to make sure that those two files are consistent with one another. bin/build
will re-generateDockerfile
anddocker-compose.yml
based on the contents of the two.template
files andbin/vars
.bin/start
will start up whatever is indocker-compose.yml
.- In another terminal window,
bin/exec bash
with runbash
inside the image where Ruby and Rails are installed, effectively "logging you in" to the running container. You should be in/root/work
, running asroot
and see all the files in this repo mirrored. If you runrails new my-app
it will create a new app in this directory.
Inside the container, you can connect to Postgres like so:
> psql -Hdb -Upostgres -p5432 # password, when promted, is postgres
postgres=#
When you run Rails, you need to tell it to bind to 0.0.0.0
, so you can't just do bin/rails c
. Instead you
must:
> bin/rails c -b0.0.0.0
When you do that, your Rails app should be available to your localhost on port 9000 (or whatever value you set in bin/vars
for EXPOSE
)
I wanted to be more explicit about what's being installed.
docker-compose.yml
and Dockerfile
share some values, but Docker provides no mechanism for that. So, the files are generated.
No reason, but Docker is a more generally useful skill to have.
Yup. You can set up NFS to make it much much faster. The only problem is that Webpack dev mode doesn't work. But that's probably fine because you should using only what JavaScript you need, right?