Question

Django, React, Docker: Bad Request 400 when connecting to Django app.

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.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Accepted 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.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel