I am trying to set up a Django back-end, with a React front-end, both running inside a Docker container, using docker-compose to expose them at different ports (8000 for the Django app, 3000 for the React app. When I navigate to http://my_ip_address:3000/ the React app loads just fine, but when I navigate to http://my_ip_address:8000 I get a 400 Bad Request error from the server. Here is the code ->

project/back-end/Dockerfile

FROM python:3.7

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

WORKDIR /nerdrich

COPY Pipfile Pipfile.lock /nerdrich/
RUN pip install pipenv && pipenv install --system

COPY . /nerdrich/
EXPOSE 8000

project/front-end/Dockerfile

# official node.js runtime for Docker
FROM node:12
# Create and set the directory for this container
WORKDIR /app/
# Install Application dependencies
COPY package.json yarn.lock /app/
RUN yarn install --no-optional
# Copy over the rest of the project
COPY . /app/

# Set the default port for the container
EXPOSE 3000

CMD yarn start

project/docker-compose.yml

version: "3"

services:
  web:
    build: ./back-end
    command: python /nerdrich/manage.py runserver 0.0.0.0:8000
    volumes:
      - ./back-end:/nerdrich
    ports:
      - "8000:8000"
    stdin_open: true
    tty: true
  client:
    build: ./front-end
    volumes:
      - ./front-end:/app
      - /app/node_modules
    ports:
      - '3000:3000'
    stdin_open: true
    environment:
      - NODE_ENV=development
    depends_on:
      - "web"
    command:
      yarn start

project/back-end/nerdrich/.env

ALLOWED_HOSTS=['165.227.82.162']

I can provide any additional information you need, and as I said, the front-end works just fine, it is just the back-end that gives the 400 Bad Request error.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Here was the problem: Apparently using the .env file – even after placing print(ALLOWED_HOSTS) to ensure the host was found – does not actually work for some reason. I simply had to restore the ALLOWED_HOSTS settings back in the settings.py file.

Submit an Answer