'Running a django application on both the host machine and docker container at the same time

I have created a simple django application that has one endpoint /health/live and it returns a success message upon receiving a get request.

I run the application locally with python manage.py runserver on port 8000

I also have a docker-compose and Dockerfile as below:

FROM python

ENV PYTHONUNBUFFERED 1

RUN mkdir /inventory

WORKDIR /inventory

COPY . /inventory

WORKDIR /inventory

RUN pip install -r requirements.txt

and

version: '3'

networks:
 kong-net:
  name: kong-net
  driver: bridge
  ipam:
    config:
      - subnet: 172.1.1.0/24

services:
  inventory:
    container_name: inventory
    build:
      context: .
      dockerfile: Dockerfile
    restart: unless-stopped
    networks:
      kong-net:
        ipv4_address: 172.1.1.11
    ports:
      - "8000:8000"
    environment:
      DEBUG: 'true'
    command: python manage.py runserver 0.0.0.0:8000

I then run docker-compose up (I don't detach it to be able to see the logs)


They both work. I send a get request to http://127.0.0.1:8000/health/live:

  • based on the logs I see, the request goes through the service running directly on the system and not on the docker container
  • If I stop the service running directly without docker, and send the request, the request goes through the one deployed on docker

is there a reason this is happening? why the first one takes priority? And shouldn't I see an error when trying to run the docker container or start the application locally? because they are both listening to port 8000!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source