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.
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.
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.
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 yourDockerfile
:ARG USERNAME=user ARG PASSWORD=1234
And then build your image withdocker 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With