Reputation: 497
I used SpringBoot RestApi Microservices and MongoDB. In MongoDB, I have three Databases such as player-db, game-db and score-db. My services are in the different folders and for each one I defined Dockerfile. Dockerfile for player service:
FROM openjdk:8
COPY ./target/demo-0.0.1-SNAPSHOT.jar player.jar
EXPOSE 8080
ENTRYPOINT ["java","-Dspring.data.mongodb.uri=mongodb://db:27017/","-jar","-Djava.rmi.server.hostname=0.0.0.0", "player.jar"]
Dockerfile for game service:
FROM openjdk:8
COPY ./target/demo-0.0.1-SNAPSHOT.jar game.jar
EXPOSE 8080
ENTRYPOINT ["java","-Dspring.data.mongodb.uri=mongodb://db1:27017/","-jar","-Djava.rmi.server.hostname=0.0.0.0", "game.jar"]
and Dockerfile for score service:
FROM openjdk:8
COPY ./target/demo-0.0.1-SNAPSHOT.jar score.jar
EXPOSE 8080
ENTRYPOINT ["java","-Dspring.data.mongodb.uri=mongodb://db2:27017/","-jar","-Djava.rmi.server.hostname=0.0.0.0", "score.jar"]
And I defined a docker-compose.yml file:
version: "3"
services:
player-docker:
build:
context: ./
dockerfile: ./src/main/java/spring/multiple/mongo/project/player/DockerFile
restart: always
ports:
- 8080:8080
depends_on:
- db
game-docker:
build:
context: ./
dockerfile: ./src/main/java/spring/multiple/mongo/project/game/DockerFile
restart: always
ports:
- 8080:8080
depends_on:
- db1
score-docker:
build:
context: ./
dockerfile: ./src/main/java/spring/multiple/mongo/project/score/Dockerfile
restart: always
ports:
- 8080:8080
depends_on:
- db2
db:
image: mongo
volumes:
- mongodata:/data/db
ports:
- 27017:27017
restart: always
environment:
MONGO_INITDB_ROOT_DATABASE: player-db
db1:
image: mongo
volumes:
- mongodata:/data/db1
ports:
- 27017:27017
restart: always
environment:
MONGO_INITDB_ROOT_DATABASE: game-db
db2:
image: mongo
volumes:
- mongodata:/data/db2
ports:
- 27017:27017
restart: always
environment:
MONGO_INITDB_ROOT_DATABASE: score-db
volumes:
mongodata:
In fact, in docker-compose file I tried to define different databases for my services, but when I execute docker-compose up I get an error
The ERROR:
$ docker-compose up
Starting springmultiplemongoproject_db_1 ...
Starting springmultiplemongoproject_db2_1 ... error
Creating springmultiplemongoproject_db1_1 ...
ERROR: for springmultiplemongoproject_db2_1 Cannot start service db2: driver fa
iled programming external connectivity on endpoint springmultiplemongoproject_db
2_1 (736a5c8f4a485472d7d5c622f29fd892b533794b352cbccc97dae5c54e3ae54f): Bind for
Creating springmultiplemongoproject_db1_1 ... error
ERROR: for springmultiplemongoproject_db1_1 Cannot start service db1: driver fa
Starting springmultiplemongoproject_db_1 ... done
1_1 (e0ea7a6e31f0bec010ccfef67705732904d3fcf0eee55cee8577d464583070ff): Bind for
0.0.0.0:27017 failed: port is already allocated
Creating springmultiplemongoproject_player-docker_1 ... done
ERROR: for db2 Cannot start service db2: driver failed programming external con
nectivity on endpoint springmultiplemongoproject_db2_1 (736a5c8f4a485472d7d5c622
f29fd892b533794b352cbccc97dae5c54e3ae54f): Bind for 0.0.0.0:27017 failed: port i
s already allocated
ERROR: for db1 Cannot start service db1: driver failed programming external con
nectivity on endpoint springmultiplemongoproject_db1_1 (e0ea7a6e31f0bec010ccfef6
7705732904d3fcf0eee55cee8577d464583070ff): Bind for 0.0.0.0:27017 failed: port i
s already allocated
ERROR: Encountered errors while bringing up the project.
I am beginner in Docker, and I read many documents, but I could not find any solution.
Upvotes: 1
Views: 3638
Reputation: 7759
This image might help you to understand why we have to write a different port for the host machine.
The container is an OS in itself. But the container is connecting to the outside OS which is hosting multiple containers. So to help host machine to identify which container you are talking to, we need to use a different port number
Upvotes: 3
Reputation: 5245
You are binding the same host ports to different docker services. You have to specify different host ports for each service. The syntax for specifying ports is as follows:
ports:
- "HOST:CONTAINER"
So for the HOST
part you need different port numbers
version: "3"
services:
player-docker:
...
ports:
- 8081:8080
game-docker:
...
ports:
- 8082:8080
score-docker:
...
ports:
- 8083:8080
db:
...
ports:
- 27018:27017
db1:
...
ports:
- 27019:27017
db2:
...
ports:
- 27019:27017
...
Or if you want docker compose to assign host port numbers for you, you can omit the HOST part, like this
version: "3"
services:
player-docker:
...
ports:
- 8080
game-docker:
...
ports:
- 8080
score-docker:
...
ports:
- 8080
db:
...
ports:
- 27017
db1:
...
ports:
- 27017
db2:
...
ports:
- 27017
...
Or if you don't want to bind ports to the host for the database services, you can just omit the ports
part
Upvotes: 2