DEV Community

Masatoshi Nishiguchi
Masatoshi Nishiguchi

Posted on

Build Phoenix Docker Compose development environment easily

I wrote a script called phx-docker-compose-new that can be used instead of mix phx.new to generate a new Phoenix application.

Building the development environment for a Phoenix application using Docker Compose is convenient because you can set up not only the application but also the PostgreSQL database, Livebook, etc. all at once.

However, the reality is that it is not always easy and requires some know-how.

https://qiita.com/koyo-miyamura/items/a609de2e9fadaf198243

https://zenn.dev/koga1020/articles/d260bc1bde8267

https://qiita.com/mnishiguchi/items/e367743bca3520e2a387

I just thought it would be nice to have a script that allows us to easily build a Phoenix development environment using Docker Compose. Of course, just thinking about it won't change anything, so I got down to business straight away.

日本語版

Getting started

Make sure that Git, Docker, and Docker Compose are installed in your system.

git version
docker --version
docker compose version
Enter fullscreen mode Exit fullscreen mode

Download the source code for the phx-docker-compose-new command.

git clone https://github.com/mnishiguchi/phx-docker-compose-new.git ~/.phx-docker-compose-new
Enter fullscreen mode Exit fullscreen mode

Define an alias so you can use the phx-docker-compose-new command in your terminal.

alias phx-docker-compose-new=~/.phx-docker-compose-new/phx-docker-compose-new.sh
Enter fullscreen mode Exit fullscreen mode

Generate a Phoenix sample app using the phx-docker-compose-new command. For available options, refer to Phoenix official documentation.

phx-docker-compose-new sample_phx_app --no-assets --no-gettext --no-mailer
Enter fullscreen mode Exit fullscreen mode

Go into the generated app directory and launch the app.

cd sample_phx_app

bin/start
Enter fullscreen mode Exit fullscreen mode

You can access the URLs below and start developing your Phoenix app right now!

docker-compose-demo 2023-11-23 09-44-06.png

You can look at the log with the command below. Press Ctrl + C to close the log.

bin/logs --follow
Enter fullscreen mode Exit fullscreen mode

Elixir's interactive console (IEx) can be started with the following command.

bin/console
Enter fullscreen mode Exit fullscreen mode

Since IEx is open, we might as well do something. Let's display a list of processes.

IEx.configure inspect: [limit: :infinity]

for pid <- Process.list, do: {pid, Process.info(pid, :registered_name) |> elem(1)}
Enter fullscreen mode Exit fullscreen mode

To stop the app, use the following command.

bin/stop
Enter fullscreen mode Exit fullscreen mode

🎉🎉🎉

Wrapping up

Congratulations! Now you can build a Phoenix application development environment easily any time.

toukon-qiita-macbook_20230912_091808.jpg

Top comments (0)