git clone Authentication failed while building Docker

1k Views Asked by At

I'm working on a Dockerfile. It's an Ubuntu 18.04 image with ROS melodic, I need to clone a few repositories while building the Docker image using git.

Normally, I'd generate a new SSH key and add that key to my GitHub account but things are different now because to do what I typically do, I'll have to build the Docker image, run it, and then clone (& build) the git repositories.

Also, I will be distributing this Docker image, and I don't really want to link these repositories to my account with an SSH key.

So far this is what I tried:

# Create a non-root user whom can sudo
RUN useradd -ms /bin/bash -G sudo <username>
RUN echo '<username>:<password>' | chpasswd

# Create a workspace and set WORKDIR
<...>

# Set git defaults
COPY --chown=<username>:<username> .gitconfig /home/<username>
COPY .gitconfig /root

# Clone the git repository
ARG ghname
ARG ghtoken
RUN git clone https://$ghname:[email protected]/<organization>/<repo>.git

This attempt failed:

Step 20/30 : Copy --chown=<username>:<username> .gitconfig /home/<username>
COPY failed: file not found in build context or excluded by .dockerignore: stat .gitconfig: file does not exist
...
...
Cloning into '<repo>'...
remote: Repository not found.
fatal: Authentication failed for 'https://:@github.com/<organization>/<repo>.git/'

Then I tried:

# Create a non-root user whom can sudo
RUN useradd -ms /bin/bash -G sudo <username>
RUN echo '<username>:<password>' | chpasswd

# Create a workspace and set WORKDIR
<...>

# Clone the git repository
git clone [email protected]:<organization>/<repo>.git

This attempt failed (as expected):

Cloning into '<repo>'...
fatal: unable to fork
The command '/bin/sh -c git clone [email protected]:<organization>/<repo>.git' returned a non-zero code: 128
0

There are 0 best solutions below