[JavaEE_PreBuilt_WAR] explained how to use an in-memory database with the application server. This gets you started rather quickly but becomes a bottleneck soon as the database is only in-memory. This means that any changes made to your schema and data are lost when the application server shuts down. In this case, you need to use a database server that resides outside the application server. For example, PostgreSQL as the database server and WildFly as the application server.
This section will show how Docker Networks can be used to connect to a service running inside a Docker container via docker network.
-
Create a docker network as:
$ docker network create mynet
-
Start PostgreSQL server as:
$ docker run --name db -d -p 5432:5432 --net mynet -e POSTGRES_USER=ticketmonster -e POSTGRES_PASSWORD=ticketmonster-docker postgres
-e
define environment variables that are read by the database at startup and allow us to access the database with this user and password.
--net
defines the network which the container will use.
-
Start WildFly with the deployed Java EE application as:
$ docker run -d --name wildfly-ticketmonster --net mynet -p 8080:8080 rafabene/wildfly-ticketmonster
--net
is used again to define the network which the container will use. In this case this container will use the same network of PostgreSQL.
Note
|
Docker network
Docker 1.9 introduced the concept of multi-host networks. With the introduction of docker networks, docker linking is expected to be deprecated and removed in a future release. Docker networks allows you to create virtual networks and attach container to them so you can create the network topology that is right for your application. In our case, the WildFly container can see information about PostgreSQL . Once they are on the same network, the See more about docker networks on the Docker Networks website. |
See the application in action at http://<external-ip>:8080/ticket-monster/. The output is shown:
Note
|
It might take a while for the container to come up. Wait for the server to complete the startup. |
Note
|
If you get a 404 or any other error, wait for a little longer until the containers are started up. |
Lets stop the previously running container as:
docker stop db wildfly-ticketmonster
docker rm db wildfly-ticketmonster