moto blog

Docker-Composeで作るElixir/Phoenix開発環境

f:id:nmmmk:20181011001551p:plain

Docker-ComposeでElixir/Phoenixの開発環境を構築します。

環境

項目 Version
Docker 18.03.1-ce, build 9ee9f40
Docker-Compose 1.21.1, build 5a3f1a3
Elixir 1.6.6 with OTP 20
Phoenix 1.3.3
PostgreSQL 10.4

docker-compose.yml

docker-compose.ymlの全体の定義は以下の様になります。

Dockerfile

コンテナ生成のための設定を行います。
Node.jsのバージョンは、8.x
npmのバージョンは、6.1.0
とします。

FROM elixir:1.6.6

# nodeのインストール
ENV NODE_VERSION 8.x
ENV NPM_VERSION 6.1.0

RUN curl -sL https://deb.nodesource.com/setup_${NODE_VERSION} | bash -
RUN apt-get install -y nodejs

RUN npm install npm@${NPM_VERSION} -g

# Elixirの依存関係のインストール
RUN mix local.hex --force
RUN mix archive.install --force https://github.com/phoenixframework/archives/raw/master/phx_new.ez
RUN mix local.rebar --force

WORKDIR /app

Phoenixのプロジェクト生成

以下のコマンドを実行して、Phoenixのプロジェクトを生成します。

$ docker-compose run web mix phx.new . --app mysite

途中、以下の様なメッセージが表示されますが、そのままEnterを押して進んでください。
- The directory /app already exists. Are you sure you want to continue? [Yn]
- Fetch and install dependencies? [Yn]

DBの接続情報を設定

./config/dev.exs を開き、Postgresのhostnameの部分を以下の様に変更します。

# Configure your database
config :mysite, Mysite.Repo,
  adapter: Ecto.Adapters.Postgres,
  username: "postgres",
  password: "postgres",
  database: "mysite_dev",
  hostname: "db",
  pool_size: 10

DBの作成

以下のコマンドを実行して、DBの作成を行います。

$ docker-compose run web mix ecto.create

起動

docker-compose実行

以下のコマンドを実行して、コンテナを起動します。

$ docker-compose up

実行確認

コンテナが起動するとサーバーが立ち上がるため、http://localhost:4000にアクセスしてください。
以下の様なWelcomeページが表示されれば成功です。

f:id:nmmmk:20181011001551p:plain

その他

ソースコードGitHubに置いています。

github.com