Docker : exec /usr/bin/sh: exec 格式错误

问题描述 投票:0回答:4

大家好需要一些帮助。

我创建了一个自定义 docker 镜像并将其推送到 docker hub,但是当我在 CI/CD 中运行它时,它给了我这个错误。

exec /usr/bin/sh: exec format error

地点:

Docker文件

FROM ubuntu:20.04
RUN apt-get update
RUN apt-get install -y software-properties-common
RUN apt-get install -y python3-pip
RUN pip3 install robotframework

.gitlab-ci.yml

robot-framework:
  image: rethkevin/rf:v1
  allow_failure: true
  script:
    - ls
    - pip3 --version

输出

Running with gitlab-runner 15.1.0 (76984217)
  on runner zgjy8gPC
Preparing the "docker" executor
Using Docker executor with image rethkevin/rf:v1 ...
Pulling docker image rethkevin/rf:v1 ...
Using docker image sha256:d2db066f04bd0c04f69db1622cd73b2fc2e78a5d95a68445618fe54b87f1d31f for rethkevin/rf:v1 with digest rethkevin/rf@sha256:58a500afcbd75ba477aa3076955967cebf66e2f69d4a5c1cca23d69f6775bf6a ...
Preparing environment
00:01
Running on runner-zgjy8gpc-project-1049-concurrent-0 via 1c8189df1d47...
Getting source from Git repository
00:01
Fetching changes with git depth set to 20...
Reinitialized existing Git repository in /builds/reth.bagares/test-rf/.git/
Checking out 339458a3 as main...
Skipping Git submodules setup
Executing "step_script" stage of the job script
00:00
Using docker image sha256:d2db066f04bd0c04f69db1622cd73b2fc2e78a5d95a68445618fe54b87f1d31f for rethkevin/rf:v1 with digest rethkevin/rf@sha256:58a500afcbd75ba477aa3076955967cebf66e2f69d4a5c1cca23d69f6775bf6a ...
exec /usr/bin/sh: exec format error
Cleaning up project directory and file based variables
00:01
ERROR: Job failed: exit code 1

对此有什么解决错误的想法吗?

python docker gitlab robotframework dockerhub
4个回答
37
投票

问题是你为 arm64/v8 构建了这个图像——但是你的运行器使用的是不同的架构。

如果你跑:

docker image inspect rethkevin/rf:v1

你会在输出中看到这个:

...
        "Architecture": "arm64",
        "Variant": "v8",
        "Os": "linux",
...

尝试从 GitLab CI runner 构建并推送您的图像,以便图像的架构与您的 runner 的架构相匹配。

或者,您可以使用 docker buildx

构建多个架构
。或者,您也可以在 ARM 架构上运行 GitLab runner,以便它可以运行您构建它的架构的图像。


15
投票

就我而言,我使用 buildx 构建它

docker buildx build --platform linux/amd64 -f ./Dockerfile -t image .

然而问题出在 AWS lambda


0
投票

你还应该确定你调用的确实是一个可执行文件。我们犯了这个错误:

COPY --from=build /usr/src/service/serviceExec /bin/service 
COPY --from=build /usr/config/file.txt /bin/service
RUN chmod a+x service
CMD ["./service"]

错误是:

exec ./service: exec format error

操作系统无法执行 txt 文件并抛出类似架构的错误。


0
投票

使用容器时出现此错误的原因可能有很多:

  1. 使用错误的脚本头,例如在中间添加空格。
  2. 在编写脚本时使用了一些不兼容的字符编码
  3. 不匹配的CPU架构
  4. 缺少文件权限

在这里用例子证明原因(3):

rohits-MacBook>目录%docker版本

它是我的笔记本电脑的 darwin/arm64 架构,并且安装了相同的 docker 引擎。

Client:
 Cloud integration: v1.0.31
 Version:           20.10.23
 API version:       1.41
 Go version:        go1.18.10
 Git commit:        7155243
 Built:             Thu Jan 19 17:35:19 2023
 OS/Arch:           darwin/arm64
 Context:           desktop-linux
 Experimental:      true

Server: Docker Desktop 4.17.0 (99724)
 Engine:
  Version:          20.10.23
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.10
  Git commit:       6051f14
  Built:            Thu Jan 19 17:31:28 2023
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          1.6.18
  GitCommit:        2456e983eb9e37e47538f59ea18f2043c9a73640
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

##############################################

在我当前的笔记本电脑上执行

rohits-MacBook>目录%制作图片

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build  -ldflags="-X github.ibm.com/cloud-sre/version.Major= -X github.ibm.com/cloud-sre/version.Minor= -X github.ibm.com/cloud-sre/version.Build= -X github.ibm.com/cloud-sre/version.Timestamp=202303170049" -o osscatimporter ./cmd/osscatimporter
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build  -ldflags="-X github.ibm.com/cloud-sre/version.Major= -X github.ibm.com/cloud-sre/version.Minor= -X github.ibm.com/cloud-sre/version.Build= -X github.ibm.com/cloud-sre/version.Timestamp=202303170049" -o osscatpublisher ./cmd/osscatpublisher
jq: error: Could not open file metadata.json: No such file or directory
docker buildx build --platform=linux/amd64 -t api-osscatalog:latest .
[+] Building 153.2s (11/11) FINISHED                                                                                                                         
 => [internal] load build definition from Dockerfile                                                                                                    0.0s
 => => transferring dockerfile: 560B                                                                                                                    0.0s
 => [internal] load .dockerignore                                                                                                                       0.0s
 => => transferring context: 2B                                                                                                                         0.0s
 => [internal] load metadata for docker-virtual.artifactory.swg-devops.com/ubi8/ubi-minimal:8.7                                            34.2s
 => [auth] ubi8/ubi-minimal:pull token for docker-virtual.artifactory.swg-devops.com                                                        0.0s
 => [1/5] FROM docker-virtual.artifactory.swg-devops.com/ubi8/ubi-minimal:8.7@sha256:65a240ad8bd3f2fff3e18a22ebadc40da0b145616231fc1e1625  91.3s
 => => resolve docker-virtual.artifactory.swg-devops.com/ubi8/ubi-minimal:8.7@sha256:65a240ad8bd3f2fff3e18a22ebadc40da0b145616231fc1e16251  0.0s
 => => sha256:0214a28336e387c66493c61bb394e86a18f3bea8dbc46de74a26f173ff553c89 429B / 429B                                                              0.0s
 => => sha256:591670a5d6a620931ec51c1e7436300894f5320e76e9737f0366fc62114addd4 6.23kB / 6.23kB                                                          0.0s
 => => sha256:d7c06497d5cebd39c0a4feb14981ec940b5c863e49903d320f630805b049cbff 39.28MB / 39.28MB                                                       90.8s
 => => sha256:65a240ad8bd3f2fff3e18a22ebadc40da0b145616231fc1e16251f3c6dee087a 1.47kB / 1.47kB                                                          0.0s
 => => extracting sha256:d7c06497d5cebd39c0a4feb14981ec940b5c863e49903d320f630805b049cbff                                                               0.4s
 => [internal] load build context                                                                                                                       0.2s
 => => transferring context: 23.77MB                                                                                                                    0.2s
 => [2/5] RUN microdnf update && microdnf install procps ;                                                                                             27.5s
 => [3/5] COPY osscatimporter /                                                                                                                         0.0s
 => [4/5] COPY mailtemplate.tmpl /                                                                                                                      0.0s
 => [5/5] COPY osscatpublisher /                                                                                                                        0.0s 
 => exporting to image                                                                                                                                  0.1s 
 => => exporting layers                                                                                                                                 0.1s 
 => => writing image sha256:8af2a24809c709c2ea80b1f3ed0c0d1dc1381c84219e0e779be43ab8542e8c0d                                                            0.0s 
 => => naming to docker.io/library/api-osscatalog:latest                                                                                                0.0s

################################

rohits-MacBook> 目录 % docker images

REPOSITORY       TAG       IMAGE ID       CREATED         SIZE
api-catalog     latest    8af2a24809c7   9 seconds ago   140MB

######################################

rohits-MacBook> osscatalog % docker image inspect api-catalog


        "Architecture": "arm64",
        "Os": "linux",
        "Size": 148693891,
        "VirtualSize": 148693891,

生成

arm64
图像

########################################

在其日志文件中运行实例后的问题显示:

rohits-MacBook> api-osscatalog % kubectl -n api logs -f catimporter-rohit-gst45

exec /bin/sh: exec format error
rohits-MacBook> api-catalog % 

评论:

这通常发生在您在采用 ARM 架构的系统上进行项目时,例如使用新的 Apple M 系列芯片组。当您将代码推送到使用 x86 系统的生产环境时,会导致“exec user process caused: exec format error”。这是因为转换为较低级别指令的每段代码对于 ARM 和 x86 都是不同的。 Docker 将 Apple M1 Pro 平台检测为“linux/arm64/v8”。

##################解决方案##########################

1。

/cloud-sre/catalog/Makefile
改变:
docker build -t $(IMAGE_NAME):latest .
到:
docker buildx build --platform=linux/amd64 -t $(IMAGE_NAME):latest .

  1. 但要使上述工作正常,还需要更改 Dockerfile
    /cloud-sre/catalog/Dockerfile
    改变:
    FROM docker-virtual.artifactory.swg-devops.com/ubi8/ubi-minimal:8.7
    到:
    FROM --platform=linux/amd64 docker-virtual.artifactory.swg-devops.com/ubi8/ubi-minimal:8.7

##################测试######################################################

rohits-MacBook> 目录 % docker images

REPOSITORY TAG IMAGE ID CREATED SIZE
删除所有图片

#####################################

rohits-MacBook>目录% rohits-MacBook> 目录 % 制作图像

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-X github.ibm.com/cloud-sre/version.Major= -X github.ibm.com/cloud-sre/version.Minor= -X github.ibm.com/cloud-sre/version.Build= -X github.ibm.com/cloud-sre/version.Timestamp=202303170049" -o osscatimporter ./cmd/osscatimporter CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-X github.ibm.com/cloud-sre/version.Major= -X github.ibm.com/cloud-sre/version.Minor= -X github.ibm.com/cloud-sre/version.Build= -X github.ibm.com/cloud-sre/version.Timestamp=202303170049" -o osscatpublisher ./cmd/osscatpublisher jq: error: Could not open file metadata.json: No such file or directory docker buildx build --platform=linux/amd64 -t api-osscatalog:latest . [+] Building 153.2s (11/11) FINISHED => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 560B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker-virtual.artifactory.swg-devops.com/ubi8/ubi-minimal:8.7 34.2s => [auth] ubi8/ubi-minimal:pull token for docker-virtual.artifactory.swg-devops.com 0.0s => [1/5] FROM docker-virtual.artifactory.swg-devops.com/ubi8/ubi-minimal:8.7@sha256:65a240ad8bd3f2fff3e18a22ebadc40da0b145616231fc1e1625 91.3s => => resolve docker-virtual.artifactory.swg-devops.com/ubi8/ubi-minimal:8.7@sha256:65a240ad8bd3f2fff3e18a22ebadc40da0b145616231fc1e16251 0.0s => => sha256:0214a28336e387c66493c61bb394e86a18f3bea8dbc46de74a26f173ff553c89 429B / 429B 0.0s => => sha256:591670a5d6a620931ec51c1e7436300894f5320e76e9737f0366fc62114addd4 6.23kB / 6.23kB 0.0s => => sha256:d7c06497d5cebd39c0a4feb14981ec940b5c863e49903d320f630805b049cbff 39.28MB / 39.28MB 90.8s => => sha256:65a240ad8bd3f2fff3e18a22ebadc40da0b145616231fc1e16251f3c6dee087a 1.47kB / 1.47kB 0.0s => => extracting sha256:d7c06497d5cebd39c0a4feb14981ec940b5c863e49903d320f630805b049cbff 0.4s => [internal] load build context 0.2s => => transferring context: 23.77MB 0.2s => [2/5] RUN microdnf update && microdnf install procps ; 27.5s => [3/5] COPY osscatimporter / 0.0s => [4/5] COPY mailtemplate.tmpl / 0.0s => [5/5] COPY osscatpublisher / 0.0s => exporting to image 0.1s => => exporting layers 0.1s => => writing image sha256:8af2a24809c709c2ea80b1f3ed0c0d1dc1381c84219e0e779be43ab8542e8c0d 0.0s => => naming to docker.io/library/api-osscatalog:latest 0.0s
##############################################

rohits-MacBook> 目录 % docker images

REPOSITORY TAG IMAGE ID CREATED SIZE api-catalog latest 8af2a24809c7 9 seconds ago 140MB
##############################################
rohits-MacBook> 目录 % docker image inspect api-catalog:latest

"Architecture": "amd64", "Os": "linux", "Size": 140425582,
—我以前总是把它放在我的旧笔记本电脑里。

############################################

现在检查新实例的日志:

rohits-MacBook> api-ocatalog % kubectl -n api logs -f catimporter-rohit-9bzk9

INFO 19:53:29 osscatimporter Version: .. - 202303170049 INFO 19:53:29 sending *osscatimporter* Starting Run osscatimporter using the slack-webhook-url Slack webhook key. Test COS connection passed: oss-rmc-data-test Test COS connection passed: oss-rmc-emergency-test AUDIT 19:53:30 Operating in read-only mode on all services and components found from all sources (with visibility=private and above) INFO 19:53:30 Forcing -check-owner=true INFO 19:53:30 Optional run action ENABLED: Services INFO 19:53:30 Optional run action ENABLED: Tribes INFO 19:53:30 Optional run action ENABLED: Environments INFO 19:53:30 Optional run action ENABLED: Deployments INFO 19:53:30 Optional run action ENABLED: Monitoring INFO 19:53:30 Optional run action ENABLED: RMC INFO 19:53:30 Optional run action ENABLED: RMC-Rescan INFO 19:53:30 Optional run action DISABLED: Environments-Native INFO 19:53:30 Optional run action DISABLED: ProductInfo-Parts INFO 19:53:30 Optional run action DISABLED: ProductInfo-Parts-Refresh INFO 19:53:30 Optional run action DISABLED: ProductInfo-ClearingHouse INFO 19:53:30 Optional run action DISABLED: Dependencies-ClearingHouse INFO 19:53:30 Optional run action DISABLED: ScorecardV1 INFO 19:53:30 Optional run action DISABLED: Doctor INFO 19:53:30 Skip reloading Segment and Tribe Info from ScorecardV1
没有错误

© www.soinside.com 2019 - 2024. All rights reserved.