Skip to content

Latest commit

 

History

History
68 lines (43 loc) · 2.9 KB

docker-network.adoc

File metadata and controls

68 lines (43 loc) · 2.9 KB

Deploy Java EE Application (Docker Network)

[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.

javaee7 hol container linking
Figure 1. Two Containers On Same Docker Host

This section will show how Docker Networks can be used to connect to a service running inside a Docker container via docker network.

  1. Create a docker network as:

$ docker network create mynet
  1. 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.

  1. 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 /etc/hosts files of both containers were updated to include information about other containers in the same network

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.
javaee ticketmonster
Figure 2. Java EE Application Output
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