A Sample Project Dockerization with Docker Compose
Docker Compose comes handy when you have multiple projects and multiple environments (database, queue mechanisms, etc) that needs to be started up together (as microservices).
Firstly, you should create Docker images of your projects:
After that, I created a new file named “docker-compose.yml” at the root of my multi-module project. You can choose any folder location you prefer.
The compose file format’s latest version is 3.8 so that will be the one I will be using.
Environment variables format should be “upper case format with underscore as the delimiter”.
My project includes modules for:
- A Eureka Server
- A Config Server
- An Api Gateway
- A Score Calculator Service that uses Score Segment and Score Calculator individually for variants in calculations.
I was getting “Could not locate PropertySource” error in my city-score project which uses config-server.
You should be careful as stated here;
depends_on
does not wait for config-service to be “ready” before starting discovery-service - it only waits until it is started.
There seems to be two ways; one is adding “retry” config and the other is a Docker workaround using command and wait-for-it sh.
If you choose to work with the first solution (by adding retry properties to projects that use config server), these properties are:
spring.cloud.config.fail-fast
spring.cloud.config.retry.initial-interval
spring.cloud.config.retry.multiplier
spring.cloud.config.retry.max-interval
spring.cloud.config.retry.max-attempts
You can read more details here:
You do not need to add “spring-boot-starter-aop” and “spring-retry” since “spring-boot-starter-aop” is already included via “spring-cloud-starter-sleuth” and “spring-retry” is already included via “spring-cloud-starter-bus-kafka” dependencies if you used Zipkin. So, check your already added dependencies.
Here is my finalized docker-compose.yml file:
Ready, Set, Go! It is now time to run “docker compose up” command (in the same directory as your docker-compose.yml file.
docker compose up
All the containers are green and healthy :)
Check http://localhost:8761/ :
Voilà!
You can visit my Microservices project (implemented with Spring Cloud) GitHub url for sample code:
Happy Coding!