備註:git
簡單使用mix 進行項目的生成,同時添加docker 構建支持github
1. 生成項目docker
mix new mydemoproject 輸出信息以下: * creating README.md * creating .formatter.exs * creating .gitignore * creating mix.exs * creating config * creating config/config.exs * creating lib * creating lib/mydemoproject.ex * creating test * creating test/test_helper.exs * creating test/mydemoproject_test.exs Your Mix project was created successfully. You can use "mix" to compile it, test it, and more: cd mydemoproject mix test Run "mix help" for more commands. 項目結構以下: ├── README.md ├── config │ └── config.exs ├── lib │ └── mydemoproject.ex ├── mix.exs └── test ├── mydemoproject_test.exs └── test_helper.exs
a. config 主要是對於不一樣環境的變量配置 b. lib 主要是咱們本身的業務代碼放置 c. mix.exs 應用運行的一些配置,包括依賴包,構建,運行的說明,後面會有doc 以及main 入口的參考例子 d. test 單元測試的代碼
備註: 項目運行結構 ├── Dockerfile ├── README.md ├── config │ └── config.exs ├── lib │ ├── mydemoproject │ │ └── run.ex │ └── mydemoproject.ex ├── mix.exs └── test ├── mydemoproject_test.exs └── test_helper.exs a. docs (項目幫助文檔) 添加依賴包 mix.exs defp deps do [ {:ex_doc, "~> 0.14", only: :dev} ] end 添加文檔的描述 mix.exs def project do [ app: :userlogin, version: "0.1.0", elixir: "~> 1.6", start_permanent: Mix.env() == :prod, deps: deps(), package: package(), docs: [extras: ["README.md"], # 文檔的起始地址 main: "readme"] ] end defp package do %{maintainers: ["dalongrong"], licenses: ["MIT"], links: %{"GitHub" => "https://github.com/rongfengliang/elixir-dockerbuild"}} end b. 項目啓動入口 mix.exs # 入口聲明 def project do [ app: :mydemoproject, version: "0.1.0", elixir: "~> 1.6", start_permanent: Mix.env() == :prod, escript: escriptconfig(), package: package(), docs: [extras: ["README.md"], # 文檔的起始地址 main: "readme"], deps: deps() ] end # 入口定義 defp escriptconfig do [ main_module: Mydemoproject.Start] end lib/mydemoproject/run.ex defmodule Mydemoproject.Start do def main(argv) do IO.puts "hello world" end end c. docker 構建支持(使用的docker stage 構建,須要docker ce 17或者最新版本) # build stage FROM elixir:1.6-alpine AS build-env WORKDIR /app COPY * /app/ RUN cd /app \ && mix local.hex --force \ && mix local.rebar --force \ && mix deps.get \ && mix deps.compile \ && cp mydemoproject /tmp/ FROM erlang:20-alpine WORKDIR /app COPY --from=build-env /tmp/mydemoproject /app/mydemoproject CMD ["./mydemoproject"]
a. 依賴下載 mix.deps.get b. 構建 mix escript.build c. 項目文檔 mix docs d. docker 構建 docker build -t mydemoproject . e. 運行 local ./mydemoproject docker docker run -it mydemoproject ./mydemoproject
https://github.com/rongfengliang/elixir-dockerbuild