如何在docker文件中运行`git clone`?

问题描述 投票:1回答:2

我在docker文件中指定了一个git clone命令,如下所示。

RUN git clone https://github.com/zhaoyi0113/test.git

但是在构建docker镜像时遇到了这个错误:

Cloning into 'test'...
fatal: could not read Username for 'https://github.com': No such device or address

我想知道为什么它不起作用。我可以在我的主机上运行此命令。如果我在docker文件中列出它有什么不同吗?

docker dockerfile
2个回答
4
投票

您可以将凭据作为参数传递给容器。这应该工作

FROM alpine:3.8

RUN apk update && apk upgrade && \
    apk add --no-cache bash git openssh

ARG username
ARG password

RUN git clone https://${username}:${password}@github.com/username/repository.git

ENTRYPOINT ["sleep 10"]

但如果您想分发该图像,可能会不安全

然后建立

docker build \
    --no-cache \
    -t git-app:latest \
    --build-arg username=user \
    --build-arg password=qwerty \
    .

0
投票

我知道这个问题有什么问题。问题是因为我克隆的repo是私有仓库,这意味着它需要凭证才能连接到github。通过将凭据传递给容器来修复它。

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