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
I've found working solution here: no internet inside docker-compose service
After executing described commands everything start to work
Thank you for everybody!
I can’t find an evident error, but I’ll try to help, and here’s where I would start in this case:
version
andexpose
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):EXPOSE 80
to dockerfilesservice2
container fromservice1
container by container name with no ports specified. To ensure that the container is accessible by its name through DNS.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, theservice2
address won’t be accessible because the browser is not in the same network as theservice2
container. There are multiple approaches to handle this, for example environment variables.