Docker:错误:约束无法满足

时间:2020-05-23 07:26:14

标签: docker dockerfile apk alpine

我正在尝试从https://docs.docker.com/compose/gettingstarted/构建docker映像示例。而我得到了这个错误

Building web
Step 1/9 : FROM python:3.7-alpine
 ---> e854017db514
Step 2/9 : WORKDIR /code
 ---> Using cache
 ---> e15b6e62d8af
Step 3/9 : ENV FLASK_APP app.py
 ---> Using cache
 ---> 759c4bc8b254
Step 4/9 : ENV FLASK_RUN_HOST 0.0.0.0
 ---> Using cache
 ---> 6d40793f3089
Step 5/9 : RUN apk add --no-cache gcc musl-dev linux-headers
 ---> Running in 5e40bd670f1b
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz
WARNING: Ignoring http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz: Permission denied
WARNING: Ignoring http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz: Permission denied
ERROR: unsatisfiable constraints:
  gcc (missing):
    required by: world[gcc]
  linux-headers (missing):
    required by: world[linux-headers]
  musl-dev (missing):
    required by: world[musl-dev]
ERROR: Service 'web' failed to build: The command '/bin/sh -c apk add --no-cache gcc musl-dev linux-headers' returned a non-zero code: 3

这是我的Dockfile

FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP app.py
ENV FLASK_RUN_HOST 0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["flask", "run"]

enyone可以帮助我解决此错误吗?谢谢大家!!!

1 个答案:

答案 0 :(得分:1)

您要安装的软件包是否已移至其他注册表?

有时,正在安装的软件包从不同的注册表分支中移出。默认的Docker Alpine映像仅设置了某些注册表。添加其他注册表会扩展您的安装选项(YMMV);我不能说与不同注册表分支相关的稳定性,安全性或风险。 This thread helped me.

失败的尝试和错误消息:

❯ docker run -it alpine sh                                                                                                                                                                 
/ # apk update
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/community/x86_64/APKINDEX.tar.gz
v3.12.0-175-g8b3334c57c [http://dl-cdn.alpinelinux.org/alpine/v3.12/main]
v3.12.0-178-gb27c83e867 [http://dl-cdn.alpinelinux.org/alpine/v3.12/community]
OK: 12749 distinct packages available
/ # apk add cowsay
ERROR: unsatisfiable constraints:
  cowsay (missing):
    required by: world[cowsay]
/ # 

  1. 我们从输出中获得的第一个线索
ERROR: unsatisfiable constraints:
       cowsay (missing):
       required by: world[cowsay]
  1. 输出中的第二条线索

更早的记录

fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/community/x86_64/APKINDEX.tar.gz

解决方案

  1. 在DuckDuckGo上搜索package <package name>

我正在使用哪种类型的内存?

  • 在线索2中找到了答案。 x86_64

在软件包的页面上,选择适当的链接。所以就我而言:

阅读该页面上的信息,我发现该软件包的下载 二进制文件下目前是

的一部分
  • http://dl-cdn.alpinelinux.org/alpine/edge/testing/x86_64/cowsay-3.04-r0.apk

将其与线索2进行比较,我发现高山容器未使用我要安装的二进制文件引用注册表。

成功&TL; DR;

通过将其他注册表添加到docker alpine容器中,向alpine添加以前未找到的软件包。

我将添加需要找到要安装的软件包的注册表(第3步)。在下面的代码块中,从第3部分中进行的研究中可以看到,第三个注册表与URL的初始部分匹配。我不想替换现有的注册表(线索2),所以我再次设置了它们。我不知道这是否必要,但我还是这么做了。

$ docker run -it alpine sh
/# apk update && apk add cowsay \
        --update-cache \
        --repository https://alpine.global.ssl.fastly.net/alpine/edge/community \
        --repository https://alpine.global.ssl.fastly.net/alpine/edge/main \
        --repository https://dl-3.alpinelinux.org/alpine/edge/testing
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/community/x86_64/APKINDEX.tar.gz
v3.12.0-175-g8b3334c57c [http://dl-cdn.alpinelinux.org/alpine/v3.12/main]
v3.12.0-178-gb27c83e867 [http://dl-cdn.alpinelinux.org/alpine/v3.12/community]
OK: 12749 distinct packages available
fetch https://dl-3.alpinelinux.org/alpine/edge/testing/x86_64/APKINDEX.tar.gz
fetch https://alpine.global.ssl.fastly.net/alpine/edge/main/x86_64/APKINDEX.tar.gz
fetch https://alpine.global.ssl.fastly.net/alpine/edge/community/x86_64/APKINDEX.tar.gz
(1/3) Installing libbz2 (1.0.8-r1)
(2/3) Installing perl (5.30.3-r2)
(3/3) Installing cowsay (3.04-r0)
Executing busybox-1.31.1-r16.trigger
OK: 43 MiB in 17 packages
/ #

Dockerfile示例

FROM alpine
RUN apk update && apk add cowsay \
        --update-cache \
        --repository https://alpine.global.ssl.fastly.net/alpine/edge/community \
        --repository https://alpine.global.ssl.fastly.net/alpine/edge/main \
        --repository https://dl-3.alpinelinux.org/alpine/edge/testing
CMD ["cowsay", "hi stackoverflow"]

构建此文件后:

 ❯ docker run cowsay                                                                                                                                                                        [13:13:45]
 __________________ 
< hi stackoverflow >
 ------------------ 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||