1. 程式人生 > >使用distillery&&docker 部署phoenix 應用

使用distillery&&docker 部署phoenix 應用

 

distillery 釋出了2.0 了,有好多的新的功能

  • config prodiver 進行環境變數的配置
  • appup tansforms 外掛系統,方便在release 構建的時候進行修改
  • mix release.gen.appup 新的mix task
  • pid 檔案

demo 是一個簡單的phoenix 同時使用docker 進行構建

專案準備

  • 專案結構
├── Dockerfile
├── README.md
├── _build
├── assets
├── config
├── deps
├── docker-compose.yaml
├── lib
├── mix.exs
├── mix.lock
├── priv
├── rel
└── test
  • 專案建立
mix phx.new --no-ecto phoenix_distillery
  • 新增distillery 支援
mix.exs
defp deps do
    [ ...,
      {:plug_cowboy, "~> 1.0"}, // 需要新增,不然構建會有問題
      {:distillery, "~> 2.0"}
       ]
end
  • 配置phoenix endpoint 資訊
config/prod.exs
config :phoenix_distillery, PhoenixDistilleryWeb.Endpoint,
  http: [port: {:system, "PORT"}],
  url: [host: "localhost", port: {:system, "PORT"}], # This is critical for ensuring web-sockets properly authorize.
  cache_static_manifest: "priv/static/cache_manifest.json",
  server: true,
  root: ".",
  version: Application.spec(:phoenix_distillery, :vsn)
  • 構建
mix deps.get --only prod
MIX_ENV=prod mix compile
cd assets
yarn build build --production
cd ..
mix phx.digest
  • distillery release
進行release 初始化配置
mix release.init
MIX_ENV=prod mix release
  • 執行
PORT=4001 _build/prod/rel/phoenix_distillery/bin/phoenix_distillery foreground

docker 構建支援

  • dockerfile

    使用mutil stage 構建

# The version of Alpine to use for the final image
# This should match the version of Alpine that the `elixir:1.7.2-alpine` image uses
ARG ALPINE_VERSION=3.8

FROM elixir:1.7.2-alpine AS builder

# The following are build arguments used to change variable parts of the image.
# The name of your application/release (required)
ARG APP_NAME=phoenix_distillery
# The version of the application we are building (required)
ARG APP_VSN=0.0.1
# The environment to build with
ARG MIX_ENV=prod
# Set this to true if this release is not a Phoenix app
ARG SKIP_PHOENIX=false
# If you are using an umbrella project, you can change this
# argument to the directory the Phoenix app is in so that the assets
# can be built
ARG PHOENIX_SUBDIR=.

ENV SKIP_PHOENIX=${SKIP_PHOENIX} \
    APP_NAME=${APP_NAME} \
    APP_VSN=${APP_VSN} \
    MIX_ENV=${MIX_ENV}

# By convention, /opt is typically used for applications
WORKDIR /opt/app

# This step installs all the build tools we'll need
RUN apk update && \
  apk upgrade --no-cache && \
  apk add --no-cache \
    nodejs \
    yarn \
    git \
    build-base && \
  mix local.rebar --force && \
  mix local.hex --force

# This copies our app source code into the build container
COPY . .

RUN mix do deps.get, deps.compile, compile

# This step builds assets for the Phoenix app (if there is one)
# If you aren't building a Phoenix app, pass `--build-arg SKIP_PHOENIX=true`
# This is mostly here for demonstration purposes
RUN if [ ! "$SKIP_PHOENIX" = "true" ]; then \
  cd ${PHOENIX_SUBDIR}/assets && \
  yarn install && \
  yarn deploy && \
  cd .. && \
  mix phx.digest; \
fi

RUN \
  mkdir -p /opt/built && \
  mix release --verbose && \
  cp _build/${MIX_ENV}/rel/${APP_NAME}/releases/${APP_VSN}/${APP_NAME}.tar.gz /opt/built && \
  cd /opt/built && \
  tar -xzf ${APP_NAME}.tar.gz && \
  rm ${APP_NAME}.tar.gz

# From this line onwards, we're in a new image, which will be the image used in production
FROM alpine:${ALPINE_VERSION}

# The name of your application/release (required)
ARG APP_NAME=phoenix_distillery

RUN apk update && \
    apk add --no-cache \
      bash \
      openssl-dev

ENV REPLACE_OS_VARS=true \
    APP_NAME=${APP_NAME}

WORKDIR /opt/app

COPY --from=builder /opt/built .

CMD trap 'exit' INT; /opt/app/bin/${APP_NAME} foreground
  • docker-compose
version: '3.5'
services:
  web:
    build: ./
    ports:
      - "4000:4000"
    env_file:
      - config/docker.env
  • docker 執行環境變數配置
config/docker.env

SECRET_KEY_BASE="u1QXlca4XEZKb1o3HL/aUlznI1qstCNAQ6yme/lFbFIs0Iqiq/annZ+Ty8JyUCDc"
PORT=4000
LANG=en_US.UTF-8
REPLACE_OS_VARS=true
ERLANG_COOKIE=myapp
  • 構建&&執行
docker-compose build && docker-compose up -d
  • 效果

說明

2.0 有好多新特性的新增,後邊會有簡單的使用demo,功能很強大

參考資料

https://hexdocs.pm/distillery/guides/phoenix_walkthrough.html
https://github.com/rongfengliang/phoenix_distillery-docker-demo