I have a stack of n services defined in a docker compose file:
version: "3.7"
services:
db:
...
backend:
image: $IMAGE_DEV_BACKEND
container_name: "backend"
hostname: backend
ports:
- "8888:8080"
depends_on:
- db
networks:
- backend
frontend:
image: $IMAGE_DEV_FRONTEND
container_name: "frontend"
hostname: frontend
depends_on:
- backend
networks:
- backend
- traefik_default
volumes:
data:
networks:
backend:
driver: bridge
external:
name: backend
traefik_default:
driver: bridge
external:
name: traefik_default
All container are in the same network.
In the frontend spring app I have a following spring configuration:
backend.url=http://backend:8888
when the frontend application tries to connect to the backend one I get
ERROR DefaultFormCommandBean:529 - Error while performing action "datastoreAction": I/O error on POST request for "http://backend:8888/oauth/token": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://backend:8888/oauth/token": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:674)
when I use the IP address of the host with the docker daemon, everything works flawlessly, the containers can talk to each other.
What can I do to make it work with the container names?
Try using backend.url=http://backend:8080
. Port 8888 is used for accessing backend from your host machine, while 8080 is actual container's port. Check more information here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.