[Django]-Can't modify files created in docker container

3๐Ÿ‘

By default, Docker containers runs as root. This has two issues:

  1. In development as you can see, the files are owned by root, which is often not what you want.
  2. In production this is a security risk (https://pythonspeed.com/articles/root-capabilities-docker-security/).

For development purposes, docker run --user $(id -u) yourimage or the Compose example given in the other answer will match the user to your host user.

For production, youโ€™ll want to create a user inside the image; see the page linked above for details.

2๐Ÿ‘

Usually files created inside docker container are owned by the root user of the container.

You could try with this inside your container:

chown 1000:1000 file-you-want-to-edit-outside

You could add this as the last layer of your Dockerfile as RUN

Edit:

If you are using docker-compose, you can add user to your container:

service:
  container:
    user: ${CURRENT_HOST_USER}

And have CURRENT_HOST_USER be equal to $(id -u):$(id -g)

๐Ÿ‘คGeorgi Velev

0๐Ÿ‘

The solution was to add

USER uwsgi_user

to Dockerfile and then simpy run docker exec -it container-name sh

๐Ÿ‘คMarek

Leave a comment