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

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!

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”, “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”]