Failure to establish connection between two Docker containers

Asked

Viewed 132 times

0

I have an application that is divided into 2 parts: Frontend and Backend. Frontend is an React JS application and backend is a Java application. This project is running on Docker, where I have 3 containers: frontend, backend and db (database). My problem is that I can’t get the frontend to send a request to the backend container. Below are the Docker configuration files:

Docker-Compose:

version: "3"

services:
  db:
    image: postgres:9.6
    container_name: db
    ports:
      - "5433:5432"
    environment:
      - POSTGRES_PASSWORD=123
      - POSTGRES_USER=postgres
      - POSTGRES_DB=test

  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: backend
    ports:
      - "8085:8085"
    depends_on:
      - db

  frontend:
    container_name: frontend
    build:
      context: ./frontend
      dockerfile: Dockerfile
    expose:
      - "80"
    ports:
      - "80:80"
    links:
      - backend
    depends_on:
      - backend

Dockerfile frontend:

# Stage 0, "build-stage", based on Node.js, to build and compile the frontend
FROM node:8.12.0 as build-stage

WORKDIR /app

COPY package*.json /app/

RUN yarn

COPY ./ /app/

RUN yarn run build

# Stage 1, based on Nginx, to have only the compiled app, ready for production with Nginx
FROM nginx

RUN  rm -rf /usr/share/nginx/html/*

COPY --from=build-stage  /app/build/  /usr/share/nginx/html

# Copy the default nginx.conf provided by tiangolo/node-frontend
COPY --from=build-stage /app/nginx.conf /etc/nginx/conf.d/default.conf

Dockerfile backend:

FROM openjdk:8
ADD /build/libs/reurb-sj-13-11-19.jar reurb-sj-13-11-19.jar
EXPOSE 8085
ENTRYPOINT ["java", "-jar", "reurb-sj-13-11-19.jar", "--app.db.host=db"]

In the Frontend I already tried to send request to:

  • localhost:8085
  • 172.18.0.3:8085
  • 172.18.0.3
  • 0.0.0.0:8085

When I try to send the Frontend request, it is "active" for about 10 seconds and then returns with error. The strange thing is that the request does not return any status.

NOTE: I already scanned the internet and everyone talks to put EXPOSE, PORTS and LINKS (there on Docker-Compose), but still nothing works.

  • it is active for about 10 seconds and then returns with error. What is the error returned? You checked whether your back-end application is actually serving requests on the exposed port?

  • Oops, the error is an "err_connection_reset". And yes, the backend is listening to the requests on port 8085. Remembering that this error I can only see on the console, because in the Chrome Network tab, the request there does not illustrate any status of the action.

1 answer

1

When you go up containers on Docker, they work as isolated servers, so it won’t be possible even for you to use localhost or any other variation from one container to another. Imagine that you are on another server, localhost will point to the machine itself that you are running, not to the other server.

But when you use Docker Compose, you create a bridge-like network for your application that is able to resolve the network connection via the name in the service on your Docker-Compose.yml.

Then, for the frontend to access your backend, use the address backend:8085.

Note: Remove settings like container_name, exposes and links from your Docker-Compose.yml. Some of them are supermoons and even discontinued (in the case of links). Only use if you really need to:

version: "3"

services:
  db:
    image: postgres:9.6
    container_name: db
    ports:
      - "5433:5432"
    environment:
      - POSTGRES_PASSWORD=123
      - POSTGRES_USER=postgres
      - POSTGRES_DB=test

  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    ports:
      - "8085:8085"
    depends_on:
      - db

  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    ports:
      - "80:80"
    depends_on:
      - backend

Browser other questions tagged

You are not signed in. Login or sign up in order to post.