Build Phoenix Docker Compose development environment easily
Tags: elixir, erlang, phoenix, docker
Published: 2023-11-28 23:25:12


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.

[日本語版](https://qiita.com/mnishiguchi/items/425a7e55f05a7ab6359b)

## Getting started

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

```shell
git version
docker --version
docker compose version
```

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

```shell
git clone https://github.com/mnishiguchi/phx-docker-compose-new.git ~/.phx-docker-compose-new
```

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

```shell
alias phx-docker-compose-new=~/.phx-docker-compose-new/phx-docker-compose-new.sh
```

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

```shell
phx-docker-compose-new sample_phx_app --no-assets --no-gettext --no-mailer
```

Go into the generated app directory and launch the app.

```shell
cd sample_phx_app

bin/start
```

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

- [http://localhost:4000/](http://localhost:4000/)
- [http://localhost:4000/dev/dashboard/](http://localhost:4000/dev/dashboard/)
- [http://localhost:4001/](http://localhost:4001/)

![docker-compose-demo 2023-11-23 09-44-06.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/82804/ee30129d-9d51-0156-6a89-96707d38c25b.png)

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

```shell
bin/logs --follow
```

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

```shell
bin/console
```

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

```elixir
IEx.configure inspect: [limit: :infinity]

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

To stop the app, use the following command.

```shell
bin/stop
```

:tada::tada::tada:

## Wrapping up

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

![toukon-qiita-macbook_20230912_091808.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/82804/fd5c55ec-4fe0-8af6-59bc-bab1ef3d182b.jpeg)


[Docker]: https://docs.docker.com/get-started/overview/
[Elixir]: https://elixir-lang.org/
[Erlang]: https://www.erlang.org/
[Livebook]: https://livebook.dev/
[Phoenix]: https://www.phoenixframework.org/
[mix phx.new]: https://hexdocs.pm/phoenix/Mix.Tasks.Phx.New.html
[phx-docker-compose-new]: https://github.com/mnishiguchi/phx-docker-compose-new
[Docker Compose]: https://docs.docker.com/compose/
[PostgreSQL]: https://www.postgresql.org/
[Git]: https://git-scm.com/
[mix phx.new]: https://hexdocs.pm/phoenix/Mix.Tasks.Phx.New.html
[IEx]: https://elixir-lang.org/getting-started/introduction.html#interactive-mode