首页 > 解决方案 > 在 Windows 上构建的 Raspberry 上运行 Docker 映像

问题描述

我想在我的 Windows 机器上使用 docker 开发应用程序,然后将它们发送到我的树莓派 3B+(并在 swarm 中运行它们)。

但是我没有这样做:

我在我的 Windows 机器上用 python 设置了一些 helloWorld:

应用程序.py

from flask import Flask
import os
import socket

app = Flask(__name__)

@app.route("/")
def hello():

    html = "<h3>Hello {name}!</h3>" \
           "<b>Hostname:</b> {hostname}<br/>"

    return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname())

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)

要求.txt

Flask

Dockerfile

FROM python
WORKDIR /app
COPY . /app
RUN pip install --trusted-host pypi.python.org -r requirements.txt
EXPOSE 80
ENV NAME World
CMD ["python", "app.py"]

跑步

docker build --tag=hello .
docker run -p 4000:80 hello

就像一个魅力,甚至我从我的注册表(在树莓上运行)推送和取回它

docker build -t somehostname/hello .
docker push somehostname/hello
//delete local container and image
docker run -p 4000:80 somehostname/hello

工作正常。

但是,当我 ssh 进入我的树莓 3B+ 并运行时

sudo docker pull somehostname/hello
sudo docker run -p 4000:80 somehostname/hello

我得到:

standard_init_linux.go:207: exec user process caused "exec format error"

我读到了这个错误,它似乎是架构差异的问题:我的 windows mashine 使用 x86-64,而树莓 3B+ 使用 ARM。

然而,我正在构建的 python 图像是为这两种架构制作的,所以 docker 应该提取正确的图像。还复制我的树莓上的代码,构建图像并使用完全相同的代码行运行容器工作正常。

我还阅读了有关 docker manifest 的信息,它是一个实验性功能,但我看不出这对我有什么帮助,因为我使用的所有东西(我猜这只是 python)已经是多架构的,所以 docker 应该自己处理这个问题。

有没有办法让我的应用程序启动并运行而无需将代码复制到我的树莓派?

更新:树莓派的输出

user@somehostname:~ $ sudo docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
registry            2                   c99846f41d25        2 months ago        22.1MB
user@somehostname:~ $ sudo docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
2af9cb1df776        registry:2          "/entrypoint.sh /etc…"   2 minutes ago       Up About a minute   0.0.0.0:5000->5000/tcp   registry
user@somehostname:~ $ sudo docker pull somehostname/hello:latest
latest: Pulling from hello
c5e155d5a1d1: Pull complete
221d80d00ae9: Pull complete
4250b3117dca: Pull complete
3b7ca19181b2: Pull complete
425d7b2a5bcc: Pull complete
dc3049ff3f44: Pull complete
472a6afc6332: Pull complete
5f79c90f8d7c: Pull complete
1051ee813012: Pull complete
38d05a77ad85: Pull complete
fe3cbf1eaf8a: Pull complete
c1e865e5779d: Pull complete
Digest: sha256:f7b31fff3116ef6621bf96fead8858ceb0768b502b2f3b221ab0f52cfc8039eb
Status: Downloaded newer image for somehostname/hello:latest
user@somehostname:~ $ sudo docker run -p 4000:80 somehostname/hello
standard_init_linux.go:207: exec user process caused "exec format error"
user@somehostname:~ $ sudo docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
somehostname/hello   latest              d7472edad23e        About an hour ago   938MB
registry            2                   c99846f41d25        2 months ago        22.1M
user@somehostname:~ $ sudo docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
2af9cb1df776        registry:2          "/entrypoint.sh /etc…"   11 minutes ago      Up 11 minutes       0.0.0.0:5000->5000/tcp   registry

标签: dockerraspberry-pi3

解决方案


您编写的几乎所有内容都是正确的,但我相信 Python 映像不是跨架构的,您应该找到在您正在使用的 Raspberry Pi 设备上运行的 Python 映像。

IIRC 3s 与 Zeros 的 ARM 架构也不同,所以要小心你得到正确的架构。

您包含的exec format errror内容是一个强有力的指标。


推荐阅读