Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't modify files created in docker container

I got a container with django application running in it and I sometimes go into the shell of the container and run ./manage makemigrations to create migrations for my app.
Files are created successfully and synchronized between host and container.
However in my host machine I am not able to modify any file created in container.

This is my Dockerfile

FROM python:3.8-alpine3.10
LABEL maintainer="Marek Czaplicki <marek.czaplicki>"

WORKDIR /app

COPY ./requirements.txt ./requirements.txt

RUN set -ex; \
        apk update; \
        apk upgrade; \
        apk add libpq libc-dev gcc g++ libffi-dev linux-headers python3-dev musl-dev pcre-dev postgresql-dev postgresql-client swig tzdata; \
        apk add --virtual .build-deps build-base linux-headers; \
        apk del .build-deps; \
        pip install pip -U; \
        pip --no-cache-dir install -r requirements.txt; \
        rm -rf /var/cache/apk/*; \
        adduser -h /app -D -u 1000 -H uwsgi_user


ENV PYTHONUNBUFFERED=TRUE

COPY . .
ENTRYPOINT ["sh", "./entrypoint.sh"]
CMD ["sh", "./run_backend.sh"]

and run_backend.sh


./manage.py collectstatic --noinput
./manage.py migrate && exec uwsgi --strict uwsgi.ini

what can I do to be able to modify these files in my host machine? I don't want to chmod every file or directory every time I create it.
For some reason there is one project in which files created in container are editable by host machine, but I cannot find any difference between these two.

like image 782
Marek Avatar asked Oct 15 '25 20:10

Marek


2 Answers

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.

like image 54
Itamar Turner-Trauring Avatar answered Oct 17 '25 11:10

Itamar Turner-Trauring


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)

like image 44
Georgi Velev Avatar answered Oct 17 '25 12:10

Georgi Velev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!