Connecting two containers using a network (django backend, react/nodejs frontend)

Greetings Community,

I am fairly new to docker. To get started with it used a small application using the REST API consisting of a nodejs/react frontend and a django backend which was created following this Tutorial.
I created two separate Dockerfiles

backend/Dockerfile

FROM python:3.9.4-slim-buster

WORKDIR /app

COPY requirements.txt requirements.txt

RUN pip3 install -r requirements.txt

EXPOSE 8000

COPY . .

CMD ["python3", "manage.py", "runserver", "localhost:8000"]

frontend/Dockerfile

FROM node:alpine

WORKDIR /app

ENV PATH ./node_modules/.bin:$PATH

COPY package.json ./

COPY package-lock.json ./

RUN npm install

COPY . ./

EXPOSE 3000

CMD ["npm", "start"]

and building and running them with Docker (running on my local machine (Archlinux)) using the following commands:

docker build -t django-backend --network=host .
docker build -t nodejs-frontend --network=host .

docker run --publish 8000:8000 django-backend
docker run --publish 3000:3000 nodejs-frontend

Both seem to use the network “bridge”. But still the frontend complains that the backend isn’t reachable. I also tried to replace all localhost appearances within the code as well as in the Dockerfile with 0.0.0.0 . I am not too experienced with network configurations and I am not sure how to correctly configure the network to make it work and what to consider for a development/experimental environment and inside a production (or whatever it is called) environment.

Usually it seems to be recommended to use docker compose and therefore create a .yaml file to handle an application using two containers. But I assume my problem is on a different level of understanding so that the use of docker compose wouldn’t change anything. Over and above that consider to further extend my experiments by trying to manage the containers using kubernetes later on. As far as I could understand you don’t use Docker compose with Kubernetes in a usual setup except when converting Docker compose files with Kompose.io or something to kubernetes setups. (Still learning, so take care of a lot of dangerous half-knowledge)

So basically I would like to know how I can setup my project to allow it to communicate in a development as well as a production setting.

Thank you very much in advance for your time and expertise!

Hello @foersben ,

You should use :
CMD [“python3”, “manage.py”, “runserver”, “0:8000”]
OR
CMD [“python3”, “manage.py”, “runserver”, “0.0.0.0:8000”]

instead of :
CMD [“python3”, “manage.py”, “runserver”, “localhost:8000”]