skip to Main Content

I am new to docker compose and trying to find the reason of error
I have ubuntu 20, Docker version 24.0.7, Docker Compose version v2.21.0
I reduced code to minimal possible:
docker-compose.jml:

version: '3'
services:
  service1:
    build: ./service1
    ports:
      - "3000:80"

  service2:
    build: ./service2
    ports:
      - "3001:80"

service2 app:

const express = require('express');
const app = express();
const port = 80;

app.get('/', (req, res) => {
  res.send('Hello from Service2!');
});

app.listen(port, () => {
  console.log(`Service2 listening at http://localhost:${port}`);
});

service1 app:

const axios = require('axios');
const express = require('express');
const app = express();
const port = 80;

app.get('/', async (req, res) => {
  try {
    // Make an HTTP request to service2
    const response = await axios.get('http://service2:80');
    res.send(`Response from Service1: ${response.data}`);
  } catch (error) {
    console.error('Error making request to Service2:', error.message);
    res.status(500).send('Internal Server Error');
  }
});

app.listen(port, () => {
  console.log(`Service1 listening at http://localhost:${port}`);
});

Dockerfile for both services:

FROM node:20-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

CMD ["node", "app.js"]

I can access service2 by http://localhost:3001
Request to service1 using http://localhost:3000 hungs for about 3 minutes and then an error is thrown by service1: Error making request to Service2: connect ETIMEDOUT 172.23.0.3:80

I tried to send request from service1 to service2 using service name and service port.
I expected successful request but it can not be sent

2

Answers


  1. Chosen as BEST ANSWER

    I've found working solution here: no internet inside docker-compose service

    pkill docker
    iptables -t nat -F
    ifconfig docker0 down
    brctl delbr docker0
    docker -d
    

    After executing described commands everything start to work

    Thank you for everybody!


  2. I can’t find an evident error, but I’ll try to help, and here’s where I would start in this case:

    1. Despite the fact that version and expose are deprecated and likely ignored, and both containers are in the same network by default, I would add details to the compose file to make it as comprehensive as possible (trimming down once everything is working):
    version: "3.9"
    services:
      service2:
        build: ./service2
        expose:
          - "80"
        ports:
          - "3001:80"
        networks:
          - services-network
    
      service1:
        build: ./service1
        depends_on:
          - service2
        expose:
          - "80"
        ports:
          - "3000:80"
        networks:
          - services-network
    
    networks:
      services-network:
    
    1. Add EXPOSE 80 to dockerfiles
    2. Try to ping service2 container from service1 container by container name with no ports specified. To ensure that the container is accessible by its name through DNS.
    3. If container is not accessible by its name, try to replace container name with IP (in service code and for ping). To get a container IP use this: docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' CONTAINER_ID_OR_NAME. Or you can configure your network submask to get static IP.

    I’m not familiar with Node.js, but if the .get('http://service2:80') code is loaded into the localhost browser and executed there, the service2 address won’t be accessible because the browser is not in the same network as the service2 container. There are multiple approaches to handle this, for example environment variables.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search