0

I have an application that is divided in 2 parts: Frontend and Backend. My Frontend is a React JS application and my backend is a Java Spring boot application. This project is running in Docker, and there's 3 containers: frontend, backend and db (database). My problem is that I can't make my front and send any request to my backend container. Below is my 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= 

Is Frontend I've tried to send requests to these Ip's:

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

When I try to send a request from Frontend, it "starts" and waits for about 10 seconds, then it returns with an error. The weird part is that my request doesn't return with any status.

PS.: I've read all internet and everyone said to put EXPOSE, PORTS and the LINKS (inside docker-compose), I've tried but still doesn't work.

2 Answers 2

1

You need to connect to backend:8085.

--

You shouldn't be using IP's to connect to your services but rather the service name listed in your docker-compose file.

Note: If using localhost, that refers to frontend container itself. Usually 0.0.0.0 is used to bind to all IP's or represent any IP address rather than connecting to a specific IP.

So in your front-end code, you need to use backend as the hostname (E.g., backend:8085).

It looks like you have already linked your services so networking shouldn't be an issue. My advice is to always test within the container using something such as:

docker-compose exec frontend bash # You may need to install packages ping backend telnet backend 8085 
Sign up to request clarification or add additional context in comments.

4 Comments

1 - I've already tried to use the name of the container host(sorry, I forgot to add that in my question), but it didn't work. It tries to send the request but after about 10 seconds I get an error that I've showed in my question. 2 - If I could ping from my frontend container to my backend container, that means the connection between them is working? So my requests will work?
1. Using the service name is the correct way to communicate between services. You must have another issue. 2. Ping will tell you that you are hitting the right host. telnet will say whether you can connect to that host on port 8085. I don't think you actually showed the error by the way and your backend entrypoint looks cut off. You could also go into your backend container and ensure it is listening properly. Can you connect to localhost:8085 in your browser.
@leeman24 I'd also add that db is the correct connection for the database. If that is not setup properly (say, connecting to 127.0.0.0.1 instead of db), the backend application will not work, possibly not even start
@ChatterOne, yes good point. Understanding what hostnames to use is important. Once Davi fixes all of that, i'd expect his apps to work.
1

I think it is worth mentioning that link is legacy and eventually will be removed. Source: https://docs.docker.com/network/links/

Unless you really need it, you should create custom network for your app. Good documentation is here: https://docs.docker.com/compose/compose-file/#networks

And example:

version: "3" services: db: image: postgres:9.6 container_name: db ports: - "5433:5432" environment: - POSTGRES_PASSWORD=123 - POSTGRES_USER=postgres - POSTGRES_DB=test networks: - new backend: build: context: ./backend dockerfile: Dockerfile container_name: backend ports: - "8085:8085" depends_on: - db networks: - new frontend: container_name: frontend build: context: ./frontend dockerfile: Dockerfile expose: - "80" ports: - "80:80" networks: - new depends_on: - backend networks: new: 

3 Comments

Yes, I was going to suggest that he use network instead but this still won't fix his immediate problem.
Thanks for the response, my immediate problem is fixed now, so I'll test your response latter, as I'm having problem with the communication of between containers, I've changed the way of them communication, I'm sending the requests out of the docker and and it goes inside the container I wan't, is a longer way but it's working for now.
OK so basically this fixed the weird 10s pause? Maybe the 10s pause was docker waiting for things to bootstrap, then it failed more quickly after that? :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.