Docker Community Forums

Share and learn in the Docker community.

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


FROM python:3.9.4-slim-buster


COPY requirements.txt requirements.txt

RUN pip3 install -r requirements.txt


COPY . .

CMD ["python3", "", "runserver", "localhost:8000"]


FROM node:alpine


ENV PATH ./node_modules/.bin:$PATH

COPY package.json ./

COPY package-lock.json ./

RUN npm install

COPY . ./


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

Step 1 — Creating a Python Virtual Environment and Installing Dependencies
In this step, we’ll create a virtual environment and install the required dependencies for our application, including Django, the Django REST framework, and django-cors-headers.

Our application will use two different development servers for Django and React. They will run on different ports and will function as two separate domains. Because of this, we need to enable cross-origin resource sharing (CORS) to send HTTP requests from React to Django without being blocked by the browser.

Navigate to your home directory and create a virtual environment using the venv Python 3 module:

cd ~
python3 -m venv ./env

Activate the created virtual environment using source:

source env/bin/activate

Next, install the project’s dependencies with pip. These will include:

Django: The web framework for the project.
Django REST framework: A third-party application that builds REST APIs with Django.
django-cors-headers: A package that enables CORS.
Install the Django framework:

pip install django djangorestframework django-cors-headers

With the project dependencies installed, you can create the Django project and the React frontend.

@lewish95 why should I install in a container a virtual environment and how is your answer connected to my question?

Hello @foersben ,

You should use :
CMD [“python3”, “”, “runserver”, “0:8000”]
CMD [“python3”, “”, “runserver”, “”]

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