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/install_packages.sh inside and executes it:

Dockerfile
COPY scripts/install_packages.sh .
RUN ./install_packages.sh

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

GitLab CI/CD Docker error
 Step 5/5 : RUN ./install_packages.sh && rm install_packages.sh
 ---> Running in 130ab2b31ea5
/bin/sh: 1: ./install_packages.sh: Permission denied
The command '/bin/sh -c ./install_packages.sh' returned a non-zero code: 126
Running after_script
00:01
Uploading artifacts for failed job
00:02
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 install_packages.sh*

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/install_packages.sh