5

I have specified a git clone command in the docker file as below.

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

but I got this error when build the docker image:

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

I wonder why it doesn't work. I am able to run this command on my host. Is there anything different if I list it on docker file?

1
  • 2
    In general you'd want to run this in the build environment outside your Dockerfile if possible. Two reasons for that: if the repository is private it's very hard to not leak the credentials you need to clone it, and because of docker build caching if the repository updates you won't actually get updated code in your image. Commented Aug 15, 2018 at 0:01

2 Answers 2

8

You can pass credentials as arguments to container. This should work

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"] 

but it might be unsafe if you want to distribute that image

then build

docker build \ --no-cache \ -t git-app:latest \ --build-arg username=user \ --build-arg password=qwerty \ . 
Sign up to request clarification or add additional context in comments.

1 Comment

This no longer works, as far as I know. Password authentication is now disabled. (Source)
-1

I know what wrong with this problem. The issue is because the repo I am cloning is a private repo which means it requires credential to connect to github. Fixing it by passing credentials to the container.

2 Comments

This way you will leak the credentials to anyone who will pull this image
You are right. I believe there are some solutions to solve this security issue such as using ssh-agent then remove the layer from docker.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.