简体   繁体   English

如何使用 Erlang 和 Docker 来运行 Phoenix 应用程序?

[英]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.

相关问题 如何使用 Phoenix Guides 中的示例在生产模式下运行 Elixir Phoenix Docker? - How do I run Elixir Phoenix Docker in production mode using example from Phoenix Guides? 如何调试用docker运行的凤凰应用程序? - How to debug phoenix application which running with docker? 如何允许用户使用不同的 arguments 运行 Docker 应用程序 - How can I allow user to run Docker application with different arguments 如何在docker run命令中使用环境变量? - How can I use environment variables in a docker run command? 如何使用“docker run --user”但具有 root 权限 - How can I use “docker run --user” but with root priviliges 我可以使用docker在OSX上运行MSBuild吗? - Can I use docker to run MSBuild on OSX? 在Docker中使用Erlang Observer App和远程Elixir Phoenix服务器 - Using the Erlang Observer App with a remote Elixir Phoenix server inside Docker 如何在 docker 容器内针对外部应用程序运行 cypress 测试 - How can I run cypress tests inside a docker container against an external application 如何在 docker 容器内以交互方式运行 dotnet 核心控制台应用程序 - How can I interactively run a dotnet core console application inside of a docker container 如何在Docker中运行Spectron测试? - How can i run Spectron test in docker?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM