|
| 1 | +# Eclipse GlassFish Docker images |
| 2 | + |
| 3 | +[Eclipse GlassFish](https://glassfish.org) is a Jakarta EE compatible implementation. |
| 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:8.0.0 |
| 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 | +## Run an application with GlassFish in Docker |
| 45 | + |
| 46 | +You can run an application located in your filesystem with GlassFIsh in a Docker container. |
| 47 | + |
| 48 | +Follow these steps: |
| 49 | + |
| 50 | +1. Create an empty directory on your filesystem, e.g. `/deployment` |
| 51 | +2. Copy the application package to this directory - so that it's for example on the path `/deployment/application.war` |
| 52 | +3. Run the following command to start GlassFish in Docker with your application, where `/deployments` is path to the directory created in step 1, and /deploy is the directory in the container where GlassFish expects applications: |
| 53 | + |
| 54 | +``` |
| 55 | +docker run -p 8080:8080 -p 4848:4848 -v /deployments:/deploy glassfish |
| 56 | +``` |
| 57 | + |
| 58 | +Alternatively, you can mount a specific WAR file directly: |
| 59 | + |
| 60 | +``` |
| 61 | +docker run -p 8080:8080 -p 4848:4848 -v /deployment/application.war:/deploy/application.war glassfish |
| 62 | +``` |
| 63 | + |
| 64 | +**Note**: GlassFish Server deploys applications using the WAR filename as the context path (e.g., `application.war` becomes accessible at `/application/`). |
| 65 | + |
| 66 | +Then you can open the application in the browser with: |
| 67 | + |
| 68 | +* http://localhost:9080/application |
| 69 | + |
| 70 | +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. |
| 71 | + |
| 72 | +## Debug GlassFish Server inside a Docker container |
| 73 | + |
| 74 | +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. |
| 75 | + |
| 76 | +``` |
| 77 | +docker run -p 9009:9009 -p 8080:8080 -p 4848:4848 glassfish startserv --debug |
| 78 | +``` |
| 79 | + |
| 80 | +Then connect your debugger to the port 9009 on `localhost`. |
| 81 | + |
| 82 | +If you need suspend GlassFish startup until you connect the debugger, use the `--suspend` argument instead: |
| 83 | + |
| 84 | +``` |
| 85 | +docker run -p 9009:9009 -p 8080:8080 -p 4848:4848 glassfish startserv --suspend |
| 86 | +``` |
| 87 | + |
| 88 | +## Environment variables |
| 89 | + |
| 90 | +The following environment variables can be set to configure GlassFish Server: |
| 91 | + |
| 92 | +* `AS_ADMIN_MASTERPASSWORD` - to change the default master password |
| 93 | +* `AS_ADMIN_PASSWORD` - to change the default admin password |
| 94 | + |
| 95 | +The following environment variables are read-only and can be used in derived Docker images or scripts: |
| 96 | + |
| 97 | +* `AS_PASSWORD_FILE` - path to the password file with the admin password. It's applied by default to any asadmin command |
| 98 | +* `AS_ADMIN_USER` - name of the admin user. It's applied by default to any asadmin command |
| 99 | +* `PATH_GF_HOME` - directory of the GlassFish installation. Also the default working directory |
| 100 | +* `PATH_GF_SERVER_LOG` - path to the server log file |
| 101 | + |
| 102 | +## Additional configuration |
| 103 | + |
| 104 | +It's possible to specify custom commands to run in the Docker container before GlassFish server starts. The following methods are supported: |
| 105 | + |
| 106 | +* `${PATH_GF_HOME}/custom/init.sh` - Execute any `bash` script, which can execute admin commands via the `asadmin` command line tool |
| 107 | +* `${PATH_GF_HOME}/custom/init.asadmin` - Execute asadmin commands directly |
| 108 | + |
| 109 | +If both of the above scripts are present, they are executed in this order: |
| 110 | + |
| 111 | +1. `init.sh` |
| 112 | +2. `init.asadmin` |
| 113 | + |
| 114 | +However, always consider to executing any asadmin configuration commands during build, because configuring the server at container startup will prolong the startup time. |
| 115 | + |
| 116 | +### Execute asadmin commands before server startup |
| 117 | + |
| 118 | +Just create a file `${PATH_GF_HOME}/custom/init.asadmin` (`/opt/glassfish7/custom/init.asadmin`), the commands will be executed before GlassFish server starts. |
| 119 | + |
| 120 | +Within the `init.asadmin` file, you can specify any asadmin command. Most of the commands require that the server is running, so you'll need to start the server first, run the configuration commands, and then stop the server. |
| 121 | + |
| 122 | +For example, to start GlassFish, increase the maximum amount of threads, and then stop it, the `init.asadmin` file can contain: |
| 123 | + |
| 124 | +``` |
| 125 | +start-domain |
| 126 | +set configs.config.server-config.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size=1000 |
| 127 | +stop-domain |
| 128 | +``` |
| 129 | + |
| 130 | +You can provide the file by mounting its directory to the `/opt/glassfish7/custom` directory in the container when running the container: |
| 131 | + |
| 132 | +``` |
| 133 | +docker run -v ./custom:/opt/glassfish7/custom -p 8080:8080 -ti glassfish |
| 134 | +``` |
| 135 | + |
| 136 | +### Execute a `bash` script before server startup |
| 137 | + |
| 138 | +Just create a Bash script `${PATH_GF_HOME}/custom/init.sh` (`/opt/glassfish7/custom/init.sh`), it will be executed before GlassFish server starts. |
| 139 | + |
| 140 | +Within the `init.sh` script, you can run any asadmin command, with `asadmin --interactive=false multimode COMMAND`. Most of the commands require that the server is running, so you'll need to start the server first, run the configuration commands, and then stop the server. If you need to run multiple commands, we recomment running asadmin commands in a single "multimode" asadmin execution to run them faster, with commands provided either on standard input or in a separate file via the `asadmin --interactive=false multimode -f` option. |
| 141 | + |
| 142 | +---- |
| 143 | + |
| 144 | +**NOTE:** If you only need to execute `asadmin` commands before server startup, it's easier to use the init.asadmin script to execute them directly, without a `bash` script. |
| 145 | + |
| 146 | +---- |
| 147 | + |
| 148 | +For example, to start GlassFish, increase the maximum amount of threads, and then stop it, the `init.sh` script can contain: |
| 149 | + |
| 150 | +``` |
| 151 | +echo "start-domain |
| 152 | +set configs.config.server-config.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size=1000 |
| 153 | +stop-domain" | asadmin --interactive=false |
| 154 | +``` |
| 155 | + |
| 156 | +You can provide the script by mounting its directory to the `/opt/glassfish7/custom` directory in the container when running the container: |
| 157 | + |
| 158 | +``` |
| 159 | +docker run -v ./custom:/opt/glassfish7/custom -p 8080:8080 -ti glassfish |
| 160 | +``` |
| 161 | + |
| 162 | +### Execute `asadmin` commands during Docker image build |
| 163 | + |
| 164 | +Applying the configuration can be a lengthy operation. If you can configure the server during build time, it's recommended running asadmin configuration commands in a custom Docker image. This moves the configuration step to the image build time instead of runtime. |
| 165 | + |
| 166 | +To do it, simply add `RUN instructions that run `asadmin` script with the usual arguments. For example, to move the example configuration from the `init.sh` script above to Dockerfile: |
| 167 | + |
| 168 | +File `Dockerfile`: |
| 169 | + |
| 170 | +``` |
| 171 | +FROM glassfish |
| 172 | +
|
| 173 | +RUN printf "start-domain \n \ |
| 174 | + set configs.config.server-config.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size=1000 \n \ |
| 175 | + stop-domain" | asadmin --interactive=false |
| 176 | +``` |
| 177 | + |
| 178 | +Or you can put the asadmin commands into a separate file and run it using `asadmin --interactive=false multimode -f`. For example: |
| 179 | + |
| 180 | +File `commands.asadmin`: |
| 181 | + |
| 182 | +``` |
| 183 | +start-domain |
| 184 | +set configs.config.server-config.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size=1000 |
| 185 | +stop-domain |
| 186 | +``` |
| 187 | + |
| 188 | +File `Dockerfile`: |
| 189 | + |
| 190 | +``` |
| 191 | +FROM glassfish |
| 192 | +
|
| 193 | +COPY commands.asadmin commands.asadmin |
| 194 | +
|
| 195 | +RUN asadmin --interactive=false multimode -f commands.asadmin |
| 196 | +``` |
| 197 | + |
| 198 | +## Examples of advanced usage |
| 199 | + |
| 200 | +Let's try something more complicated. |
| 201 | + |
| 202 | +* 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: |
| 203 | + |
| 204 | +```bash |
| 205 | +docker run glassfish startserv --debug mydomain |
| 206 | +``` |
| 207 | + |
| 208 | +* Environment variable `AS_TRACE=true` enables tracing of the GlassFish startup. It is useful when the server doesn't start without any useful logs. |
| 209 | + |
| 210 | +* `docker run` with the `--user` argument configures explicit user id for the container. It can be useful for K8S containers. |
| 211 | + |
| 212 | +* `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. |
| 213 | + |
| 214 | +```bash |
| 215 | +docker run -d glassfish |
| 216 | +``` |
| 217 | + |
| 218 | +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 ): |
| 219 | + |
| 220 | +```bash |
| 221 | +docker run -d -e AS_TRACE=true --user 1000 glassfish startserv --debug=true |
| 222 | +5a11f2fe1a9dd1569974de913a181847aa22165b5015ab20b271b08a27426e72 |
| 223 | + |
| 224 | +docker logs 5a11f2fe1a9dd1569974de913a181847aa22165b5015ab20b271b08a27426e72 |
| 225 | +... |
| 226 | + |
| 227 | +docker stop 5a11f2fe1a9dd1569974de913a181847aa22165b5015ab20b271b08a27426e72 |
| 228 | +``` |
| 229 | + |
| 230 | +## TestContainers |
| 231 | + |
| 232 | +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. |
| 233 | + |
| 234 | +If you want to run more complicated tests, the good path is to |
| 235 | + |
| 236 | +1. Write a singleton managing the GlassFish Docker Container or the whole test environment. |
| 237 | +2. Write your own Junit5 extension which would start the container before your test and ensure that everything stops after the test including failures. |
| 238 | +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. |
| 239 | + |
| 240 | +```java |
| 241 | +@Testcontainers |
| 242 | +public class WelcomePageITest { |
| 243 | + |
| 244 | + @Container |
| 245 | + private final GenericContainer server = new GenericContainer<>("glassfish:8.0.0").withExposedPorts(8080); |
| 246 | + |
| 247 | + @Test |
| 248 | + void getRoot() throws Exception { |
| 249 | + URL url = new URL("http://localhost:" + server.getMappedPort(8080) + "/"); |
| 250 | + StringBuilder content = new StringBuilder(); |
| 251 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 252 | + try { |
| 253 | + connection.setRequestMethod("GET"); |
| 254 | + try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { |
| 255 | + String inputLine; |
| 256 | + while ((inputLine = in.readLine()) != null) { |
| 257 | + content.append(inputLine); |
| 258 | + } |
| 259 | + } |
| 260 | + } finally { |
| 261 | + connection.disconnect(); |
| 262 | + } |
| 263 | + assertThat(content.toString(), stringContainsInOrder("Eclipse GlassFish", "index.html", "production-quality")); |
| 264 | + } |
| 265 | + |
| 266 | +} |
| 267 | +``` |
0 commit comments