[英]How can I use Erlang with Docker to run a Phoenix application?
I want to use a docker image in production to run a Phoenix container, However, since Elixir is just a layer on top of Erlang, it feels like it might be a waste of space to have Elixir running in my production environment.我想在生产环境中使用 docker 图像来运行 Phoenix 容器,但是,由于 Elixir 只是 Erlang 之上的一层,所以感觉在我的生产环境中运行 Elixir 可能会浪费空间。
Ideally, I would be able to compile an entire Phoenix application into Erlang, and then use an image from erlang:alpine to actually run the app in production.理想情况下,我可以将整个 Phoenix 应用程序编译成 Erlang,然后使用来自 erlang:alpine 的图像在生产环境中实际运行该应用程序。 Something like this...像这样的东西......
FROM elixir:alpine as builder
(install dependencies and copy files)
RUN mix compile_app_to_erlang
FROM erlang:alpine
COPY --from=builder /path/to/compiled/erlang /some/other/path
CMD ["erlang", "run"]
note: compile_app_to_erlang is not a real command, but I'm looking for something like it.注意:compile_app_to_erlang 不是真正的命令,但我正在寻找类似的命令。 Also, I have no idea how erlang runs, so all the code in there is completely made up.另外,我不知道 erlang 是如何运行的,所以里面的所有代码都是完全编造的。
Also, from what I know, there is a project called distillery that kind of does this, but this seems like the type of thing that shouldn't be too complicated (if I knew how erlang worked,) and I'd rather not rely on another dependency if I don't have too.另外,据我所知,有一个名为 distillery 的项目就是这样做的,但这似乎是那种不应该太复杂的事情(如果我知道 erlang 是如何工作的话),我宁愿不依赖如果我没有的话,在另一个依赖项上。 Plus it looks like if you use distillery you also have to use custom made docker images to run the code which is something I try to avoid.另外,如果您使用酿酒厂,您似乎还必须使用定制的 docker 图像来运行代码,这是我尽量避免的事情。
Is something like this even possible?这样的事情甚至可能吗? If so, anyone know a DIY solution?如果是这样,有人知道 DIY 解决方案吗?
Elixir 1.9 added the concept of a "release" to Mix. Elixir 1.9向 Mix 添加了“发布”的概念。 (This was released about 11 months after the question was initially asked.) Running mix release
will generate a tree containing the BEAM runtime, your compiled applications, and all of its dependencies. (这是在最初提出问题后大约 11 个月发布的。)运行mix release
将生成一个包含 BEAM 运行时、您编译的应用程序及其所有依赖项的树。 There is extensive documentation for the mix release
task on hexdocs.pm . hexdocs.pm 上有大量关于mix release
任务的文档。
In a Docker context, you can combine this with a multi-stage build to do exactly what you're requesting: start from the complete elixir
image, create a tree containing the minimum required to run the image, and COPY
it into a runtime image.在 Docker 上下文中,您可以将其与多阶段构建结合起来以完全按照您的要求执行:从完整的elixir
图像开始,创建一个包含运行图像所需的最小值的树,并将其COPY
到运行时图像中. I've been working with a Dockerfile like:我一直在使用 Dockerfile,例如:
FROM elixir:1.13 AS build
WORKDIR /build
ENV MIX_ENV=prod
# Install two tools needed to build other dependencies.
RUN mix do local.hex --force, local.rebar --force
# Download dependencies.
COPY mix.exs mix.lock ./
RUN mix deps.get --only prod
# Compile dependencies (could depend on config/config.exs)
COPY config/ config/
RUN mix deps.compile
# Build the rest of the application.
COPY lib/ lib/
COPY priv/ priv/
RUN mix release --path /app
FROM ubuntu:20.04
# Get the OpenSSL runtime library
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive \
apt-get install --no-install-recommends --assume-yes \
libssl1.1
# Get the compiled application.
COPY --from=build /app /app
ENV PATH=/app/bin:$PATH
# Set ordinary metadata to run the container.
EXPOSE 4000
CMD ["myapp", "start"]
If you're using Phoenix, the Phoenix documentation has a much longer example .如果您使用的是 Phoenix,Phoenix 文档中有一个更长的示例。 On the one hand that covers some details like asset compilation;一方面涵盖了资产编译等一些细节; on the other, its runtime image seems to have a bit more in it than may be necessary.另一方面,它的运行时映像似乎比必要的要多一些。 That page also has some useful discussion on running Ecto migrations;该页面还对运行 Ecto 迁移进行了一些有用的讨论; with the Elixir fragment described there you could docker run
a temporary container to do migrations, run them in an entrypoint wrapper script, or use any other ordinary Docker technique.使用此处描述的 Elixir 片段,您可以docker run
一个临时容器来执行迁移,在入口点包装器脚本中运行它们,或使用任何其他普通的 Docker 技术。
I suggest you to use distillery to build a binary. 我建议您使用酒厂构建二进制文件。
Then just run a alpine
container, mount the distillery release
to it, run the binary. 然后,只需运行一个alpine
容器,将distillery release
安装到其中,运行二进制文件。 Yeah, you can eve use supervisor to run it. 是的,您可以使用超级用户运行它。
You can use remote_console
of distillery to link to the console of this binary. 您可以使用酒厂的remote_console
链接到此二进制文件的控制台。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.