0

I have a Python script that creates a folder and writes a file in that folder. I can open the file and see its contents, but unfortunately I cannot edit it. I tried to add the command, RUN chmod -R 777 . but that didn't help either. In the created files and folders I see a lock sign as follows -
enter image description here enter image description here
I have been able to recreate the same on a small demo. The contents are as follows -

demo.py

from pathlib import Path
Path("./created_folder").mkdir(parents=True, exist_ok=True)
with open("./created_folder/dummy.txt", "w") as f:
    f.write("Cannot edit the contents of this file")

Dockerfile

FROM python:buster

COPY . ./data/
WORKDIR /data
RUN chmod -R 777 .

CMD ["python", "demo.py"]

docker-compose.yaml

version: "3.3"
services:
  python:
    working_dir: /data
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - .:/data/

After making these files run docker-compose up --build and see the results and then try to edit and save the created file dummy.txt - which should fail.

Any idea how to make sure that the created files can be edited and saved on the host?

EDIT:

  1. I am running docker-compose rootless. I had read that it is not a good idea to run docker with sudo, so I followed the official instructions on adding user group etc.
  2. I actually run the command docker-compose up --build not just docker compose up
  3. I am on Ubuntu 20.04
  4. username is same for both
    ~$ grep /etc/group -e "docker"
    docker:x:999:username
    ~$ grep /etc/group -e "sudo"
    sudo:x:27:username
    
  5. Tried using PUID and PGID environment variables... but still the same issue. Current docker-compose file -
    version: "3.3"
    services:
      python:
        working_dir: /data
        build:
          context: .
          dockerfile: Dockerfile
        volumes:
          - .:/data/
        environment:
          - PUID=1000
          - PGID=1000
    
jar
  • 2,646
  • 1
  • 22
  • 47
  • I'm not sure what the lock icon means. What are the permission bits of the output? (eg run `ls -l dummy.txt`) Also, are you running docker compose rootless? (did you call docker-compose with sudo) – petrucci4prez Feb 06 '21 at 19:56
  • I can't repro (macOS Catalina); the directory is created with mode 755 and the file within it with mode 644, owned by myself. – tripleee Feb 06 '21 at 20:22
  • (`chmod 777` on the directory seems vaguely idiotic but probably not unsafe in this context.) – tripleee Feb 06 '21 at 20:23
  • I also get exactly what @tripleee is seeing (arch linux with rootless docker) – petrucci4prez Feb 06 '21 at 20:26
  • If the primary goal of your application is to read and write host files, it will be much easier to run it directly on the host, not in Docker. This is doubly true if your application is in a widely available scripting language like Python. – David Maze Feb 06 '21 at 20:37
  • @petrucci4prez Yes, I am running it without sudo. I had read that it is not a good idea to run docker with sudo, so I followed the official instructions on adding user group etc. – jar Feb 07 '21 at 04:26
  • @tripleee I see what you mean. But I thought that since the directory is forming on the host giving it all permissions might work. Clearly that is not the case. – jar Feb 07 '21 at 04:27
  • @DavidMaze I understand. I cannot share the actual project with everyone here, so this is a contrived example. Moreover, the purpose of Docker is that people be able to use packages without installing it on their own. So I think whatever I am doing is just fine. It just needs some refining, – jar Feb 07 '21 at 04:29
  • @petrucci4prez I get the following on dummy.txt `-rw-r--r-- 1 root root 37 Feb 7 00:48 dummy.txt` – jar Feb 07 '21 at 04:38

2 Answers2

0

It seems like the Docker group is different from your user group and you're misunderstanding the way Docker RUN works.

Here's what happens when you run Docker build:

  • You pull the python:buster images
  • You copy the contents of the current directory to the docker image
  • You set the Work directory to the existing data directory
  • You set the permissions of the data directory to 777
  • Finally, the CMD is set to indicate what program should run

When do a docker-compose, the RUN command has no effect, but it's an Dockerfile instruction and not a runtime command. When your container runs, the writes the files with the user/group of the Docker command which your user doesn't have the permissions to edit.

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
  • My group output is - ```~$ grep /etc/group -e "docker" docker:x:999:username ~$ grep /etc/group -e "sudo" sudo:x:27:username``` It is the same username for both – jar Feb 07 '21 at 04:47
0

Docker container runs in an isolated environment so that the UID/GID to user name/group name mapping is not shared to the process inside containers. You are facing two problems.

  1. The files created inside containers is owned by root:root (by default, or a randomly chosen UID by the image)
  2. The files created with permission 644 or 755 that not editable by host user.

You could refer to images provided by linuxserver.io to learn how they solve the two problems. for example qBittorrent. They provide two environment variables PUID and PGID to solve problem 1. UMASK option to solve problem 2.

Below is the related implementation in their custom Ubuntu base image. You could use that as your image base as well.

the place PUID and PGID is handled:

https://github.com/linuxserver/docker-baseimage-ubuntu/blob/b529d350b1/root/etc/cont-init.d/10-adduser

the place UMASK is handled:

https://github.com/linuxserver/docker-baseimage-ubuntu/blob/b529d350b1438aa81e68a5d87eff39ade0f1c879/root/usr/bin/with-contenv#L2-L5

Personally, I'd prefer setting PUID and PGID to the ones in accord with the host (could get from id -u and id -g) and NOT touching UMASK unless absolutely necessary.

ttimasdf
  • 1,367
  • 1
  • 12
  • 19
  • I tried adding the PUID and PGID as environment variables to my docker-compose file and then running as usual with `docker-compose up --build` but unfortunately I get the same read only file. Regarding that QBitTorrent thing you mentioned...it looks like they are using some sort of `init.d` file and based on this post https://stackoverflow.com/questions/26938684/docker-io-init-d-script-not-working-on-start-container I think it may not be possible to get it running without using some other softwares. I really didn't want to complicate things so much for such a seemingly simple task. – jar Feb 07 '21 at 06:32