Estoy teniendo un problema utilizando Django REST Framework App en un entorno de docker compose en modo productivo. El problema ocurre cuando un endpoint de la API retorna la ruta de un campo ImageField
de un modelo, regresa la url sin el puerto del host, con los archivos estaticos no tengo problema.
Ejemplo:
Valor actual: http://127.0.0.1/media/machines/034793bb-8516-45e3-a50a-4e00e7488617.png
Esperado: http://127.0.0.1:8000/media/machines/034793bb-8516-45e3-a50a-4e00e7488617.png
Settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
STATIC_DIR = os.path.join(BASE_DIR,'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATIC_ROOT = '/app/static/'
** Docker file **
FROM python:3.6
ENV PYTHONUNBUFFERED 1
RUN mkdir /app
WORKDIR /app
ADD . /app/
RUN pip install -r requirements.txt
CMD ["gunicorn", "-c", "config/gunicorn/conf.py", "--bind", ":8000", "--chdir",
"app_api.wsgi:application"]
Docker-compose
version: '3.7'
services:
web-service:
build: .
command: bash -c "pip install -r requirements.txt && python manage.py makemigrations && python
manage.py migrate && python manage.py collectstatic --noinput && python manage.py runserver
0.0.0.0:8000"
container_name: app_backend
volumes:
- static:/app/static
- media:/app/media
- .:/app
depends_on:
- db
restart: on-failure:2
db:
image: postgres
container_name: app_postgres
environment:
- POSTGRES_USER=bd_user
- POSTGRES_PASSWORD=password
- POSTGRES_DB=new_database
ports:
- "5432:5432"
volumes:
- postgres:/var/lib/postgresql/data
restart: on-failure:2
nginx:
image: nginx:latest
ports:
- 8000:80
volumes:
- ./config/nginx/conf.d:/etc/nginx/conf.d
- static:/app/static
- media:/app/media
depends_on:
- web-service
volumes:
media:
static:
postgres:
driver: local
Nginx config file
upstream django_server {
server web-service:8000;
}
server {
listen 80;
server_name localhost;
location /static/ {
alias /app/static/;
}
location /media/ {
alias /app/media/;
}
location / {
proxy_pass http://django_server;
proxy_set_header X-Fowarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
}
** Gunicorn config **
name = 'docker_django'
loglevel = 'info'
errorlog = '-'
accesslog = '-'
workers = 2
Model
class Machine(models.Model):
name = models.CharField(max_length=100, blank=False, null=False)
description = models.CharField(max_length=250, blank=True, null=True)
image = models.ImageField(upload_to = 'machines', default='default.png')
provider = models.TextField(blank=True, null=True)
model = models.CharField(max_length=250, blank=True, null=True)
data_source = models.ForeignKey(DataSource, related_name='data_source', on_delete=models.CASCADE)
def __str__(self):
return self.name