DevOps

Docker Multi-Host Networking with Couchbase and WildFly

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

  1. 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:
    mycouchbase:
      container_name: "db"
      image: couchbase/server
      ports:
        - 8091:8091
        - 8092:8092 
        - 8093:8093 
        - 11210:11210
    mywildfly:
      image: arungupta/wildfly-admin
      environment:
        - COUCHBASE_URI=db
      ports:
        - 8080:8080
        - 9990:9990

    arungupta/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 using COUCHBASE_URI. This is then used to connect to Couchbase from the Java EE application.

    The application environment is started as:

    docker-compose --x-networking up -d
    Creating network "wildflycouchbasejavaee7" with driver "None"
    Creating wildflycouchbasejavaee7_mywildfly_1
    Creating 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:

    docker network ls
    NETWORK ID          NAME                      DRIVER
    968bde2b7745        bridge                    bridge              
    79e937bcf1f6        none                      null                
    d06a4bd59a4b        host                      host                
    5e26a9a205ff        wildflycouchbasejavaee7   bridge

    Issue 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

  1. Clone couchbase-javaee repo:
    https://github.com/arun-gupta/couchbase-javaee
  2. Configure Couchbase server:
    mvn 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 setup docker.host property in pom.xml.

  3. Deploy the application to WildFly:
    mvn 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.Final
    Nov 26, 2015 5:17:39 AM org.xnio.nio.NioXnio <clinit>
    INFO: XNIO NIO Implementation Version 3.3.1.Final
    Nov 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

  1. Invoke the REST endpoint using cURL:
    curl -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:

    1. GET a single airline:
      curl -v http://192.168.99.100:8080/couchbase-javaee/resources/airline/139
    2. Create a new airline using POST:
      curl -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
    3. Update an existing airline using PUT:
      curl -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
       
    4. Delete an existing airline using DELETE:
      curl -v -X DELETE http://192.168.99.100:8080/couchbase-javaee/resources/airline/19810

Enjoy!

Arun Gupta

Arun is a technology enthusiast, avid runner, author of a best-selling book, globe trotter, a community guy, Java Champion, JavaOne Rockstar, JUG Leader, Minecraft Modder, Devoxx4Kids-er, and a Red Hatter.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button