Sam Hooke

Permission denied in Docker container upon COPY and RUN of a file from git

TL;DR: Using COPY to copy files into a Docker container retains the permissions from the host system. If host obtains the files from git, make sure that file attributes are set correctly in git to avoid getting a “Permission denied” error when using RUN to execute these files in the Docker container.

Situation: Have a Dockerfile which copies scripts/ inside and executes it:

Dockerfile §
COPY scripts/ .
RUN ./

Problem: The execution always fails with “Permission denied” when the Docker container runs in GitLab, even though execute permissions are granted on on the local file system.

GitLab CI/CD Docker error §
 Step 5/5 : RUN ./ && rm
 ---> Running in 130ab2b31ea5
/bin/sh: 1: ./ Permission denied
The command '/bin/sh -c ./' returned a non-zero code: 126
Running after_script
Uploading artifacts for failed job
ERROR: Job failed: exit code 126
Verifying local execute permissions §
$ ls scripts/ -lrth
total 1.0K
-rwxr-xr-x 1 sam 1234567 534 Oct 16 11:14*

Solution: Use git update-index --chmod+x <filename> to grant the execute permissions within git, so that when GitLab clones the file it has the execute permission.

git update-index --chmod=+x scripts/