You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Deploy Java EE 7 Application (Container Linking)
3
+
4
+
<<JavaEE7_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 after the application server shuts down. In this case, you need to use a database server that resides outside the application server. For example, MySQL as the database server and WildFly as the application server.
This section will show how https://docs.docker.com/userguide/dockerlinks/[Docker Container Linking] can be used to connect to a service running inside a Docker container via a network port.
10
+
11
+
. Start MySQL server as:
12
+
+
13
+
[source, text]
14
+
----
15
+
docker run --name mysqldb -e MYSQL_USER=mysql -e MYSQL_PASSWORD=mysql -e MYSQL_DATABASE=sample -e MYSQL_ROOT_PASSWORD=supersecret -p 3306:3306 -d mysql
16
+
----
17
+
+
18
+
`-e` define environment variables that are read by the database at startup and allow us to access the database with this user and password.
19
+
+
20
+
. Start WildFly and deploy Java EE 7 application as:
21
+
+
22
+
[source, text]
23
+
----
24
+
docker run -it --name mywildfly --link mysqldb:db -p 8080:8080 arungupta/wildfly-mysql-javaee7
25
+
----
26
+
+
27
+
`--link` takes two parameters - first is name of the container we're linking to and second is the alias for the link name.
28
+
+
29
+
.Container Linking
30
+
[NOTE]
31
+
===============================
32
+
Creating a link between two containers creates a conduit between a source container and a target container and securely transfer information about source container to target container.
33
+
34
+
In our case, target container (WildFly) can see information about source container (MySQL). When containers are linked, information about a source container can be sent to a recipient container. This allows the recipient to see selected data describing aspects of the source container. For example, IP address of MySQL server is expoed at $DB_PORT_3306_TCP_ADDR and port of MySQL server is exposed at $DB_PORT_3306_TCP_PORT. These are then used to create the JDBC resource.
35
+
36
+
See more about container communication on the Docker website link:https://docs.docker.com/userguide/dockerlinks/[Linking Containers Together]
37
+
===============================
38
+
+
39
+
. See the output as:
40
+
+
41
+
[source, text]
42
+
----
43
+
> curl http://$(docker-machine ip lab):8080/employees/resources/employees
Copy file name to clipboardExpand all lines: chapters/docker-javaee7.adoc
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,4 @@
1
+
[[JavaEE7_PreBuilt_WAR]]
1
2
## Deploy Java EE 7 Application (Pre-Built WAR)
2
3
3
4
https://github.com/javaee-samples/javaee7-hol[Java EE 7 Hands-on Lab] has been delivered all around the world and is a pretty standard application that shows design patterns and anti-patterns for a typical Java EE 7 application.
@@ -23,6 +24,11 @@ docker run -it -p 8080:8080 dockerlab:5000/javaee7-hol
23
24
24
25
See the application in action at http://dockerhost:8080/movieplex7/.
25
26
27
+
This uses an in-memory database with WildFly application server as shown in the image:
The option "--swarm" configures the machine with Swarm, "--swarm-master" configures the created machine to be Swarm master. Make sure to replace cluster id after token:// with that obtained in the previous step. Swarm master creation talks to the hosted service on Docker Hub and informs that a master is created in the cluster.
38
+
Replace `<TOKEN>` with the cluster id obtained in the previous step.
39
+
+
40
+
`--swarm` configures the machine with Swarm, `--swarm-master` configures the created machine to be Swarm master. Swarm master creation talks to the hosted service on Docker Hub and informs that a master is created in the cluster.
39
41
+
40
42
. Connect to this newly created master and find some more information about it:
NOTE: If you're on Windows, use the "docker-machine env swarm-master" command only and copy the output into an editor to replace all appearances of EXPORT with SET and issue the three commands at your command prompt, remove the quotes and all duplicate appearences of "/".
50
+
NOTE: If you're on Windows, use the `docker-machine env swarm-master` command only and copy the output into an editor to replace all appearances of EXPORT with SET and issue the three commands at your command prompt, remove the quotes and all duplicate appearences of "/".
51
+
+
52
+
This will show the output as:
53
+
+
54
+
[source, text]
55
+
----
56
+
> docker info
57
+
Containers: 2
58
+
Images: 7
59
+
Storage Driver: aufs
60
+
Root Dir: /mnt/sda1/var/lib/docker/aufs
61
+
Backing Filesystem: extfs
62
+
Dirs: 11
63
+
Dirperm1 Supported: true
64
+
Execution Driver: native-0.2
65
+
Kernel Version: 4.0.3-boot2docker
66
+
Operating System: Boot2Docker 1.6.2 (TCL 5.4); master : 4534e65 - Wed May 13 21:24:28 UTC 2015
Node creation talks to the hosted service at Docker Hub and joins the previously created cluster. This is specified by --swarm-discovery token://... and specifying the cluster id obtained earlier.
93
+
Node creation talks to the hosted service at Docker Hub and joins the previously created cluster. This is specified by `--swarm-discovery token://...` and specifying the cluster id obtained earlier.
58
94
+
59
95
. To make it a real cluster, let's create a second node:
The machines that are part of the cluster have the cluster’s name in the SWARM column, blank otherwise. For example, ``mymachine'' is a standalone machine where as all other machines are part of swarm-master cluster. The Swarm master is also identified by (master) in the SWARM column.
120
+
The machines that are part of the cluster have the cluster’s name in the SWARM column, blank otherwise. For example, ``la'' is a standalone machine where as all other machines are part of the ``swarm-master'' cluster. The Swarm master is also identified by (master) in the SWARM column.
81
121
+
82
122
. Connect to the Swarm cluster and find some information about it:
There are 3 nodes – one Swarm master and 2 Swarm nodes. There is a total of 4 containers running in this cluster – one Swarm agent on master and each node, and there is an additional swarm-agent-master running on the master. This can be verified by connecting to the master and listing all the containers:
98
154
+
155
+
. List nodes in the cluster with the following command:
156
+
+
99
157
[source, text]
100
158
----
101
-
eval "$(docker-machine env swarm-master)"
102
-
docker info
159
+
docker run swarm list token://<TOKEN>
103
160
----
104
161
+
105
-
. List nodes in the cluster with the following command:
162
+
This shows the output as:
106
163
+
107
164
[source, text]
108
165
----
109
-
docker run swarm list token://<TOKEN>
166
+
> docker run swarm list token://b9d9da9198c0facbeeae302242fb65a5
167
+
192.168.99.109:2376
168
+
192.168.99.108:2376
169
+
192.168.99.107:2376
110
170
----
111
-
+
112
-
The complete cluster is in place now, and we need to deploy the Ticket Monster application to it.
171
+
172
+
The complete cluster is in place now, and we need to deploy the Java EE application to it.
113
173
114
174
Swarm takes care for the distribution of the deployments across the nodes. The only thing, we need to do is to deploy the application as explained already:
0 commit comments