Recently was working on a project which needed to build a docker image, but unfortunately it kept failing as below error was seen.
fatal: could not read Username for 'https://xxx.com': terminal prompts disabled
Based on the error, it looked like it was trying to pull code from remote Gitlab repository but failed as the terminal prompt is disabled.
At first glance, have a doubt why it needs terminal prompt to be enabled? It should just succeed and without prompting for anything. The only reason for prompting is that it needs user authentication to login to the remote Gitlab repo and fetch code.
However, the expectation is it should use ssh credentials to login and fetch codes. It looks like this is not the case here. With a closer look at the error, it says it tries to access the remote Gitlab repo with URL https://xxx.com. Here it should make sense now, as lots of developers have experienced this before where normal user authentication would be needed if accessing using HTTPS scheme.
In our case, we should use SSH, hence the URL should be something like git@xxx.com so that it can use SSH to login. To make it to use git@xxx.com, there is some configuration to be done.
git config --global url."git@xxx.com:".insteadOf "https://xxx.com/"
This tells git to replace https://www.com with git@xxx.com to access the remote Gitlab repo. Hence in the Dockerfile, just need to add one line.
RUN git config --global url."git@xxx.com:".insteadOf "https://xxx.com/"
With this, when building the docker image, it would first update the git config and then git should work without throwing the same error.