football-player-microservice

The following is the Dockerfile for the football-player-microservice:

FROM docker.io/openjdk
ADD football-player-microservice/target/football-player-microservice-thorntail.jar /opt/service.jar
ENTRYPOINT ["java","-jar","/opt/service.jar"]
CMD [""]

Now, within the same path of the Docker file, build the Docker image, as follows:

docker build -f football-player-microservice.dockerfile -t foogaro/football-player-microservice 

Check that the image is actually on your local Docker registry, issuing the following command:

docker images

If the image that was just built is listed (it should appear at the top of the list), run the container, as follows:

docker run -it --rm=true --name="football-player-microservice" -p 8180:8180 foogaro/football-player-microservice -Dswarm.http.port=8180 -Dweb.primary.port=8180

The application should not start properly, due to an error while connecting to the Postgres database, shown as follows:

Caused by: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

We need to hook the database running on a different container, and thus potentially on a different network.

To achieve this, we need to link those two containers, football-player-microservice and postgres_thorntail, so that they can communicate with each other. The best scenario would involve them sharing the same network layer; this can be achieved in two ways with plain Docker, as follows:

  • Sharing the network provided by the host (that is, the server running Docker; your laptop, in the case)
  • Creating a dedicated network (a dedicated Docker network) for the two containers

The first option is probably the easiest, but in big environments, it will not scale. On the other hand, the second option adds a little complexity, but it provides a more scalable solution; and, if we are dealing with containers, we want to scale.

So, let's create a dedicated Docker network, as follows:

docker network create football
c59ac3a846920891ce092868945caf316e5adbb4007715f9f0c38f8d2954583e
docker network inspect football
[
{
"Name": "football",
"Id": "c59ac3a846920891ce092868945caf316e5adbb4007715f9f0c38f8d2954583e",
"Created": "2018-10-23T10:08:03.496371735+02:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]

If you have the Postgres container up and running, stop it, and then run it again using the football network, as follows:

docker run --name postgres_thorntail --net="football" --rm="true" -e POSTGRES_PASSWORD=postgresPwd -e POSTGRES_DB=football_players_registry -d -p 5532:5432 postgres

If we inspect the network again, we should see the Postgres instance belonging to it:

docker network inspect football
[
{
"Name": "football",
"Id": "c59ac3a846920891ce092868945caf316e5adbb4007715f9f0c38f8d2954583e",
"Created": "2018-10-23T10:08:03.496371735+02:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Containers": {
"7a69aa8c0d700110d0ea4a49a2ee3f590f07856de4d060469758cf09fbdebf43": {
"Name": "postgres_thorntail",
"EndpointID": "0a0d0a55d5d3141babff26a2917ae8c838af4518cc842ac6da2afd1f56d8e4b7",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]

Now, we need to link our microservice with the database that it uses as a backing service, as follows:

docker run -it --rm=true --net="football" --link="postgres_thorntail" --name="football-player-microservice" -p 8180:8180 foogaro/football-player-microservice -Dweb.primary.port=8180 -Dswarm.http.port=8180

If we inspect the network again, we should also see the football-player-microservice belonging to it, as follows:

Docker network inspect football
[
{
"Name": "football",
"Id": "c59ac3a846920891ce092868945caf316e5adbb4007715f9f0c38f8d2954583e",
"Created": "2018-10-23T10:08:03.496371735+02:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Containers": {
"7a69aa8c0d700110d0ea4a49a2ee3f590f07856de4d060469758cf09fbdebf43": {
"Name": "postgres_thorntail",
"EndpointID": "0a0d0a55d5d3141babff26a2917ae8c838af4518cc842ac6da2afd1f56d8e4b7",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
},
"962c79c46821204345ab86a3cb40a5691140ccf100112904e678c044a68e5138": {
"Name": "football-player-microservice",
"EndpointID": "0d1232f4c3cfc3f82ddefa2bd4c0ceaf994aa6b87292341f761009c56ccbe80f",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]

Now, we are sure that the containers can communicate with each other, but the football-player-microservice application needs to bind to the database at a specific IP and port.

This can be achieved by passing those parameters to the container as environment variables, as follows:

docker run -it --rm=true --net="football" --link="postgres_thorntail" --name="football-player-microservice" -e FOOTBALL_POSTGRES_IP=172.18.0.2 -e FOOTBALL_POSTGRES_PORT=5432 -p 8180:8180 foogaro/football-player-microservice -Dweb.primary.port=8180 -Dswarm.http.port=8180

These environment variables must be picked up by the application; so, the code needs to change a little.

Open the source code of the football-player-microservice application, and edit the YAML file project-defaults.yml; replace the connection-url with the following definition:

docker run -it --rm=true --net="football" --link="postgres_thorntail" --name="football-player-microservice" -e FOOTBALL_POSTGRES_IP=172.18.0.2 -e FOOTBALL_POSTGRES_PORT=5432 -p 8180:8180 foogaro/football-player-microservice -Dweb.primary.port=8180 -Dswarm.http.port=8180

Now, compile the application again, as described in Chapter 5, Eclipse MicroProfile and Transactions – Narayana LRA, and rebuild the Docker image; then, run the container, as follows:

docker run -it --rm=true --net="football" --link="postgres_thorntail" --name="football-player-microservice" -e FOOTBALL_POSTGRES_IP=172.18.0.2 -e FOOTBALL_POSTGRES_PORT=5432 -p 8180:8180 foogaro/football-player-microservice -Dweb.primary.port=8180 -Dswarm.http.port=8180

Now, let's containerize the football-player-ui frontend application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset