Running docker container non-root user permission denied

Viewed 2431

I have an issue while running docker container as non-root user. My DockerFile is



.....ARG SSH_KEY

RUN mkdir root/.ssh/ &&\
    chmod 0700 /root/.ssh/ &&\
    ssh-keyscan -t (url) >> root/.ssh/known_hosts &&\ 
    echo "{SSH_KEY}" > /root/.ssh/id_rsa &&\
    chmod 600 ~/.ssh/id_rsa

COPY host_config root/.ssh/config

ADD ./entrypoint.sh /apps/entrypoint.sh
RUN chmod -R 755 /apps/
ENTRYPOINT "./apps/entrypoint.sh"


entrypoint.sh file contains git commands

git clone ssh://repository.com

Docker-compose file

user: 5555:5555

host_config file

Host *
IdentityFile /root/.ssh/id_rsa
StrictHostKeyChecking no
UserKnownHostsFile

When I use user option in docker-compose file it gives an error "fatal:could not create work tree dir repository name Permission denied", but when I run docker container with root user it works.

When I create new directory "example" in DOCKERFILE and add do chown that non-root user to that "example" folder then try git clone in that folder it gives an error "Host key verification failed".

Is there any way of running docker container that has script file with git commands with non-root user?

1 Answers

There are several elements which seem a bit off in the script you posted :

  • .ssh folder is created and set up in /root folder (not $HOME or ~ or ...), which is the explicit home folder of root (not of "current user")
    the following should work to set it in "my home" rather than "root's home" :

     cd &&
     mkdir .ssh &&
     chmod 0700 .ssh && ...
    
  • likewise : host_config is copied to root's home
    see suggestions here : Dockerfile: $HOME is not working with ADD/COPY instructions

  • you set up the .ssh/id_rsa file, which would be the private key, but you do not set the publickey

  • etc ...

To debug the ssh connection from within the container : add a RUN ssh -v repository.com instruction (or -vv or -vvv if you need more debug info) in your Dockerfile and check the output.

Related