Although Docker is often used to deploy/run applications in a defined environment, you can also use Docker in your development environment. To make this productive, you need an easy way to deploy and debug your web application. In the Glassfish image (see https://github.com/koert/docker-glassfish) I can add deploy a web application and hook into the file system.
Glassfish provides an autodeploy directory – just copy a war or ear file to this directory and Glassfish will deploy this automatically. You can mount this directory to your host with the -v option:
docker run ... -v ~/tmp/glassfish/deploy:/opt/glassfish4/glassfish/domains/domain1/autodeploy ...
To view the logs directory you can add this:
docker run ... -v ~/tmp/glassfish/logs:/opt/glassfish4/glassfish/domains/domain1/logs ...
I cloned a copy of the Spring petclinic application (https://github.com/koert/spring-petclinic) to setup my development environment. Curiously, I needed to add “implements Serializable” to BaseEntity, to make it work on Glassfish 4.1. Normally, the petclinic application creates its own datasource, not a JEE datasource. I changed the configuration to use the Glassfish datasource.
The Glassfish server now needs a datasource configuration, including the database driver implementation for hsqldb. By adding the jar to /opt/app/extlib, the driver is copied to the Glassfish lib directory – this makes it available for datasources.
The configure-glassfish.sh (in /opt/app/bin) script creates the necessary datasource:
asadmin create-jdbc-connection-pool --restype=javax.sql.DataSource --datasourceclassname=org.hsqldb.jdbc.JDBCDataSource \
--property "user=sa:password=sa:url=jdbc\:hsqldb\:hsql\:mem\:petclinic" \
asadmin create-jdbc-resource --connectionpoolid PetClinicPool jdbc/petclinic
In the Dockerfile I add the hsqldb jar and the configure script:
ADD hsqldb-2.3.2.jar /opt/app/extlib/hsqldb-2.3.2.jar
ADD configure-glassfish.sh /opt/app/bin/configure-glassfish.sh
After building the Docker image, you can run Glassfish with the necessary configuration:
docker run --rm -it -v ~/tmp/glassfish/deploy:/opt/glassfish4/glassfish/domains/domain1/autodeploy \
-v ~/tmp/glassfish/logs:/opt/glassfish4/glassfish/domains/domain1/logs \
-p 4848:4848 -p 8080:8080 -p 9009:9009 \
-e DEBUG="true" koert/glassfish-showcase
The “–rm” option instructs Docker not to keep the state of the started container, so you can start the image fresh every time. The “-it” option keeps the container running in the foreground – you can stop it with Ctrl-C.
While this is running, you can deploy your web application by copying its war or ear file to the ~/tmp/glassfish/deploy directory. You can debug the application by letting your IDE connect to port 9009.
This way you have a consist development environment with a well defined state.