|
| 1 | +# Eclipse GlassFish Embedded Docker Image |
| 2 | + |
| 3 | +[Eclipse GlassFish](https://glassfish.org) is a Jakarta EE compatible implementation. This is the **Embedded** version that runs as a single JAR file without requiring a full server installation. |
| 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 Embedded |
| 12 | + |
| 13 | +Run GlassFish Embedded with the following command: |
| 14 | + |
| 15 | +``` |
| 16 | +docker run -p 8080:8080 @docker.glassfish.embedded.repository@ |
| 17 | +``` |
| 18 | + |
| 19 | +Or with a command for a specific tag (GlassFish version): |
| 20 | + |
| 21 | +``` |
| 22 | +docker run -p 8080:8080 @docker.glassfish.embedded.image@ |
| 23 | +``` |
| 24 | + |
| 25 | +Open the following URL in the browser to access the HTTP port: |
| 26 | + |
| 27 | +* http://localhost:8080 |
| 28 | + |
| 29 | +**Note:** GlassFish Embedded does not include the Administration Console. Use the full GlassFish Server image if you need administrative capabilities. |
| 30 | + |
| 31 | +### Stop GlassFish Embedded |
| 32 | + |
| 33 | +Stop GlassFish Embedded with the following command: |
| 34 | + |
| 35 | +``` |
| 36 | +docker stop CONTAINER_ID |
| 37 | +``` |
| 38 | + |
| 39 | +CONTAINER_ID can be found from the output of the following command: |
| 40 | + |
| 41 | +``` |
| 42 | +docker ps |
| 43 | +``` |
| 44 | + |
| 45 | +## Run an application with GlassFish Embedded in Docker |
| 46 | + |
| 47 | +You can run an application located in your filesystem with GlassFish Embedded in a Docker container. |
| 48 | + |
| 49 | +Follow these steps: |
| 50 | + |
| 51 | +1. Create an empty directory on your filesystem, e.g. `/deployments` |
| 52 | +2. Copy the application package to this directory - so that it's for example on the path `/deployments/application.war` |
| 53 | +3. Run the following command to start GlassFish Embedded in Docker with your application, where /deployments is the directory created in step 1 and /deploy is the directory inside the container where Embedded GlassFish expects applications: |
| 54 | + |
| 55 | +``` |
| 56 | +docker run -p 8080:8080 -v /deployments:/deploy @docker.glassfish.embedded.repository@ |
| 57 | +``` |
| 58 | + |
| 59 | +Then you can open the application in the browser with: |
| 60 | + |
| 61 | +* http://localhost:8080 |
| 62 | + |
| 63 | +If there's a single application, if will be available under the roo (`/`) context root. If there are multiple applications, each will be available under a context root derived from the name of the application file (e.g. `application.war` would be deployed under the `/application` context root). |
| 64 | + |
| 65 | +## Debug GlassFish Embedded inside a Docker container |
| 66 | + |
| 67 | +You can enable debug mode by specifying JVM debug arguments in the standard `JAVA_TOOL_OPTIONS` environment variable and expose the debug port. For example: |
| 68 | + |
| 69 | +``` |
| 70 | +docker run -p 9009:9009 -p 8080:8080 -e 'JAVA_TOOL_OPTIONS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:9009' @docker.glassfish.embedded.repository@ |
| 71 | +``` |
| 72 | + |
| 73 | +Then connect your debugger to port 9009 on `localhost`. |
| 74 | + |
| 75 | +If you need to suspend GlassFish startup until you connect the debugger, change `suspend=n` to `suspend=y`: |
| 76 | + |
| 77 | +``` |
| 78 | +docker run -p 9009:9009 -p 8080:8080 -e 'JAVA_TOOL_OPTIONS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:9009' @docker.glassfish.embedded.repository@ |
| 79 | +``` |
| 80 | + |
| 81 | +## Environment variables |
| 82 | + |
| 83 | +The following environment variables are available: |
| 84 | + |
| 85 | +* `PATH_GF_HOME` - directory that contains Embedded GlassFish JAR and is used as the default working directory (`/opt/glassfish`) |
| 86 | +* `PATH_GF_JAR` - path to the Embedded GlassFish JAR file |
| 87 | +* `GLASSFISH_VERSION` - version of GlassFish |
| 88 | + |
| 89 | +## Additional configuration |
| 90 | + |
| 91 | +### Custom JVM arguments |
| 92 | + |
| 93 | +You can pass custom JVM arguments by setting them in the standard `JAVA_TOOL_OPTIONS` variable |
| 94 | + |
| 95 | +``` |
| 96 | +docker run -p 8080:8080 -e 'JAVA_TOOL_OPTIONS=-Xmx512m -Dmy.property=value' @docker.glassfish.embedded.repository@ |
| 97 | +``` |
| 98 | + |
| 99 | +### Custom application deployment |
| 100 | + |
| 101 | +Applications placed in the `/deploy` directory will be automatically deployed at startup: |
| 102 | + |
| 103 | +``` |
| 104 | +docker run -p 8080:8080 -v /path/to/apps:/deploy @docker.glassfish.embedded.repository@ |
| 105 | +``` |
| 106 | + |
| 107 | +Paths to applications in different locations can be passed on command line, e.g.: |
| 108 | + |
| 109 | +``` |
| 110 | +docker run -p 8080:8080 @docker.glassfish.embedded.repository@ /mydeployments/myapp.war |
| 111 | +``` |
| 112 | + |
| 113 | +NOTE: If you point to a path on the command line, the path must exist inside the container. If it's not there, mount a local directory, e.g. using the `-v` option. |
| 114 | + |
| 115 | +### Using with custom Dockerfile |
| 116 | + |
| 117 | +You can create a custom Docker image based on GlassFish Embedded: |
| 118 | + |
| 119 | +```dockerfile |
| 120 | +FROM @docker.glassfish.embedded.repository@ |
| 121 | + |
| 122 | +# Copy your application |
| 123 | +COPY myapp.war /deploy/ |
| 124 | + |
| 125 | +# Set custom JVM options |
| 126 | +ENV JAVA_TOOL_OPTIONS=-Xmx512m |
| 127 | +``` |
| 128 | + |
| 129 | +## Examples of advanced usage |
| 130 | + |
| 131 | +### Running with specific JVM settings |
| 132 | + |
| 133 | +```bash |
| 134 | +docker run -p 8080:8080 -e JAVA_TOOL_OPTIONS='-Xmx1g -XX:+UseG1GC' @docker.glassfish.embedded.repository@ |
| 135 | +``` |
| 136 | + |
| 137 | +### Running in background with logs |
| 138 | + |
| 139 | +```bash |
| 140 | +docker run -d -p 8080:8080 @docker.glassfish.embedded.repository@ |
| 141 | +CONTAINER_ID=$(docker ps -q --filter ancestor=@docker.glassfish.embedded.repository@) |
| 142 | +docker logs -f $CONTAINER_ID |
| 143 | +``` |
| 144 | + |
| 145 | +### Running with custom user (useful for Kubernetes) |
| 146 | + |
| 147 | +```bash |
| 148 | +docker run --user 1000 -p 8080:8080 @docker.glassfish.embedded.repository@ |
| 149 | +``` |
| 150 | + |
| 151 | +## TestContainers |
| 152 | + |
| 153 | +This is a simple test example using [Embedded GlassFish ](https://glassfish.org/) and [TestContainers](https://www.testcontainers.org/): |
| 154 | + |
| 155 | +```java |
| 156 | +@Testcontainers |
| 157 | +public class EmbeddedGlassFishITest { |
| 158 | + |
| 159 | + @Container |
| 160 | + private final GenericContainer server = new GenericContainer<>("@docker.glassfish.embedded.image@") |
| 161 | + .withExposedPorts(8080); |
| 162 | + |
| 163 | + @Test |
| 164 | + void testServerStartup() throws Exception { |
| 165 | + URL url = new URL("http://localhost:" + server.getMappedPort(8080) + "/"); |
| 166 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 167 | + try { |
| 168 | + connection.setRequestMethod("GET"); |
| 169 | + assertEquals(200, connection.getResponseCode()); |
| 170 | + } finally { |
| 171 | + connection.disconnect(); |
| 172 | + } |
| 173 | + } |
| 174 | +} |
| 175 | +``` |
0 commit comments