chmod: changing permissions of 'myscript.sh' : Operation not permitted

53,209

Solution 1

Set the permissions before you build the image

chmod +x scripts/myScript.sh
docker build .

docker will keep the permissions when it copies the files.

Solution 2

Changing permissions of files you do not own in Linux requires root access, and the COPY command is most likely copying the file as root. You can change back to the sonarqube user after fixing the permissions. Here's an example Dockerfile for that:

FROM sonarqube:7.7-community
COPY plugins/ /plugins/
COPY scripts/ /scripts/
COPY conf/ /conf/
COPY bin/ /bin/
USER root
RUN chmod 755 /scripts/myScript.sh
USER sonarqube
ENTRYPOINT ["/scripts/myScript.sh"]

Note I've also switched from ADD to COPY since you are not pulling remote URL's or extracting tar/zip files. The * is also implied when copying a directory. For a single script, you do not need the -R recursive option, and I'm explicit with the permissions since shell scripts also require read access by all users.

If you want the script to be owned by sonarqube instead, you could run:

COPY --chown=sonarqube:sonarqube scripts/ /scripts/

That should allow the user to change permissions on the script without a USER change, but may give that user more access than you desire to modify the script inside the container.

Solution 3

Check permissions on the /scripts directory using ls -ld scripts. It might be that your user does not have write permission on this directory.

Solution 4

use sudo chmod instead of just using chmod

Share:
53,209

Related videos on Youtube

JYOTI PRAKASH MALLICK
Author by

JYOTI PRAKASH MALLICK

Updated on September 18, 2022

Comments

  • JYOTI PRAKASH MALLICK
    JYOTI PRAKASH MALLICK over 1 year

    While running the following docker file I am getting "chmod: changing permissions of '/scripts/entrypoint.sh': Operation not permitted" error.

    FROM sonarqube:7.7-community
    ADD plugins/* /plugins/
    ADD scripts/* /scripts/
    ADD conf/* /conf/
    ADD bin/* /bin/
    RUN chmod -R a+X /scripts/myScript.sh
    ENTRYPOINT ["/scripts/myScript.sh"]
    

    But when I add USER root it is working. But I don't want to run it with root. Any way I can run it without root ?

    • GeralexGR
      GeralexGR almost 5 years
      You must check the execute permissions of the file entrypoint.sh. If you want to execute it by a specific user, this user should have execute permissions on this file
  • JYOTI PRAKASH MALLICK
    JYOTI PRAKASH MALLICK almost 5 years
    I guess the only root has the access to this folder, drwxr-xr-x 2 root root 4096 May 16 11:57 /scripts
  • JYOTI PRAKASH MALLICK
    JYOTI PRAKASH MALLICK almost 5 years
    Any way we can provide permission to any user?
  • BMitch
    BMitch over 4 years
    You do not sudo from inside of containers. Allowing that would break the security of running commands as a non-root user.
  • JYOTI PRAKASH MALLICK
    JYOTI PRAKASH MALLICK over 4 years
    But the default user for a docker container is root. I will try with sudo as well.