Skip to content

Commit a4ed698

Browse files
committed
adding a section on container linking, and some images
1 parent 4093c6c commit a4ed698

7 files changed

Lines changed: 126 additions & 13 deletions
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
[[JavaEE7_Container_Linking]]
2+
## 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.
5+
6+
.Two Containers On Same Docker Host
7+
image::../images/javaee7-hol-container-linking.png[]
8+
9+
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
44+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><collection><employee><id>1</id><name>Penny</name></employee><employee><id>2</id><name>Sheldon</name></employee><employee><id>3</id><name>Amy</name></employee><employee><id>4</id><name>Leonard</name></employee><employee><id>5</id><name>Bernadette</name></employee><employee><id>6</id><name>Raj</name></employee><employee><id>7</id><name>Howard</name></employee><employee><id>8</id><name>Priya</name></employee></collection>
45+
----

chapters/docker-javaee7.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
[[JavaEE7_PreBuilt_WAR]]
12
## Deploy Java EE 7 Application (Pre-Built WAR)
23

34
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
2324

2425
See the application in action at http://dockerhost:8080/movieplex7/.
2526

27+
This uses an in-memory database with WildFly application server as shown in the image:
28+
29+
.In-memory Database
30+
image::../images/javaee7-hol-in-memory-database.png[]
31+
2632
Only two changes are required to the standard `jboss/wildfly` image:
2733

2834
. Start WildFly in full platform:

chapters/docker-swarm.adoc

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ NOTE: Make sure to note this cluster id now as there is no means to list it late
3535
docker-machine create -d virtualbox --swarm --swarm-master --swarm-discovery token://<TOKEN> swarm-master
3636
----
3737
+
38-
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.
3941
+
4042
. Connect to this newly created master and find some more information about it:
4143
+
@@ -45,7 +47,41 @@ eval "$(docker-machine env swarm-master)"
4547
docker info
4648
----
4749
+
48-
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
67+
CPUs: 1
68+
Total Memory: 998.1 MiB
69+
Name: swarm-master
70+
ID: USSA:35LS:WVRN:GKAZ:MLD4:XMQF:P7PL:VQ5D:7V4K:2QAH:5D2L:HC4K
71+
Debug mode (server): true
72+
Debug mode (client): false
73+
Fds: 24
74+
Goroutines: 37
75+
System Time: Wed Jun 10 03:40:00 UTC 2015
76+
EventsListeners: 1
77+
Init SHA1: 7f9c6798b022e64f04d2aff8c75cbf38a2779493
78+
Init Path: /usr/local/bin/docker
79+
Docker Root Dir: /mnt/sda1/var/lib/docker
80+
Username: arungupta
81+
Registry: [https://index.docker.io/v1/]
82+
Labels:
83+
provider=virtualbox
84+
----
4985
+
5086
. Create Swarm nodes.
5187
+
@@ -54,7 +90,7 @@ NOTE: If you're on Windows, use the "docker-machine env swarm-master" command on
5490
docker-machine create -d virtualbox --swarm --swarm-discovery token://<TOKEN> swarm-node-01
5591
----
5692
+
57-
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.
5894
+
5995
. To make it a real cluster, let's create a second node:
6096
+
@@ -67,17 +103,21 @@ docker-machine create -d virtualbox --swarm --swarm-discovery token://<TOKEN> sw
67103
+
68104
[source, text]
69105
----
70-
TODO: ADD CODE
106+
docker-machine ls
71107
----
72108
+
73109
This shows the output as:
74110
+
75111
[source, text]
76112
----
77-
TODO: ADD CODE
113+
NAME ACTIVE DRIVER STATE URL SWARM
114+
lab virtualbox Running tcp://192.168.99.103:2376
115+
swarm-master * virtualbox Running tcp://192.168.99.107:2376 swarm-master (master)
116+
swarm-node-01 virtualbox Running tcp://192.168.99.108:2376 swarm-master
117+
swarm-node-02 virtualbox Running tcp://192.168.99.109:2376 swarm-master
78118
----
79119
+
80-
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.
81121
+
82122
. Connect to the Swarm cluster and find some information about it:
83123
+
@@ -91,25 +131,45 @@ This shows the output as:
91131
+
92132
[source, text]
93133
----
94-
TODO: ADD CODE
134+
> docker info
135+
Containers: 4
136+
Strategy: spread
137+
Filters: affinity, health, constraint, port, dependency
138+
Nodes: 3
139+
swarm-master: 192.168.99.107:2376
140+
└ Containers: 2
141+
└ Reserved CPUs: 0 / 1
142+
└ Reserved Memory: 0 B / 1.023 GiB
143+
swarm-node-01: 192.168.99.108:2376
144+
└ Containers: 1
145+
└ Reserved CPUs: 0 / 1
146+
└ Reserved Memory: 0 B / 1.023 GiB
147+
swarm-node-02: 192.168.99.109:2376
148+
└ Containers: 1
149+
└ Reserved CPUs: 0 / 1
150+
└ Reserved Memory: 0 B / 1.023 GiB
95151
----
96152
+
97153
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:
98154
+
155+
. List nodes in the cluster with the following command:
156+
+
99157
[source, text]
100158
----
101-
eval "$(docker-machine env swarm-master)"
102-
docker info
159+
docker run swarm list token://<TOKEN>
103160
----
104161
+
105-
. List nodes in the cluster with the following command:
162+
This shows the output as:
106163
+
107164
[source, text]
108165
----
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
110170
----
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.
113173

114174
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:
115175

docker-java-lab.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ include::chapters/docker-container.adoc[]
2222

2323
include::chapters/docker-javaee7.adoc[]
2424

25+
include::chapters/docker-container-linking.adoc[]
26+
2527
include::chapters/ticket-monster.adoc[]
2628

2729
include::chapters/docker-maven.adoc[]

docker-java-lab.pdf

266 KB
Binary file not shown.
153 KB
Loading
92.3 KB
Loading

0 commit comments

Comments
 (0)