15
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DockerでPhoenix Framework

Posted at

環境とか

  • macOS
  • Docker For Mac Version 18.03.1

Dockerfileとdocker-composeをcloneしてbuild

git clone git@github.com:foxtrackjp/elixir_test.git
cd elixir_test
docker-compose build

Dockerfileはこんな感じ

Dockerfile
FROM elixir:alpine

RUN mkdir /app
WORKDIR /app

RUN yes | mix local.hex
RUN yes | mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new.ez
RUN mix local.rebar --force
RUN apk update && \
    apk upgrade && \
    apk add --update\
    bash \
    curl-dev \
    git \
    nodejs \
    nodejs-npm \
    && rm -rf /var/cache/apk/*

alpineベースなイメージがあったのでこちらを利用しました。(ビルド後のイメージは150M弱くらい)

docker-composeはこんな感じ

docker-compose.yml
version: '3'
services:
  db:
    image: mysql:5.7.22
    volumes:
      - mysql:/var/lib/mysql
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: gJwWACGZqaukCjL7

  app:
    build: .
    tty: true
    volumes:
      - .:/app
    ports:
      - "80:80"
      - "4000:4000"
    command: mix phx.server
    depends_on:
      - db
      
volumes:
  mysql:
    driver: local

アプリケーションの作成

docker-compose run app mix phx.new . --app sample --database mysql

Railsで言うとrails new sample --database=mysql

DBの接続情報を設定

生成されたアプリケーションのconfig/dev.exsにDBの接続情報を設定します。

dev.exs
config :sample, Sample.Repo,
  adapter: Ecto.Adapters.MySQL,
  username: "root",
  password: "gJwWACGZqaukCjL7", #ここ
  database: "sample_dev",
  hostname: "db",#ここ
  pool_size: 10

DBの作成

docker-compose run app mix ecto.create

Railsで言うとRails db:create

起動

docker-compose up

起動するとサーバーが立ち上がり、http://localhost:4000 にアクセスしてWelcomeページが表示されれば成功です

Welcomeページ

スクリーンショット 2018-05-24 14.56.47.png

感想

  • 全体的にRailsっぽさがあるので仲良くなれそう
  • と思ったらちょいちょい差異がある
15
12
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?