Antoine
Antoine

Reputation: 587

Django server in a Docker

I'm starting to develop an application with a Django back-end, and I wish to do it inside a Docker. I almost managed to do it, but I'm still having an issue. Currently I have two containers running :

docker ps

The first one contains my django app and the complete command is

python3 manage.py runserver 0.0.0.0:8000

and the second one is hosting my database.

My docker-compose.yml file is this one :

version: '3'
services:
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD : root
      MYSQL_DATABASE : ml_gui
  back:
    build: ./back/
    command: python3 manage.py runserver
    ports:
      - "8000:8000"
    depends_on:
      - db

and my django settings concerning the database is :

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'ml_gui',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': 'db',
        'PORT': '3306',
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
        },
        'TEST': {
            'CHARSET': 'utf8',
            'COLLATION': 'utf8_general_ci',
        },
    },
}

The problem is, when I do requests outside of the container (I've tried in my browser, with curl and with Postman) on localhost:8000, I have no answer. But, when I do the same request inside the container with curl, it works.

How could I make those requests work from outside of the containers ?

Upvotes: 5

Views: 502

Answers (1)

Mazel Tov
Mazel Tov

Reputation: 2182

you should never run ./manage.py runserver in production, it is only for testing... also if you dont specify the IP address that the server listens to it is available only for localhost (see here)

so in you docker-compose.yml the command should be

./manage.py runserver 0:8000

Upvotes: 2

Related Questions