运行Docker Compose构建Schemathesis服务时遇构建失败错误求助
Docker Compose构建Schemathesis服务失败问题排查
项目结构
docker-compose.yml:自定义Docker Compose配置文件schemathesis:包含源代码和Dockerfile的目录
docker-compose.yml内容
version: '3' services: schemathesis: build: ./schemathesis ports: - "8180:80" networks: - default
schemathesis目录下的Dockerfile内容
FROM python:3.10-alpine LABEL Name=Schemathesis WORKDIR /app RUN addgroup --gid 1000 -S schemathesis && \ adduser --uid 1000 -D -S schemathesis -G schemathesis -s /sbin/nologin COPY --chown=1000:1000 pyproject.toml README.rst src ./ RUN apk add --no-cache --virtual=.build-deps build-base libffi-dev curl openssl-dev && \ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && \ source $HOME/.cargo/env && \ pip install --upgrade pip && pip install --no-cache-dir ./ && \ apk del .build-deps && \ rustup self uninstall -y # Needed for the `.hypothesis` directory RUN chown -R 1000:1000 /app USER schemathesis ENTRYPOINT ["schemathesis"]
执行命令后的错误
执行sudo docker-compose up --build时,报错:ERROR: Service 'schemathesis' failed to build,具体错误输出如下:
Building schemathesis Step 1/9 : FROM python:3.10-alpine ---> 8a3a8409a638 Step 2/9 : LABEL Name=Schemathesis ---> Using cache ---> f108325765ba Step 3/9 : WORKDIR /app ---> Using cache ---> 9fc24fc24456 Step 4/9 : RUN addgroup --gid 1000 -S schemathesis && adduser --uid 1000 -D -S schemathesis -G schemathesis -s /sbin/nologin ---> Using cache ---> 64881e35ad07 Step 5/9 : COPY --chown=1000:1000 pyproject.toml README.rst src ./ ---> Using cache ---> 8b9dfeb0ed09 Step 6/9 : RUN apk add --no-cache --virtual=.build-deps build-base libffi-dev curl openssl-dev && curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && source $HOME/.cargo/env && pip install --upgrade pip && pip install --no-cache-dir ./ && apk del .build-deps && rustup self uninstall -y ---> Running in fafc492c1f99 fetch https://dl-cdn.alpinelinux.org/alpine/v3.17/main/x86_64/APKINDEX.tar.gz fetch https://dl-cdn.alpinelinux.org/alpine/v3.17/community/x86_64/APKINDEX.tar.gz WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.17/main: temporary error (try again later) WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.17/community: temporary error (try again later) ERROR: unable to select packages: .build-deps-20230120.185651: masked in: cache satisfies: world[.build-deps=20230120.185651] build-base (no such package): required by: .build-deps-20230120.185651[build-base] libffi-dev (no such package): required by: .build-deps-20230120.185651[libffi-dev] curl (no such package): required by: .build-deps-20230120.185651[curl] openssl-dev (no such package): required by: .build-deps-20230120.185651[openssl-dev] ERROR: Service 'schemathesis' failed to build: The command '/bin/sh -c apk add --no-cache --virtual=.build-deps build-base libffi-dev curl openssl-dev && curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && source $HOME/.cargo/env && pip install --upgrade pip && pip install --no-cache-dir ./ && apk del .build-deps && rustup self uninstall -y' returned a non-zero code: 5
解决方案
错误原因
Alpine镜像无法访问官方软件源,导致找不到指定的依赖包,同时缓存中存在损坏的源信息。
解决步骤
- 清理构建缓存
执行命令清除旧的构建缓存,避免缓存干扰:
sudo docker-compose build --no-cache
如果需要更彻底清理Docker缓存,可执行:
sudo docker system prune -a
- 更换Alpine软件源
如果网络访问官方源持续异常,修改Dockerfile,在apk add前添加更换国内镜像源的命令,示例如下:
修改后的Dockerfile第6步RUN块:
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \ apk add --no-cache --virtual=.build-deps build-base libffi-dev curl openssl-dev && \ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && \ source $HOME/.cargo/env && \ pip install --upgrade pip && pip install --no-cache-dir ./ && \ apk del .build-deps && \ rustup self uninstall -y
- 检查网络连通性
验证主机是否能正常访问Alpine官方源:
curl -I https://dl-cdn.alpinelinux.org/alpine/v3.17/main/x86_64/APKINDEX.tar.gz
若返回200状态码则网络正常,否则需排查防火墙、代理等网络设置。
内容的提问来源于stack exchange,提问作者illuminato
相关产品推荐
相关产品推荐

