How to server Django media files on apache2 and docker

Viewed 15

I'm Running a django app on apache2 using docker, everything works great but when i try to open an uploaded file ( media file) it says an URL not found error like : physical/path/to/file/filename.jpg doesnt exsits so it converts the url to a phyical path from inside the docntainer..

Here are my files:

DockerFile

FROM ubuntu

RUN apt-get update

# Avoid tzdata infinite waiting bug
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Africa/Cairo

RUN apt-get install -y apt-utils vim curl apache2 apache2-utils
RUN apt-get -y install python3 libapache2-mod-wsgi-py3
RUN ln /usr/bin/python3 /usr/bin/python
RUN apt-get -y install python3-pip
#Add sf to avoid ln: failed to create hard link '/usr/bin/pip': File exists
RUN ln -sf /usr/bin/pip3 /usr/bin/pip
RUN pip install --upgrade pip
RUN pip install django ptvsd
COPY www/demo_app/water_maps/requirements.txt requirements.txt
RUN pip install -r requirements.txt
ADD ./demo_site.conf /etc/apache2/sites-available/000-default.conf
EXPOSE 80 5432
WORKDIR /var/www/html/demo_app
#CMD ["apache2ctl", "-D", "FOREGROUND"]
#CMD ["python", "manage.py", "migrate", "--no-input"]

docker-compose.yaml

version: "2"

services:

  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
      - ./data/static:/var/www/html/demo_app/static
      - ./data/upload:/var/www/html/demo_app/upload
    ports:
      - '5432:5432'
    environment:
      - POSTGRES_DB=database_innvoentiq
      - POSTGRES_USER=database_user_innvoentiq
      - POSTGRES_PASSWORD=mypass@
      - PGDATA=/tmp
  django-apache2:
    build: .
    container_name: water_maps
    environment:
      - POSTGRES_DB=database_innvoentiq
      - POSTGRES_USER=database_user_innvoentiq
      - POSTGRES_PASSWORD=mypass@
      - PGDATA=/tmp
    ports:
      - '80:80'
    volumes:
      - ./www/:/var/www/html
#    command: sh -c 'python manage.py migrate && python manage.py loaddata db_backkup.json && apache2ctl -D FOREGROUND'
    command: sh -c 'python manage.py migrate && apache2ctl -D FOREGROUND'

    depends_on:
      - db

demo_Site.conf

WSGIPythonPath /var/www/html/demo_app

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/

       Alias /static /var/www/html/demo_app/static

        WSGIScriptAlias / /var/www/html/demo_app/water_maps/wsgi.py

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn
<Directory /var/www/html/demo_app/static>
Require all granted
</Directory>

<Directory /var/www/html/demo_app/upload>
Require all granted
</Directory>
<Directory /var/www/html/demo_app/water_maps>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

Settings.py

STATIC_ROOT = os.path.join(BASE_DIR, "static")
MEDIA_ROOT = os.path.join(BASE_DIR, "upload/upload")

STATIC_URL = 'static/'
MEDIA_URL = 'upload/'

and when i open a media file it shows this error :

enter image description here

despite the file already exists in the mentioned physical address. but it is not an URL

0 Answers
Related