|
| 1 | +# Eclipse GlassFish Docker images |
| 2 | + |
| 3 | +[Eclipse GlassFish](https://glassfish.org) is a Jakarta EE compatible implementation sponsored by the Eclipse Foundation. |
| 4 | + |
| 5 | +%%LOGO%% |
| 6 | + |
| 7 | +**Source code repository of the Docker image:** https://github.com/eclipse-ee4j/glassfish.docker |
| 8 | + |
| 9 | +## Quick start |
| 10 | + |
| 11 | +### Start GlassFish |
| 12 | + |
| 13 | +Run GlassFish with the following command: |
| 14 | + |
| 15 | +``` |
| 16 | +docker run -p 8080:8080 -p 4848:4848 glassfish |
| 17 | +``` |
| 18 | + |
| 19 | +Or with a command for a specific tag (GlassFish version): |
| 20 | + |
| 21 | +``` |
| 22 | +docker run -p 8080:8080 -p 4848:4848 glassfish:7.0.19 |
| 23 | +``` |
| 24 | + |
| 25 | +Open the following URLs in the browser: |
| 26 | + |
| 27 | +* **Welcome screen:** http://localhost:8080 |
| 28 | +* **Administration Console:** https://localhost:4848 - log in using `admin`/`admin` (User name/Password) |
| 29 | + |
| 30 | +### Stop GlassFish |
| 31 | + |
| 32 | +Stop GlassFish with the following command: |
| 33 | + |
| 34 | +``` |
| 35 | +docker stop CONTAINER_ID |
| 36 | +``` |
| 37 | + |
| 38 | +CONTAINER_ID can be found from the output of the following command: |
| 39 | + |
| 40 | +``` |
| 41 | +docker ps |
| 42 | +``` |
| 43 | + |
| 44 | +### Start GlassFish Embedded |
| 45 | + |
| 46 | +Run GlassFish Embedded runnable JAR (static shell) with the following command: |
| 47 | + |
| 48 | +``` |
| 49 | +docker run -it -p 8080:8080 glassfish runembedded |
| 50 | +``` |
| 51 | + |
| 52 | +This will run GlassFish static shell JAR from the GlassFish installation, which is equivalent to running a standalone GlassFish Embedded JAR. If no applications are deployed, GlassFish Embedded will start an interactive prompt to input commands. For this to work in Docker, the `-it` arguments are needed. |
| 53 | + |
| 54 | +To display usage instructions, run: |
| 55 | + |
| 56 | +``` |
| 57 | +docker run -it -p 8080:8080 glassfish runembedded --help |
| 58 | +``` |
| 59 | + |
| 60 | +To deploy an application, copy the application into the Docker image or mount the directory that contains it, and then pass the path to it as an argument. For example, if the application myapp.war is copied to the default `/opt/glassfish7` directory: |
| 61 | + |
| 62 | +``` |
| 63 | +docker run -p 8080:8080 glassfish runembedded myapp.war |
| 64 | +``` |
| 65 | + |
| 66 | + |
| 67 | +## Run an application with GlassFish in Docker |
| 68 | + |
| 69 | +You can run an application located in your filesystem with GlassFIsh in a Docker container. |
| 70 | + |
| 71 | +Follow these steps: |
| 72 | + |
| 73 | +1. Create an empty directory on your filesystem, e.g. `/deployment` |
| 74 | +2. Copy the application package to this directory - so that it's for example on the path `/deployment/application.war` |
| 75 | +3. Run the following command to start GlassFish in Docker with your application, where `/deployments` is path to the directory created in step 1: |
| 76 | + |
| 77 | +``` |
| 78 | +docker run -p 8080:8080 -p 4848:4848 -v /deployments:/opt/glassfish7/glassfish/domains/domain1/autodeploy glassfish |
| 79 | +``` |
| 80 | + |
| 81 | +Then you can open the application in the browser with: |
| 82 | + |
| 83 | +* http://localhost:9080/application |
| 84 | + |
| 85 | +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. |
| 86 | + |
| 87 | +## Debug GlassFish Server inside a Docker container |
| 88 | + |
| 89 | +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. |
| 90 | + |
| 91 | +``` |
| 92 | +docker run -p 9009:9009 -p 8080:8080 -p 4848:4848 glassfish startserv --debug |
| 93 | +``` |
| 94 | + |
| 95 | +Then connect your debugger to the port 9009 on `localhost`. |
| 96 | + |
| 97 | +If you need suspend GlassFish startup until you connect the debugger, use the `--suspend` argument instead: |
| 98 | + |
| 99 | +``` |
| 100 | +docker run -p 9009:9009 -p 8080:8080 -p 4848:4848 glassfish startserv --suspend |
| 101 | +``` |
| 102 | + |
| 103 | +## Examples of advanced usage |
| 104 | + |
| 105 | +Let's try something more complicated. |
| 106 | + |
| 107 | +* 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: |
| 108 | + |
| 109 | +```bash |
| 110 | +docker run glassfish startserv --debug mydomain |
| 111 | +``` |
| 112 | + |
| 113 | +* Environment variable `AS_TRACE=true` enables tracing of the GlassFish startup. It is useful when the server doesn't start without any useful logs. |
| 114 | + |
| 115 | +* `docker run` with the `--user` argument configures explicit user id for the container. It can be useful for K8S containers. |
| 116 | + |
| 117 | +* `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. |
| 118 | + |
| 119 | +```bash |
| 120 | +docker run -d glassfish |
| 121 | +``` |
| 122 | + |
| 123 | +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 ): |
| 124 | + |
| 125 | +```bash |
| 126 | +docker run -d -e AS_TRACE=true --user 1000 glassfish startserv --debug=true |
| 127 | +5a11f2fe1a9dd1569974de913a181847aa22165b5015ab20b271b08a27426e72 |
| 128 | + |
| 129 | +docker logs 5a11f2fe1a9dd1569974de913a181847aa22165b5015ab20b271b08a27426e72 |
| 130 | +... |
| 131 | + |
| 132 | +docker stop 5a11f2fe1a9dd1569974de913a181847aa22165b5015ab20b271b08a27426e72 |
| 133 | +``` |
| 134 | + |
| 135 | +## Running GlassFish Embedded |
| 136 | + |
| 137 | +### Run an application |
| 138 | + |
| 139 | +To deploy an application with GlassFish Embedded, copy the application into the Docker image or mount the directory that contains it, and then pass the path to it as an argument. For example, if the application myapp.war is copied to the default `/opt/glassfish7` directory: |
| 140 | + |
| 141 | +``` |
| 142 | +docker run -p 8080:8080 glassfish runembedded myapp.war |
| 143 | +``` |
| 144 | + |
| 145 | +You can also just copy applications into the /opt/glassfish7/autodeploy directory or mount a directory with applications to it. All applications in that directory will be automatically deployed. For example, if you have applications on a local directory `/deployments`: |
| 146 | + |
| 147 | +``` |
| 148 | +docker run -p 8080:8080 -v /deployments:/opt/glassfish7/autodeploy glassfish runembedded |
| 149 | +``` |
| 150 | + |
| 151 | + |
| 152 | +### Configure GlassFish Embedded |
| 153 | + |
| 154 | +You can configure GlassFish Embedded by command line aguments after the command runembedded, or by a configuration file. Several options are supported, for example set a different HTTP port or disable HTTP listener, set path to a custom domain configuration, run asadmin commands at startup, deploy applications. |
| 155 | + |
| 156 | +You can also just create a configuration file called `glassfish.properties` in the default directory `/opt/glassfish7`, with all the options, including commands to execute and applications to deploy. Or specify path to a different configuration file with the `--properties` option. |
| 157 | + |
| 158 | +To display usage instructions, run: |
| 159 | + |
| 160 | +``` |
| 161 | +docker run -it -p 8080:8080 glassfish runembedded --help |
| 162 | +``` |
| 163 | + |
| 164 | +This Docker image also supports adding custom Java VM arguments, with the JVM_OPTS environments variable. FOr example, you can specify `-XX:MaxRAMPercentage=75` to set maximum heap size to 75% of RAM: |
| 165 | + |
| 166 | +``` |
| 167 | +docker run -it -e JVM_OPTS=="-XX:MaxRAMPercentage=75" -p 8080:8080 glassfish runembedded |
| 168 | +``` |
| 169 | + |
| 170 | + |
| 171 | +### Debug with GlassFish Embedded |
| 172 | + |
| 173 | +To enable debugging, you can either add a custom debugging instruction for the JVM with the `JVM_OPTS` variable, or you can set one of the following environment variables to `true`: |
| 174 | + |
| 175 | +* `DEBUG` - enables remote debugger on port 9009, doesn't suspend the server |
| 176 | +* `SUSPEND` - suspends the server right at the startup and continues when a debugger connects on port 9009 |
| 177 | + |
| 178 | +Example: |
| 179 | + |
| 180 | +``` |
| 181 | +docker run -e SUSPEND=true -p 8080:8080 glassfish:7.0.19 runembedded |
| 182 | +``` |
| 183 | + |
| 184 | +## TestContainers |
| 185 | + |
| 186 | +This is probably the simplest possible test with [GlassFish](https://glassfish.org/) and [TestContainers](https://www.testcontainers.org/). It automatically starts the GlassFish Docker Container and then stops it after the test. The test here is quite trivial - downloads the welcome page and verifies if it contains expected phrases. |
| 187 | + |
| 188 | +If you want to run more complicated tests, the good path is to |
| 189 | + |
| 190 | +1. Write a singleton managing the GlassFish Docker Container or the whole test environment. |
| 191 | +2. Write your own Junit5 extension which would start the container before your test and ensure that everything stops after the test including failures. |
| 192 | +3. You can also implement direct access to the virtual network, containers, so you can change the environment configuration in between tests and simulate network failures, etc. |
| 193 | + |
| 194 | +```java |
| 195 | +@Testcontainers |
| 196 | +public class WelcomePageITest { |
| 197 | + |
| 198 | + @Container |
| 199 | + private final GenericContainer server = new GenericContainer<>("glassfish:7.0.19").withExposedPorts(8080); |
| 200 | + |
| 201 | + @Test |
| 202 | + void getRoot() throws Exception { |
| 203 | + URL url = new URL("http://localhost:" + server.getMappedPort(8080) + "/"); |
| 204 | + StringBuilder content = new StringBuilder(); |
| 205 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 206 | + try { |
| 207 | + connection.setRequestMethod("GET"); |
| 208 | + try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { |
| 209 | + String inputLine; |
| 210 | + while ((inputLine = in.readLine()) != null) { |
| 211 | + content.append(inputLine); |
| 212 | + } |
| 213 | + } |
| 214 | + } finally { |
| 215 | + connection.disconnect(); |
| 216 | + } |
| 217 | + assertThat(content.toString(), stringContainsInOrder("Eclipse GlassFish", "index.html", "production-quality")); |
| 218 | + } |
| 219 | + |
| 220 | +} |
| 221 | +``` |
0 commit comments