When I spin up a new project, I always want a dev environment with a database and test runner. I’ve had a set of files I’ve copied from project to project over the years, but it’s manual and error prone. I’ve finally gotten annoyed enough to create a mix task that sets it all up for you.
You install it with mix archive.install hex dockerize_elixir
then you can run mix dockerize path/to/my_app
to install all the docker stuff into your project. Then a docker compose up
will start the whole thing.
It creates 3 containers:
- your app
- postgres
- your tests (
mix test
ormix test.watch
, if installed)
Things it does for you:
- It configures ecto to connect to the
postgres
container instead oflocalhost
. - It exposes port 4000 to your host machine, which is the default port for Phoenix. It also configures Phoenix to listen on 0.0.0.0, so you can actually access it.
- It makes a volume to share the code between your host machine and the containers.
- It makes volumes to isolate
_build
anddeps
folders from your host machine.
It also installs a few helper scripts:
bin/iex
runsiex
inside theapp
containerbin/psql
runspsql
inside thepostgres
containerbin/rollback
rolls back the migration. Amigrate
script is not necessary because it runs migrations when the container starts.
This should work for Phoenix apps, or vanilla elixir apps that use ecto.
Please give it a try and let me know how it works for you.