简体   繁体   English

mix 发布的 Docker 因“Exec 格式错误”而崩溃

[英]Docker of mix release crashes with "Exec format error"

Background背景

I have an executable created via mix release which works just fine when I run it with start on my local machine.我有一个通过mix release创建的可执行文件,当我在本地机器上使用start运行它时效果很好。 However, my docker image crashes when trying to start the executable and I don't know why.但是,我的 docker 映像在尝试start可执行文件时崩溃,我不知道为什么。

Docker码头工人

FROM elixir:1.10

# Install Hex + Rebar
RUN mix do local.hex --force, local.rebar --force

COPY . /
WORKDIR /

ENV MIX_ENV=prod
RUN mix do deps.get --only $MIX_ENV, deps.compile
RUN mix release

EXPOSE 8080
ENV PORT=8080
ENV SHELL=/bin/bash

CMD ["_build/prod/rel/my_app/bin/my_app", "start"]

This is my docker file.这是我的 docker 文件。 It does nothing special other than compiling and then trying to run the release via start .除了编译然后尝试通过start运行发布之外,它没有什么特别的。

Error错误

However, when I execute the docker image with docker run -p 8080:8080 my-app:1.0 docker crashes:但是,当我使用docker run -p 8080:8080 my-app:1.0执行 docker 镜像时,docker 崩溃了:

/_build/prod/rel/my_app/releases/0.1.0/../../erts-10.5/bin/erl: 12: exec: /_build/prod/rel/my_app/erts-10.5/bin/erlexec: Exec format error

Research研究

At first I thought this was happening because the file _build/prod/rel/my_app/bin/my_app was missing #!/usr/bin/env bash at the top (after reading some SO questions).起初我认为这是因为文件_build/prod/rel/my_app/bin/my_app在顶部丢失了#!/usr/bin/env bash (在阅读了一些 SO 问题之后)。

While indeed it doesn't have that, it has something else that should equally work: #!/bin/sh虽然它确实没有那个,但它还有一些同样有效的东西: #!/bin/sh

So this theory is out.所以这个理论出来了。

Question

Is something wrong with my Dockerfile?我的 Dockerfile 有问题吗? How can I get rid this error?我怎样才能摆脱这个错误?

RCA RCA

This was a tricky one.这是一个棘手的问题。 After reading other similar questions (and the comments from @potibas and @Stefano) I found out what was happening.在阅读了其他类似的问题(以及来自 @potibas 和 @Stefano 的评论)后,我发现了发生了什么。

So, as I have said, the release file works well on my local machine.所以,正如我所说,发布文件在我的本地机器上运行良好。 This happens because in my local machine the files were being compiled to its operative system (OS).发生这种情况是因为在我的本地机器中,文件被编译到其操作系统 (OS)。

Now, the docker VM does not have the same OS as my local machine.现在,docker VM 与我的本地机器没有相同的操作系统。 This matters because of this line:这很重要,因为这一行:

COPY . /

Here I am copying everything from the workdir to docker.在这里,我将所有内容从工作目录复制到 docker。 Including the binaries and dependencies compiled for my local machine's OS .包括为我本地机器的 OS 编译的二进制文件和依赖项。

This caused the issue above mentioned.这导致了上述问题。

Solution解决方案

There are 2 possible solutions.有 2 种可能的解决方案。

  1. Remove the _build and deps folders from the image after the COPY command, via RUN rm -r _build and RUN rm -r deps/ .COPY命令之后,通过RUN rm -r _buildRUN rm -r deps/从映像中删除_builddeps文件夹。
  2. Add the mentioned folders as well as all the non-essential files to a .dockerignore file.将上述文件夹以及所有非必要文件添加到.dockerignore文件中。

I personally opted for the second option, as it allows me to have a leaner image.我个人选择了第二个选项,因为它可以让我拥有更精简的形象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM