1

In a Dockerfile, I'm trying to clone several Gitlab repositories that are specified in a file, one per line, using an access token passed as an environment variable set in docker-compose.yml:

COPY my-repos.txt my-repos.txt
RUN 'cat my-repos.txt | while read REPO; do git clone https://oauth2:$GITLAB_ACCESS_TOKEN@gitlab2.<domain>.com:16443/$REPO.git; done'

But I'm getting the following error:

/bin/sh: 1: cat my-repos.txt | while read REPO; do git clone https://oauth2:$GITLAB_ACCESS_TOKEN@gitlab2.<domain>.com:16443/$REPO.git; done: not found
The command '/bin/sh -c 'cat my-repos.txt | while read REPO; do git clone https://oauth2:$GITLAB_ACCESS_TOKEN@gitlab2.<domain>.com:16443/$REPO.git; done'' returned a non-zero code: 127

Running without quotes gives another error:

Cloning into 'ML_tools'...
remote: HTTP Basic: Access denied
fatal: Authentication failed for 'https://gitlab2.<domain>.com:16443/<correct_repo>.git/'
The command '/bin/sh -c cat my-repos.txt | while read REPO; do git clone https://oauth2:$GITLAB_ACCESS_TOKEN@gitlab2.<domain>.com:16443/$REPO.git; done' returned a non-zero code: 128

The thing is, if I comment out the RUN line, build the image and enter the container, running manually the command works perfectly. The environment variable is correctly set. Is maybe some variable out of scope at build time?

5
  • 1
    Are you sure it's interpolating the variable $GITLAB_ACCESS_TOKEN correctly? If that is your exact output, I don't think it is....
    – Preston Martin
    Jul 15 at 15:53
  • There is something shady indeed. If I do RUN echo $GITLAB_ACCESS_TOKEN > /token.txt, the file is empty. But when I do it from inside the container, it works.
    – dvilela
    Jul 15 at 18:45
  • That means your environment variable is not being set correctly
    – Preston Martin
    Jul 15 at 20:31
  • It is correct on the container, but not during build maybe? (I do not understand yet how all this works). I'm setting it from docker-compose.yml: environment: GITLAB_ACCESS_TOKEN: ***********.
    – dvilela
    Jul 15 at 22:07
  • It sounds like you defined your access token as an ENV instead of an ARG. vsupalov.com/docker-arg-vs-env
    – chicks
    Jul 16 at 12:20
1

The problem was that I was declaring:

ARG GITLAB_ACCESS_TOKEN

BEFORE the FROM statement and it has to be done AFTER it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.