using docker on mac to run java 9

Last week, I was trying to install Java 9 when I messed up my Mac and had to recover using Time Machine. I didn’t have difficulty using the Java 8 preview. Anyway, I realized I should be using a VM for Java 9 so this doesn’t happen again.

Conveniently, I was reading a book about Docker on the plane when I was traveling right after that. Which meant I had a use case for installing Docker on my home machine. It was easy and I went from not having Docker installed to being able to use it for Java 9 in under 30 minutes.

Installing

Since Mac isn’t Linux, the first step is to install the Docker Toolbox for OS X. This uses half a gig of disk space. Which is fair since it is running a VM.  Overall, the Docker getting started docs for Mac are excellent. It took a few minutes the first time I ran the Docker Quickstart Client as I saw:

Creating CA: /Users/nyjeanne/.docker/machine/certs/ca.pem
Creating client certificate: /Users/nyjeanne/.docker/machine/certs/cert.pem
Running pre-create checks...
Creating machine...
(default) Copying /Users/nyjeanne/.docker/machine/cache/boot2docker.iso to /Users/nyjeanne/.docker/machine/machines/default/boot2docker.iso...
(default) Creating VirtualBox VM...
(default) Creating SSH key...
(default) Starting the VM...
(default) Check network to re-create if needed...
(default) Found a new host-only adapter: "vboxnet1"
(default) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: /usr/local/bin/docker-machine env default
##         .
## ## ##        ==
## ## ## ## ##    ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
\______ o           __/
\    \         __/
\____\_______/
docker is configured to use the default machine with IP 192.168.99.100
For help getting started, check out the docs at https://docs.docker.com

Initial test

I ran:

docker run hello-world

It worked as expected. Pretty easy.

Java 9 beta container

Finding the relevant library on DockerHub was pretty easy. There is an official Java library.

First I accidentally ran docker with only the tag:

docker run openjdk-9

It probably isn’t a surprise that I got an error. It just wasn’t the error I expected:

docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.

Luckily, a github issue helped. I needed to set the environment variables. The issue said to run:

eval $(docker-machine env default)

I ran the docker-machine command first so I knew what I was evaluating. It was harmless

export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/nyjeanne/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell:
# eval $(docker-machine env default)

Then I got the message I was expecting:

Unable to find image 'openjdk-9:latest' locally
Pulling repository docker.io/library/openjdk-9
docker: Error: image library/openjdk-9 not found.
See 'docker run --help'.

Running the command correctly was far more productive:

docker run java:openjdk-9

It started downloading the container. While it was downloading, it looked like:

52e20300f877: Downloading 20.54 MB/52.71 MB
a3ed95caeb02: Download complete
fb4c7723b752: Download complete
45c1fd7a2514: Download complete
d12ad3d1f060: Download complete
80f5aeb42fe5: Download complete
a6fac263fca7: Download complete
8cb87a5d8c56: Downloading 15.12 MB/215.8 MB

And when it was done, I had:

Unable to find image 'java:openjdk-9' locally
openjdk-9: Pulling from library/java
52e20300f877: Pull complete
a3ed95caeb02: Pull complete
fb4c7723b752: Pull complete
45c1fd7a2514: Pull complete
d12ad3d1f060: Pull complete
80f5aeb42fe5: Pull complete
a6fac263fca7: Pull complete
8cb87a5d8c56: Pull complete
Digest: sha256:49a691b3e64950a45a45dbac6fde08636bded81acf4a36807d12208d3d3af293
Status: Downloaded newer image for java:openjdk-9

Then I ran it interactive mode so I could test jshell:

docker run -i java:openjdk-9

By default, there is no prompt, but you can type unix commands. I typed

jshell

To get out of jshell, you type /exit and to get of the container, you type exit.

End to end success

This is pretty easy when you aren’t doing it wrong:

nyjeanne$ docker run -i java:openjdk-9
javac -version
javac 9-internal
jshell
May 02, 2016 12:17:02 AM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.
|  Welcome to JShell -- Version 9-internal
|  For an introduction type: /help intro
jshell> 1+2
1+2
$1 ==> 3
jshell> new File("abc")
new File("abc")
$2 ==> abc
 
jshell> java.util.stream.Stream.of(11, 2, 16).sorted().findFirst()
java.util.stream.Stream.of(11, 2, 16).sorted().findFirst()
$3 ==> Optional[2]
 
jshell> /exit
/exit
|  Goodbye
exit

Initial impressions of JShell

Granted it isn’t released yet, but I’m not more impressed with JShell than I was with Nashorn’s jjs. There’s still no tab autocompleted or up arrow support. And while there are a couple imports included by default, NIO and Streams packages are not in that list.

Leave a Reply

Your email address will not be published. Required fields are marked *