Skip to content

Example: Using GlassFish Server with the docker Command

Ondro Mihalyi edited this page Oct 11, 2024 · 2 revisions

Start GlassFish

Run GlassFish with the following command. Ports 4848 and 8080 inside the container will be mapped to the host.

docker run -p 8080:8080 -p 4848:4848 ghcr.io/eclipse-ee4j/glassfish

Or with a command for a specific tag (GlassFish version or alias latest):

docker run -p 8080:8080 -p 4848:4848 ghcr.io/eclipse-ee4j/glassfish:latest

Open the following URLs in the browser:

Stop GlassFish

Stop GlassFish with the following command:

docker stop CONTAINER_ID

CONTAINER_ID can be found from the output of the following command:

docker ps

If you are experimenting just on your local machine, you can also just press CTRL+C or use the SIGTERM signal.

Run an application with GlassFish in Docker

You can run an application located in your filesystem with GlassFIsh in a Docker container.

Follow these steps:

  1. Create an empty directory on your filesystem, e.g. /deployment
  2. Copy the application package to this directory - so that it's for example on the path /deployment/application.war
  3. Run the following command to start GlassFish in Docker with your application, where /deployments is path to the directory created in step 1:
docker run -p 8080:8080 -p 4848:4848 -v /deployments:/opt/glassfish7/glassfish/domains/domain1/autodeploy glassfish

Then you can open the application in the browser with:

The context root (application) is derived from the name of the application file (e.g. application.war would deployed under the application context root). If your application file has a different name, please adjust the contest root in the URL accordingly.

Debug GlassFish Server inside a Docker container

You can modify the start command of the Docker container to startserv --debug to enable debug mode. You should also map the debug port 9009.

docker run -p 9009:9009 -p 8080:8080 -p 4848:4848 glassfish startserv --debug

Then connect your debugger to the port 9009 on localhost.

If you need suspend GlassFish startup until you connect the debugger, use the --suspend argument instead:

docker run -p 9009:9009 -p 8080:8080 -p 4848:4848 glassfish startserv --suspend

Examples of advanced usage

Let's try something more complicated.

  • To modify startup arguments for GlassFish, just add startserv to the command line and then add any arguments supported by the asadmin start-domain command. The startserv script is an alias to the asadmin start-domain command but starts GlassFish in a more efficient way that is more suitable in Docker container. For example, to start in debug mode with a custom domain, run:
docker run glassfish startserv --debug mydomain
  • Environment variable AS_TRACE=true enables tracing of the GlassFish startup. It is useful when the server doesn't start without any useful logs.

  • docker run with the --user argument configures explicit user id for the container. It can be useful for K8S containers.

  • docker run with -d starts the container as a daemon, so the shell doesn't print logs and finishes. Docker then returns the container id which you can use for further commands.

docker run -d glassfish

Example of running a Docker container in background, view the logs, and then stop it (with debug enabled, trace logging, and user 1000 convenient for Kubernetes):

docker run -d -e AS_TRACE=true --user 1000 glassfish startserv --debug=true
5a11f2fe1a9dd1569974de913a181847aa22165b5015ab20b271b08a27426e72

docker logs 5a11f2fe1a9dd1569974de913a181847aa22165b5015ab20b271b08a27426e72
...

docker stop 5a11f2fe1a9dd1569974de913a181847aa22165b5015ab20b271b08a27426e72