This is the dev environment used in the book Sustainable Web Development with Ruby on Rails. It's here primarily to allow readers to follow along.
- Ensure you have Docker installed
- Clone this repo
dx/build- this will build a Docker image you will use to start a container in which development can happendx/start- this will use Docker Compose to start three containers: a linux dev box using the image you built in the previous step (which is where you will run Rails), Postgres, and Redis.- In another terminal, you can do
dx/exec bashto get a prompt into the container where you will do dev work. You can also issue commands directly, e.g.dx/exec rails --help
This is not a Docker tutorial, but basically:
- Docker runs containers, which are like virtual computers
- Each container is based on an image which is like a cloned hard drive you can use to run inside a computer
- An image is built by a Dockerfile which describes what goes into the image
- Docker Compose can run multiple containers networked together
Dockerfile.dx creates an image that has a Ruby dev environment in it. That image, when run via dx/start will have access to
a Postgres (via the host db) and a Redis (via the host redis). The Rails app you will build will be available via
localhost:9999
[ Your Computer ]
|
|
|
|
| +-----------------------+
v | |
{9999}-+ sustainable-rails-dev |
| |
| «rails app/ruby dev» |
+-------+---------------+
|
|
+---+---+
______________ | | __________
/ /| | | / /|
+--------------+| | | +----------+|
| || v v | ||
| db-------+---{5432} {6379}--+--redis ||
| || | ||
| «Postgres» |/ | «Redis» |/
+--------------+ +----------+
The scripts are in dx/ and all respond to -h, but here is what they all do:
Dockerfile.dx- Used to create the dev environment container. It is heavily commented so you can see what is doing what.docker-compose.dx.yml- Used to start up the networked services described above. It is commented to explain a few things.dx/- holds automation scripts. These should all respond to-hdx/build- Builds an image fromDockerfile.dxdx/docker-compose.env- common configuration needed between the scripts anddocker-compose.dx.ymldx/dx.sh.lib- shared bash functionality between the scriptsdx/exec- Wrapsdocker compose execto run commands in a running containerdx/setupkit.sh.lib- Other common base functionalitydx/show-help-in-app-container-then-wait.sh- Script that is run inside the dev container to keep it running. This is for use inside the Docker containerdx/start- Starts up all the containers using Docker Composedx/stop- Stops th containers (though you can use Ctrl-C in the terminal where you randx/start
README.md- this file
Yes, you could use this as the basis for a dev environment for your Rails app. "Sustainable Dev Environments with Docker and Bash" expands greatly on how this all works if you want a deeper dive.
To use this for your app:
- Copy the files in this repo to your app or clone it using GitHub's template repository feature.
- Edit
dx/docker-compose.envand change the values there:IMAGEshould be in format«org»/«repo»:«tag», where:«org»is your org on Docker Hub or GitHub (doesn't really matter and could be omitted)«repo»should be the name of your app, suffixed with-dev. It technically doesn't matter, but it's used to build a dev image, not one for production, so using-devsuffix helps communicate that.«tag»should beruby-3.2since the image is based on Ruby's 3.2 image. You want this tag to be a version relevant to the environment, not your app.
PROJECT_NAMEandDEFAULT_SERVICEshould ideally be the same and be either simplyappor the name of your app, without spaces, formatted to be a valid hostname. These values are used in Docker Compose to start your app's dev environment container up.
- Edit
Dockerfile.dxto add any additional tooling your app needs to work. dx/builddx/start- etc.
- This repo is synced from a private repo where the book is developed. These are the actual files the book uses to execute the code.
- The social image was created in Pixelmator based on a 35mm photo I took of the House of Eyrabakki in Iceland. Text is set in Helvetica Neue.
- All content was created without any assistance from a Generative AI.
