I have this docker compose file shown file
version: '3'
services:
backend-flask:
build: ./backend
frontend-fligh:
build: ./frontend-fligh
ports:
- "3000:3000"
depends_on:
- backend-flask
In my frontend I’m trying to make a backend api call by doing
const response = await fetch('http://backend-flask:5000/search', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(query)
});
but this requests returns me this error
POST http://backend-flask:5000/search net::ERR_NAME_NOT_RESOLVED
in my app.py of my backend flask application the route is defined as well
app = Flask(__name__)
CORS(app) #for production change to have only my site
@app.route('/search', methods=['POST'])
//more code
also note that this works if i change it from backend-flask to localhost:5000/search
i inspected the network and the containers are all in the same network, I’m really lost here, and not sure why it’s not working
jasonwang@Jasons-MacBook-Pro startUp % docker network inspect startup_default
[
{
"Name": "startup_default",
"Id": "f9507df9f26968c4665da09bf90b24904267b177476c3654197d4b9afbf1dd78",
"Created": "2023-08-25T02:26:45.523880875Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.19.0.0/16",
"Gateway": "172.19.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"1278a785f11c2227638d12aba3c5286a29346134801b43481fcedc5d15037b12": {
"Name": "startup-redis-1",
"EndpointID": "cfaf2be033ce04fd9f7cdd5cba4d287d0d3a9ee77edd79324fe8b6dfd2a93ca6",
"MacAddress": "02:42:ac:13:00:02",
"IPv4Address": "172.19.0.2/16",
"IPv6Address": ""
},
"5ca87c376d0342d67e07a2452f2ae188af710c65753894fdddda99bb05e0e902": {
"Name": "startup-backend-flask-1",
"EndpointID": "24bd57249b98e595b54846d5581655798730b3eb475624fe057599ad1f492b5c",
"MacAddress": "02:42:ac:13:00:03",
"IPv4Address": "172.19.0.3/16",
"IPv6Address": ""
},
"6a968c8e2bad884cc56540cc43699dc51c479486693e60209add3e890e84d94c": {
"Name": "startup-frontend-fligh-1",
"EndpointID": "36598650abb0c25cde0da3442f587771a947f1c8b119f5455834b0d699fa82e1",
"MacAddress": "02:42:ac:13:00:04",
"IPv4Address": "172.19.0.4/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "default",
"com.docker.compose.project": "startup",
"com.docker.compose.version": "2.20.2"
}
}
]
docker ps shown below
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fa9c94681ab1 startup-frontend-fligh "docker-entrypoint.s…" 48 minutes ago Up 4 seconds 0.0.0.0:3000->3000/tcp startup-frontend-fligh-1
66759928be0a startup-backend-flask "flask run" 48 minutes ago Up 5 seconds startup-backend-flask-1
1278a785f11c redis:alpine "docker-entrypoint.s…" About an hour ago Up 5 seconds 6379/tcp startup-redis-1
2
Answers
According to the output of
docker compose ps
command, your backend is listening on port5000
which you have forwarded on port8000
to the host. So you have to use eitherhttp://backend-flask:5000
orhttp://localhost:8000
.It is not a real solution but could you try with this docker-compose to check if it is just the DNS problem?
And change this too: