-1

I have django-app which has docker-compose:

services:
  web:
    build: .
    volumes:
      - .:/code
    ports:
      - "80:80"
    command: >
      sh -c "python manage.py wait_for_db &&
             python manage.py migrate &&
             python manage.py runserver 0.0.0.0:80"
    env_file:
      - ./.env
    depends_on:
      - db
    restart: always

(... here postgres, redis and celery)

and i have three questions about this:

  1. Doest it matter where we specify the ports? I think they should be after ports, but it works too.
  2. For example i want run my app on default 127.0.0.1:8000, but if i make:
ports:
    "8000":"8000"
...
    python manage.py runserver 127.0.0.1:8000

i cant get acess to my app. It works only on localhost when i runserver on 0.0.0.0:8000.
Can i run my app on default 127.0.0.1 or localhost only?

  1. Is there any "correct" order of components (build, volumes, ports, etc.) for django-app?
  • 3. There is no correct order while defining the values in docker compose it could be in any order. – sidharth vijayakumar Aug 23 '22 at 12:02
  • The YAML map structure is like a Python dictionary and order doesn't usually matter. [Docker app server ip address 127.0.0.1 difference of 0.0.0.0 ip](https://stackoverflow.com/questions/59179831/docker-app-server-ip-address-127-0-0-1-difference-of-0-0-0-0-ip) describes the Django `runserver` address argument (it _must_ be `0.0.0.0` in Docker). – David Maze Aug 24 '22 at 13:36

1 Answers1

1

Point 1 is really ambiguous (maybe there is a typo?). Ports belong under "- ports"

Point 2: when you access an application running in a docker container from the host, you are not accessing the local interface (127.0.0.1) but a special exposed interface. Only when you open a shell inside the container (so from inside the container) can you access the local interface. So all your applications running in containers should run on 0.0.0.0. If they run on 127.0.0.1, they can't be accessed from the host (but they will still run).

Point 3: There is no correct order. Usually they are generated in alphabetical order so many ops guys are used to arrange them in that way as well.

Mihai
  • 9,526
  • 2
  • 18
  • 40