Docker Multi-Host networking allows you to create virtual networks and attach containers to them so you can create the network topology that is right for your application. This blog will show how to use it with Docker Compose.
CRUD Java Application with Couchbase, Java EE, and WildFly explained how to use a Java EE application to provide a CRUD/REST interface on a data bucket in Couchbase. It required to manually download and run WildFly. The blog also used Couchbase server using Docker and required manual configuration to load travel-sample
bucket.
Configure Couchbase Docker Container using REST API explained how to use Couchbase REST API to configure Couchbase Server.
This blog will remove the explicit download of WildFly and manual configuration of Couchbase server:
- Use Docker Compose to start WildFly and Couchbase (no download required)
- Use a Maven profile to configure Couchbase server (no manual configuration required)
- Uses Docker multi-host networking so that WildFly and Couchbase server can talk to each other
Lets get started!
Start Couchbase and WildFly using Docker Multi-Host Networking and Compose
- Start WildFly and Couchbase server using
docker-compose.yml
file from github.com/arun-gupta/docker-images/blob/master/wildfly-couchbase-javaee7/docker-compose.yml:
1234567891011121314151617mycouchbase:container_name: "db"image: couchbase/serverports:- 8091:8091- 8092:8092- 8093:8093- 11210:11210mywildfly:image: arungupta/wildfly-adminenvironment:- COUCHBASE_URI=dbports:- 8080:8080- 9990:9990arungupta/wildfly-admin
image is used as it binds WildFly’s management to all network interfaces, and in addition also exposes port 9990. This enables WildFly Maven Plugin to be used to deploy the application.container_name
is specified for Couchbase service and referred in WildFly service usingCOUCHBASE_URI
. This is then used to connect to Couchbase from the Java EE application.The application environment is started as:
123456docker-compose --x-networking up -dCreating network "wildflycouchbasejavaee7" with driver "None"Creating wildflycouchbasejavaee7_mywildfly_1Creating db--x-networking
is an experimental switch added to Docker Compose 1.9 that allows to create a bridge or an overlay network. By default, it creates a bridge network that works on a single host. The network created can be seen as:12345678docker network lsNETWORK ID NAME DRIVER968bde2b7745 bridge bridge79e937bcf1f6 none nulld06a4bd59a4b host host5e26a9a205ff wildflycouchbasejavaee7 bridgeIssue 2221 provide more explanation about the default networks created.
wildflycouchbasejavaee7
is the new bridge network created for our application. Issue #2345 provide some details about incorrect driver name in the output message.
Configure Couchbase Server
- Clone
couchbase-javaee
repo:123https://github.com/arun-gupta/couchbase-javaee - Configure Couchbase server:
1234567891011121314151617181920212223242526272829mvn install -Pcouchbase[INFO] Scanning for projects...[INFO][INFO] ------------------------------------------------------------------------[INFO] Building couchbase-javaee 1.0-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO]. . .* Server auth using Basic with user 'Administrator'> POST /sampleBuckets/install HTTP/1.1> Authorization: Basic QWRtaW5pc3RyYXRvcjpwYXNzd29yZA==> User-Agent: curl/7.37.1> Host: 192.168.99.100:8091. . .< Cache-Control: no-cache<{ [data not shown]100 19 100 2 100 17 50 425 --:--:-- --:--:-- --:--:-- 435* Connection #0 to host 192.168.99.100 left intact[][INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------
exec-maven-plugin
is used to invoke REST API and configure Couchbase server and is configured in a Maven profile. Make sure to setupdocker.host
property inpom.xml
. - Deploy the application to WildFly:
123456789101112131415161718192021mvn install -Pwildfly -Dwildfly.hostname=192.168.99.100 -Dwildfly.username=admin -Dwildfly.password=Admin#007[INFO] Scanning for projects...[INFO][INFO] ------------------------------------------------------------------------[INFO] Building couchbase-javaee 1.0-SNAPSHOT[INFO] ------------------------------------------------------------------------. . .Nov 26, 2015 5:17:39 AM org.xnio.Xnio <clinit>INFO: XNIO version 3.3.1.FinalNov 26, 2015 5:17:39 AM org.xnio.nio.NioXnio <clinit>INFO: XNIO NIO Implementation Version 3.3.1.FinalNov 26, 2015 5:17:39 AM org.jboss.remoting3.EndpointImpl <clinit>INFO: JBoss Remoting version 4.0.9.Final[INFO] Authenticating against security realm: ManagementRealm[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------
Make sure to specify the correct host on CLI. In this case, this is the IP address obtained using
docker-machine ip default
.
Invoke the Application
- Invoke the REST endpoint using cURL:
12345678910111213141516171819202122curl -v http://192.168.99.100:8080/couchbase-javaee/resources/airline* Hostname was NOT found in DNS cache* Trying 192.168.99.100...* Connected to 192.168.99.100 (192.168.99.100) port 8080 (#0)> GET /couchbase-javaee/resources/airline HTTP/1.1> User-Agent: curl/7.37.1> Host: 192.168.99.100:8080> Accept: */*>< HTTP/1.1 200 OK< Connection: keep-alive< X-Powered-By: Undertow/1* Server WildFly/8 is not blacklisted< Server: WildFly/8< Content-Type: application/octet-stream< Content-Length: 1393< Date: Thu, 26 Nov 2015 13:18:36 GMT<* Connection #0 to host 192.168.99.100 left intact[{"travel-sample":{"id":112,"iata":"5W","icao":"AEU","name":"Astraeus","callsign":"FLYSTAR","type":"airline","country":"United Kingdom"}}, {"travel-sample":{"id":109,"iata":"KO","icao":"AER","name":"Alaska Central Express","callsign":"ACE AIR","type":"airline","country":"United States"}}, {"travel-sample":{"id":1191,"iata":"UU","icao":"REU","name":"Air Austral","callsign":"REUNION","type":"airline","country":"France"}}, {"travel-sample":{"id":1203,"iata":"A5","icao":"RLA","name":"Airlinair","callsign":"AIRLINAIR","type":"airline","country":"France"}}, {"travel-sample":{"id":10642,"iata":null,"icao":"JRB","name":"Jc royal.britannica","callsign":null,"type":"airline","country":"United Kingdom"}}, {"travel-sample":{"id":13633,"iata":"WQ","icao":"PQW","name":"PanAm World Airways","callsign":null,"type":"airline","country":"United States"}}, {"travel-sample":{"id":137,"iata":"AF","icao":"AFR","name":"Air France","callsign":"AIRFRANS","type":"airline","country":"France"}}, {"travel-sample":{"id":10748,"iata":"ZQ","icao":"LOC","name":"Locair","callsign":"LOCAIR","type":"airline","country":"United States"}}, {"travel-sample":{"id":10,"iata":"Q5","icao":"MLA","name":"40-Mile Air","callsign":"MILE-AIR","type":"airline","country":"United States"}}, {"travel-sample":{"id":10226,"iata":"A1","icao":"A1F","name":"Atifly","callsign":"atifly","type":"airline","country":"United States"}}]
Complete set of REST endpoints are documented at CRUD Java Application with Couchbase, Java EE and WildFly. They are listed here for convenience:
- GET a single airline:
123curl -v http://192.168.99.100:8080/couchbase-javaee/resources/airline/139 -
Create a new airline using POST:
123curl -v -H "Content-Type: application/json" -X POST -d '{"country":"France","iata":"A5","callsign":"AIRLINAIR","name":"Airlinair","icao":"RLA","type":"airline"}' http://192.168.99.100:8080/couchbase-javaee/resources/airline - Update an existing airline using PUT:
123curl -v -H "Content-Type: application/json" -X PUT -d '{"country":"France","iata":"A5","callsign":"AIRLINAIR","name":"Airlin Air","icao":"RLA","type":"airline","id": "19810"}' http://192.168.99.100:8080/couchbase-javaee/resources/airline/19810 - Delete an existing airline using DELETE:
123curl -v -X DELETE http://192.168.99.100:8080/couchbase-javaee/resources/airline/19810
- GET a single airline:
Enjoy!
Really great post, with enough info. Thanks for sharing these codes.
Great post… thanks… Not to be nit-picky but the title is miss leading, Your example is not a multi-host example. In order for this example to be multi-host you would need to use the overlay driver and a key value store install for the docker engine. For example, you would need to do three things for this to work as a multi-host example.
1) Implement a clustered or single node key value store (consul, etcd or zookeeper) that all Docker hosts (that need to see the network) can advertise to.
2) Create the network using the “docker network create -d overlay” before doing the compose up command.
3) add the net: in the compose yaml.
John Willis
@botchagalupe
John,
Thanks for your comment, title updated.
A true multi-host networking sample is https://htmlpreview.github.io/?https://github.com/javaee-samples/docker-java/blob/master/readme.html#Docker_Swarm that explains the steps clearly.
Blog coming on that soon, stay tuned.
Arun
Excellent example…. Most people don’t know that Swarm uses overlay by default with experimental. Great work…
John
Thanks, more details would be coming out on that soon!
Thanks for publishing such useful information.
Thank you for your information, it is quite helpful.
Really great post. Thanks for sharing those codes. I found them useful.