Search code examples
pythonlinuxdockerdebian

How to allow docker to read files with root permissions


So I'm trying to run my FastAPI python app in a Docker container. I choose python:3.9 as a base image and everything seemed to work until I decided to integrate my SSL Cert-Files into the container.

Dockerfile:

FROM python:3.9

WORKDIR /app

COPY requirements.txt requirements.txt

RUN pip3 install -r requirements.txt

RUN mkdir -p /app/SSL

VOLUME /etc/letsencrypt/live/soulforger.net/:/app/SSL/

COPY . .

CMD [ "uvicorn", "core:app", "--host", "0.0.0.0", "--port", "8000", "--ssl-keyfile", "/app/SSL/privkey.pem", "--ssl-certfile", "/app/SSL/cert.pem" ]

EXPOSE 8000

Docker run command:sudo docker run -p 33665:8000 -v /etc/letsencrypt/live/soulforger.net/:/app/SSL --name soulforger_api -d 24aea28ce756

Now the problem is that the directory im mapping is only accessible as a root user. When I exec into the Container, the files are there but I can't cat /app/SSL/cert.pem. Due to the fact that I can cat everything else without problem I assume its some sort of permissions problem when mapping the dir into the container. Does anybody have an idea of what can cause this issue?

Solution: After a lot of digging I found out what the problem is, for anyone that happens upon this post and also uses Let's Encrypt, the files within /etc/letsencrypt/live/some.domain/ are only links to files in another directory. If you want to mount the SSL certificates of your server to your containers, you have to mount the entire /etc/letsencrypt/ dir in order to have access to the files referenced by the links. All props go to this answer.


Solution

  • You can change the user in the Dockerfile. Try to add USER root in your dockerfile. Hopefully it will be helpful.

    FROM python:3.9
    
    USER root
    
    WORKDIR /app
    
    COPY requirements.txt requirements.txt
    
    RUN pip3 install -r requirements.txt
    
    RUN mkdir -p /app/SSL
    
    VOLUME /etc/letsencrypt/live/soulforger.net/:/app/SSL/
    
    COPY . .
    
    CMD [ "uvicorn", "core:app", "--host", "0.0.0.0", "--port", "8000", "--ssl-keyfile", "/app/SSL/privkey.pem", "--ssl-certfile", "/app/SSL/cert.pem" ]
    
    EXPOSE 8000