Dockerized Rails pidfile Hack
I usually fix things quickly when they repeatedly add friction. But every once in a while, something slips through.
One of those things for me was Rails’ server pidfile in a dockerized dev environment. Any time a container exited abnormally, the pidfile would be left behind, causing the the container to fail to start the next time:
A server is already running. Check /var/www/my_app/tmp/pids/server.pid. Exiting.
My docker-compose.yml was configured to sync my local Rails app with the container so I could make changes locally and see them in the containerized app. Unfortunately, changes in the app container, such as that pidfile, also sync back to my local dev environment. There are several ways to handle this, but here’s the one I settled on: creating a tmpfs mount for the pids directory.
- type: tmpfs
target: /var/www/my_app/tmp/pids
In context:
version: '2'
services:
app:
build: my-app
command: bin/rails server --port 3000 --binding 0.0.0.0
links:
- db
ports:
- "3000:3000"
volumes:
- './my-app:/var/www/my_app'
- type: tmpfs
target: /var/www/my_app/tmp/pids