<p>The problem was that I was declaring:</p>
<pre><code>ARG GITLAB_ACCESS_TOKEN
</code></pre>
<p>BEFORE the FROM statement and it has to be done AFTER it.</p>

CC BY-SA 4.0
2021-07-20T10:07:58.877Z
2021-07-20T10:07:58.877Z
dvilela
<p>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 <code>docker-compose.yml</code>:</p>
<pre><code>COPY my-repos.txt my-repos.txt
RUN 'cat my-repos.txt | while read REPO; do git clone https://oauth2:$GITLAB_ACCESS_TOKEN@gitlab2.&lt;domain&gt;.com:16443/$REPO.git; done'
</code></pre>
<p>But I'm getting the following error:</p>
<pre><code>/bin/sh: 1: cat my-repos.txt | while read REPO; do git clone https://oauth2:$GITLAB_ACCESS_TOKEN@gitlab2.&lt;domain&gt;.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.&lt;domain&gt;.com:16443/$REPO.git; done'' returned a non-zero code: 127
</code></pre>
<p>Running without quotes gives another error:</p>
<pre><code>Cloning into 'ML_tools'...
remote: HTTP Basic: Access denied
fatal: Authentication failed for 'https://gitlab2.&lt;domain&gt;.com:16443/&lt;correct_repo&gt;.git/'
The command '/bin/sh -c cat my-repos.txt | while read REPO; do git clone https://oauth2:$GITLAB_ACCESS_TOKEN@gitlab2.&lt;domain&gt;.com:16443/$REPO.git; done' returned a non-zero code: 128
</code></pre>
<p>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?</p>

CC BY-SA 4.0
2021-07-15T15:53:39.243Z
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
CC BY-SA 4.0
2021-07-15T18:45:08.680Z
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
CC BY-SA 4.0
2021-07-15T20:31:29.363Z
That means your environment variable is not being set correctly
Preston Martin
CC BY-SA 4.0
2021-07-15T22:07:06.047Z
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
CC BY-SA 4.0
2021-07-16T12:20:33.073Z
It sounds like you defined your access token as an `ENV` instead of an `ARG`.  https://vsupalov.com/docker-arg-vs-env/
chicks
<docker><docker-compose><dockerfile><bash>
Using access tokens on Dockerfile to clone repository: RUN command error
dvilela
# Using access tokens on Dockerfile to clone repository: RUN command error

(Asked by: username_0 on 2021-07-15)

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..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..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..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..com:16443/.git/'
The command '/bin/sh -c cat my-repos.txt | while read REPO; do git clone https://oauth2:$GITLAB_ACCESS_TOKEN@gitlab2..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?

**Tags:** docker, docker-compose, dockerfile, bash

> **Comments:**
> **username_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....  
> **username_0**: 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.  
> **username_1**: That means your environment variable is not being set correctly  
> **username_0**: 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: ***********.  
> **username_2**: It sounds like you defined your access token as an `ENV` instead of an `ARG`.  https://vsupalov.com/docker-arg-vs-env/  

---

## 1 answers

### Answer by username_0 on 2021-07-20. Score: 1 | Accepted answer

The problem was that I was declaring:
ARG GITLAB_ACCESS_TOKEN

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


---

