I have 3 services
zuul, eureka, and a rest service.
Running the code locally via springboot works and I can use the url
http://localhost:8080/account/bank/account/
But when I dockerise it
The eurka application.yml
server:
port: '8761'
spring:
application:
name: eureka-service
eureka:
client:
fetchRegistry: 'false'
registerWithEureka: 'false'
Eureka local.dockerfile
FROM adoptopenjdk:16.0.1_9-jdk-hotspot-focal
EXPOSE 8761
RUN mkdir /app
COPY target/eureka-service.jar /app
WORKDIR /app
ENTRYPOINT ["java", "-jar", "/app/eureka-service.jar"]
The zuul application.yml
spring:
application:
name: zuul-service
server:
port: '8080'
eureka:
client:
instance:
preferIpAddress: 'true'
fetchRegistry: 'true'
registerWithEureka: 'true'
serviceUrl:
defaultZone: http://172.21.0.2:8761/eureka
zuul:
routes:
account:
path: /account/**
serviceId: account-service
zuul local.dockerfile
FROM adoptopenjdk:16.0.1_9-jdk-hotspot-focal
EXPOSE 8080
RUN mkdir /app
COPY target/zuul-service.war /app
WORKDIR /app
ENTRYPOINT ["java", "-jar", "/app/zuul-service.war"]
the rest application.yml
spring:
application:
name: account-service
server:
port: '8082'
eureka:
client:
instance:
preferIpAddress: 'true'
fetchRegistry: 'true'
registerWithEureka: 'true'
serviceUrl:
defaultZone: http://172.21.0.2:8761/eureka
rest local.dockerfile
FROM adoptopenjdk:16.0.1_9-jdk-hotspot-focal
EXPOSE 8082
RUN mkdir /app
COPY target/account-service.war /app
WORKDIR /app
ENTRYPOINT ["java", "-jar", "/app/account-service.war"]
I have two docker-compose files. Mainly through experimenting with trying to publish the ports.
Eureka and rest
version: "3"
services:
eureka-service:
image: eureka-service
container_name: eureka-service
build:
context: ../../eureka-service
dockerfile: local.dockerfile
ports:
- "8761:8761"
networks:
- banking-network
account-service:
image: account-service
container_name: account-service
build:
context: ../../account-service
dockerfile: local.dockerfile
ports:
- "8082:8082"
depends_on:
- eureka-service
environment:
- eureka.client.service-url.defaultZone=http://172.21.0.2:8761/eureka
networks:
- banking-network
networks:
banking-network:
external:
name: banking-network
zuul
version: "3"
services:
zuul-service:
image: zuul-service
container_name: zuul-service
build:
context: ../../zuul-service
dockerfile: local.dockerfile
ports:
- "8080:8080"
environment:
- eureka.client.service-url.defaultZone=http://172.21.0.2:8761/eureka
networks:
- banking-network
networks:
banking-network:
external:
name: banking-network
when I run docker-compose up on both docker-compose
zuul and application is registered with eureka
But in postman when I run http://localhost:8080/account/bank/account/
it fails.
If I am correct I need to publish zuul-service port to the host.
But this is where I am having real problems
So following the docker docs
I run the following on the zuul docker-compose
docker-compose run --publish 8080:8080 zuul-service
running docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ea2957b56646 zuul-service "java -jar /app/zuul…" About a minute ago Up About a minute 0.0.0.0:8080->8080/tcp gateway_zuul-service_run_c3988736de38
Zuul and account are registered
But neither URLs work
http://localhost:8080/account/bank/account/
http://0.0.0.0:8080/account/bank/account/
I created a simple health page on zuul. to see if I can connect to zuul
This url works, when its run from springboot
http://localhost:8080/gateway/health/
But neither works from docker
http://localhost:8080/gateway/health/
http://0.0.0.0:8080/gateway/health/
I thought publishing 8080 to my local host, would mean I could use localhost
2
Answers
The code does work. I found out that because I was developing my code in increments. I thought I was rebuilding a new image. I was not, I removed the images, rebuilt and the code works.
A few suggestions to debug the issue:
Put all services in one docker compose file and make sure they are on the same network
For defaultZone, change ip to eureka docker image name so instead of:
defaultZone: http://172.21.0.2:8761/eureka
Try:
I’d try without
preferIpAddress: true
If you don’t have multiple replicas of your registered servicesAfter Exposing zuul gateway port, check what routes are available at
localhost:8080/routes