I have a microservices setup with config ser, configaration in git hub, product service, Admin service, service discovery, api gateway respectively. The server port is dynamically assigned. Thiss setup works perfectly. When I am planning to dockerise my microservices not sure how to start with. Any help will be greatly appreciated.
1 Answers
When you package it in a container, your application should listen on a fixed port. Normally a Spring Boot HTTP application listens on port 8080 without extra configuration; if you have Spring properties that randomize this, remove those.
Your Dockerfile may have an EXPOSE
line to document that the service listens on that port. This is optional, and this line has almost no useful effect beyond documentation.
Inside the Docker networking space, each service has its own IP address, and Docker provides a DNS system so that containers can talk to each other using the container names as host names, provided they're on the same manually-created docker run --network
. (Also see How to communicate between Docker containers via "hostname".) These connections will also used the fixed port; http://product:8080
.
For any of the services that need to be called from outside Docker (including from a Web browser; in your setup likely only the API gateway) you need to publish ports to make them accessible. In a docker run -p
option, the second port number again is the fixed port 8080. The first port may be any unused port number on the host. It is possible (but unusual in practice) to only specify the container port number docker run -p 8080
to let Docker pick the host port, and again you will get something random.
If you are running this under Docker Compose, it manages the networking layer for you, and you do not generally need any manual network setup at all. Compose ports:
is equivalent to the docker run -p
option. You do not need to specify networks:
, container_name:
, links:
, or expose:
.

- 130,717
- 29
- 175
- 215
-
many thanks for the answer. When you say the containers can talk to each other using the container names as host names then we dont need eureka server ? Little confused... still novice in this part – rolling stone Oct 28 '22 at 12:11