Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prompting for username/password with git clone from Dockerfile run step

Tags:

git

docker

I'm trying to set up a multi-stage docker build and needing to cloning my sources for the initial first stage build step.

However, git clone requires a username/password as the path to the repository is on a private github enterpr server. Under normal circumstances git will prompt you for the username/password. However, with git clone started from a RUN step in the Dockerfile there is no such prompt and the output is simply:

fatal: could not read Username for 'https://yourserver.com': No such device or address The command '/bin/sh -c git clone https://yourserver.com/name/yourpath' returned a non-zero code: 128

Even with the -ti flag specified in the docker build step. How do I pass the username/password or at least prompt for it? I don't want this embedded in the Dockerfile.

like image 923
hookenz Avatar asked Mar 27 '18 01:03

hookenz


People also ask

How do I give my git clone a username and password?

username and password. We can supply the username and password along with the git clone command in the remote repository url itself. The syntax of the git clone command with the http protocol is, git clone http[s]://host. xz[:port]/path/to/repo.

How do I stop Git from asking for username and password?

You can avoid being prompted for your password by configuring Git to cache your credentials for you. Once you've configured credential caching, Git automatically uses your cached personal access token when you pull or push a repository using HTTPS.


1 Answers

If you want to git clone with user/password, all you need to do is

git clone https://username:[email protected]/username/repository.git

In your case it would be

RUN git clone https://username:[email protected]/username/repository.git

A better solution would be to create a set of private/public keys without a password and ADD or COPY the private key into your Dockerfile. In that case you will need to register the public key with github. Then the simple command will suffice:

RUN git clone ssh://[email protected]/username/repository.git

UPDATE

I also had an idea based on my answer here. If you need different usernames/passwords to use, you could set an ARG in your Dockerfile: ARG USERNAME=user ARG PASSWORD=1234 And then build your image with docker build --build-arg USERNAME=user --build-arg PASSWORD=1234 .` And you will get at least some simulation of user input :)

The solution I posted above (in "spoiler" now) is not safe. As per actual docs:

Warning: It is not recommended to use build-time variables for passing secrets like github keys, user credentials etc. Build-time variable values are visible to any user of the image with the docker history command.

like image 175
Alex Karshin Avatar answered Sep 19 '22 03:09

Alex Karshin