With Docker it’s easy to develop applications in an isolated, deterministic and secure environment. So let’s see how we can leverage Docker features to develop JEE applications.
We will see how to create a container that features Glassfish (the reference implementation for JEE application servers) and it’s default database JavaDB (Derby).
To simply test how it works on a sample project you can use this one: https://github.com/MicheleGuerriero/se2-class-example/.
mvn compile war:war
). To simplify this guide I’ll refer to this .war as “example-0.0.1-SNAPSHOT.war”.docker build -t example-IMAGE .
to build the docker image described in “Dockerfile”.docker run --name example-CONTAINER -p 8080:8080 -p 4848:4848 -i -t example-IMAGE
to start a container based on the created imageThe Dockerfile (saved in a file called “Dockerfile”) looks like this:
FROM glassfish # official Docker Glassfish image
# ADD WAR (change it accordingly to the name of your .war file)
ADD example-0.0.1-SNAPSHOT.war /usr/local/glassfish4/glassfish/domains/domain1/autodeploy/
# SETUP/START GF
ADD init\_gf.sh /home/ RUN chmod +x /home/init\_gf.sh CMD /home/init\_gf.sh
# EXPOSE CONTAINER PORTS (8080 for the JEE application and 4848 for the Glassfish admin area)
EXPOSE 8080 4848
The script to setup and initialize Glassfish (saved in a file called “init_gf.sh”) looks like this:
#!/bin/sh
# change password (it's horrible, but gf seems to accept as password parameters files only...)
echo "AS\_ADMIN\_PASSWORD= AS\_ADMIN\_NEWPASSWORD=password" >> /home/gf-password.txt asadmin --user=admin --passwordfile=/home/gf-password.txt change-admin-password --domain\_name domain1
rm /home/gf-password.txt
# enable secure area asadmin start-domain
echo "AS\_ADMIN_PASSWORD=password" >> /home/gf-password.txt asadmin --user=admin --passwordfile=/home/gf-password.txt enable-secure-admin rm /home/gf-password.txt asadmin --user=admin stop-domain
# finally start db and glassfish
asadmin start-database
asadmin start-domain -v
docker exec -i -t class-example-CONTAINER /bin/bash
tip: exploded project in the container is located here: “/usr/local/glassfish4/glassfish/domains/domain1/applications/example-0.0.1-SNAPSHOT”