Search code examples
djangodockerdocker-composefilesystemsdocker-volume

Folder creation in docker-compose volume


I have a Django project running with docker-compose for dev env. I'm struggling with file/folder creation on an aws server (AMI ubuntu-bionic)

I mounted a volume on my backend_container with the projects files. On my local computer, file/folder creation from django inside the container and to the volume works well. On the server everything seem to work as expected (no errors whatsoever), but when I ls to find my folders/files, they aren't there.

here is the backend service of my docker-compose.yml file

  backend:
    container_name: project_backend_container
    env_file:
      - ../project-backend/set_env.env
    build:
      context: ../project-backend
    command: bash -c "python3 manage.py runserver 0.0.0.0:8000"
    volumes:
      - ../project-backend/project:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

I tried to debug it myself, but so far : when I exec into the container, I can create folders

~/project-backend$ docker exec -it project_backend_container /bin/bash
root@d34b85516a59:/code# mkdir test
root@d34b85516a59:/code# ls
__pycache__  app  manage.py static  test  uploads project

when I run the shell with django, I can also create folders

root@d34b85516a59:/code# python manage.py shell
Python 3.7.4 (default, Sep 12 2019, 15:40:15)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import os
>>> os.makedirs('my/awesome/dirs')
>>>
now exiting InteractiveConsole...
root@d34b85516a59:/code# ls
__pycache__/ app/         manage.py    my/          static/      uploads/

I tried to override django storage class to log errors, but everything is going fine. From the django side, everything looks alright.

My knowledge on docker layers and filesystem management for volume is clearly lacking but I think my problem is related to them. Has someone already encountered a similar issue and know how to resolve, or at least, debug it ? Thanks


Solution

  • turns out It was a configuration issue ! My settings was using an absolute path for MEDIA_ROOT in an environment file. MEDIA_ROOT is the filesystem path to the directory that will hold user-uploaded files in Django projects.

    The path to the uploads folder in the container was not the same as the given path in the env file.