2

I'm trying to find a workflow with Docker and Django. Currently, I'm using the basic configuration from the docker documentation.

I'd like to use manage.py startapp directly from the container to start a new app using:

docker-compose run web ./manage.py startapp myapp

But all the files created in the volume are owned by the root user and not by myself, so I can't edit them from the host.

My idea is to avoid installing all the requirements on my host machine but maybe I should not create app from the container?

One possible solution is to create a user and make it having the same UID/GID than my user on my host machine but it won't work if I try to use an other account on my host machine...

Any suggestion?

Antoine Fontaine
  • 792
  • 7
  • 16
  • You should add a `USER xx` in your Dockerfile, xx being a non-privileged user, that you created before in your Dockerfile an extract from https://docs.docker.com/articles/dockerfile_best-practices/ "with something like RUN groupadd -r postgres && useradd -r -g postgres postgres" – user2915097 Jul 14 '15 at 22:28
  • an example of Dockerfile with useradd and USER https://forum.ubuntu-fr.org/viewtopic.php?id=1809831 – user2915097 Jul 14 '15 at 22:39
  • It works because my user account on the host machine has the same UID/GID than the user created in the container. However, it won't work if I use another account on my host machine :/ – Antoine Fontaine Jul 14 '15 at 23:12
  • You can uid gid as parameters for the Dockerfile – user2915097 Jul 15 '15 at 13:52

1 Answers1

1

What worked best for me was avoiding (or minimizing) file creation inside the containers.

My Dockerfile would just copy the requirements.txt and install them; and the container would access the app files through a mounted volume.

I pass the env var PYTHONDONTWRITEBYTECODE=1 to the containers, so python does not create *.pyc/*.pyo files.

The few times I cannot avoid it (like, ./manage.py makemigrations), I run chown afterwards. It's not ideal, but as this happens rarely for my case, I don't bother.

Fedalto
  • 1,507
  • 1
  • 10
  • 13