In a previous blog on Docker, we ran the GridDB server in one container and the application in another. It worked well but there have been many requests to run GridDB in a container on Docker Desktop and the application on the host.
It should be easy right? Wrong. On a Linux host, it just works but on Windows and MacOSX hosts, the different networking stacks don’t allow direct routing to the container which prevents the usual GridDB configuration from working.
After spending time trying to make Windows or MacOSX Docker behave like Linux, a trick was discovered when configuring GridDB to allow it seamlessly work on Docker Desktop. That trick is setting the container hostname to localhost, so GridDB never sees the Docker bridge network that prevents it from communicating with the host.
The following figure shows how when the GridDB server container doesn’t use localhost, it’s unable to communicate with the client across the 172.16.0.0 network but when the hostname is set to localhost, the connections are successful using the 127.0.0.1 address.
Start The Server
We’ll use the same server image as the previous blog which is hosted on DockerHub.
> docker pull griddbnet/griddb
Now we can start the server:
> docker run --hostname=localhost -d -t griddbnet/griddb
It can take a minute or so for the container to start, so watch the output of the docker log for that container until the cluster has been joined before trying to connect to it.
Run the Application
First fetch Sample1.java from GitHub here. I always remove the package line to not require a certain directory structure to make complilation easier. From there, I edit edit the GridStore Properties to look like the following:
Properties props = new Properties(); props.setProperty("notificationMember", "127.0.0.1:10001"); props.setProperty("clusterName", "defaultCluster"); props.setProperty("user", "admin"); props.setProperty("password", "admin");
The above edits are required because GridDB in the Docker container is configured to use FIXED_LIST networking versus the default MULTICAST networking as described in this previous blog post.
If you need to download the gridstore.jar file, it can be downloaded from MvnRepository.
> javac -cp ".:gridstore-4.6.0.jar" Sample1.java > java -cp ".:gridstore-4.6.0.jar" Sample1 Person: name=name02 status=false count=2 lob=[65, 66, 67, 68, 69, 70, 71, 72, 73, 74]
If you want to connect to GridDB using JDBC in SQLWorkBenchJ, you will need to set the driver to com.toshiba.mwcloud.gs.sql.Driver, the driver JAR path appropriately, and the URL to jdbc:gs:///defaultCluster/public?notificationMember=127.0.0.1:20001, and the username and password to their configured values (admin/admin in the default GridDB container).
In SqlWBConsole, the connect string would look like:
SQL> WbConnect -driver=com.toshiba.mwcloud.gs.sql.Driver -driverJar=gridstore-jdbc-4.6.0.jar -url=jdbc:gs:///defaultCluster/public?notificationMember=127.0.0.1:20001 -username=admin -password=admin
The gridstore-jdbc.jar can also be downloaded from MvnRepository