Setting up media directory

1) Make directories for media and static files in Dockerfile

2) Set MEDIA_URL and MEDIA_ROOT in settings.py

3) Add MEDIA_URL and MEDIA_ROOT in urls.py

Make directories and set permissions

Dockerfile
# Directories for storing media and static files
# -p allows creation of sub-directories if not available
RUN mkdir -p /vol/web/media
RUN mkdir -p /vol/web/static
# Create a user. -D represents this user can only run apps.
# This is recommended. Otherwise, the image will be run from root account
RUN adduser -D user
# Give permission to user to access volume dirs
RUN chown -R user:user /vol/
# This means owner can do everything, but rest can only read and execute
RUN chmod -R 755 /vol/web/
# Switch to this user
USER user

Configure settings

In settings.py

# 0.0.0.0:8000/static points to all static files
STATIC_URL = '/static/'
# 0.0.0.0:8000/media points to all media files
MEDIA_URL = '/media/'

# Location to store media and static files
MEDIA_ROOT = '/vol/web/media'
STATIC_ROOT = '/vol/web/static'

Map URLs

In urls.py

from django.contrib import admin
from django.urls import path, include
# to map static and media files
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/user/', include('user.urls')),
    path('api/recipe/', include('recipe.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# append the media url and its root from settings