Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom init.sh script, master/admin passwords via ENV vars #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/main/resources/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ ENV AS_USER=${AS_ADMIN_USER} \
PATH_GF_SERVER_LOG="${PATH_GF_HOME}/glassfish/domains/domain1/logs/server.log"
ENV PATH="${PATH_GF_BIN}:${PATH}"

COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh

RUN true \
&& set -x \
&& apt update \
Expand Down Expand Up @@ -74,12 +72,17 @@ RUN true \
&& asadmin enable-secure-admin \
&& asadmin stop-domain --kill \
&& rm -f ${PATH_GF_SERVER_LOG} ${PATH_GF_PASSWORD_FILE_FOR_CHANGE} \
&& chown -R glassfish:glassfish "${PATH_GF_HOME}" \
&& chmod +x /usr/local/bin/docker-entrypoint.sh \
&& mkdir ${PATH_GF_HOME}/autodeploy \
&& mkdir ${PATH_GF_HOME}/custom \
&& chown -R glassfish:glassfish "${PATH_GF_HOME}" \
&& echo "Installation was successful."

COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN chown glassfish:glassfish /usr/local/bin/docker-entrypoint.sh \
&& chmod +x /usr/local/bin/docker-entrypoint.sh
ENTRYPOINT ["docker-entrypoint.sh"]

USER glassfish
WORKDIR ${PATH_GF_HOME}
ENTRYPOINT ["docker-entrypoint.sh"]

CMD ["startserv"]
21 changes: 21 additions & 0 deletions src/main/resources/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
#!/bin/bash
set -e;

change_passwords () {
local PWD_FILE=/tmp/passwordfile
if [ x${AS_ADMIN_PASSWORD} != x ]; then
echo -e "AS_ADMIN_PASSWORD=admin\nAS_ADMIN_NEWPASSWORD=${AS_ADMIN_PASSWORD}" > $PWD_FILE
asadmin change-admin-password --passwordfile=${PWD_FILE}
rm -rf ${PWD_FILE}
echo "AS_ADMIN_PASSWORD=${AS_ADMIN_PASSWORD}" > "${AS_PASSWORD_FILE}"
fi
if [ x${AS_ADMIN_MASTERPASSWORD} != x ]; then
echo -e "AS_ADMIN_MASTERPASSWORD=changeit\nAS_ADMIN_NEWMASTERPASSWORD=${AS_ADMIN_MASTERPASSWORD}" > ${PWD_FILE}
asadmin change-master-password --passwordfile=${PWD_FILE} --savemasterpassword=true
rm -rf ${PWD_FILE}
fi
}

change_passwords

if [ -f custom/init.sh ]; then
/bin/bash custom/init.sh
fi

if [ "$1" != 'asadmin' -a "$1" != 'startserv' -a "$1" != 'runembedded' ]; then
exec "$@"
fi
Expand Down
45 changes: 45 additions & 0 deletions src/main/resources/docs/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,51 @@ To deploy an application, copy the application into the Docker image or mount th
docker run -p 8080:8080 @docker.glassfish.repository@ runembedded myapp.war
```

## Environment variables

The following environment variables can be set to configure GlassFish Server:

* `AS_ADMIN_MASTERPASSWORD` - to change the default master password
* `AS_ADMIN_PASSWORD` - to change the default admin password

The following environment variables are read-only and can be used derived Docker images or scripts:

* `AS_PASSWORD_FILE` - path to the password file with the admin password. It's applied by default to any asadmin command
* `AS_ADMIN_USER` - name of the admin user. It's applied by default to any asadmin command
* `PATH_GF_HOME` - directory of the GlassFish installation. Also the default working directory
* `PATH_GF_SERVER_LOG` - path to the server log file

## Additional configuration

It's possible to specify custom commands to run before GlassFish server starts. Just create a Bash script `${PATH_GF_HOME}/custom/init.sh` (`/opt/glassfish7/custom/init.sh`), it will be executed before GlassFish server starts.

Within the `init.sh` script, you can run any asadmin command, with `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. 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 -f` option.

For example, to start GlassFish, increase the maximum amount of threads, and then stop it, the `init.sh` script can contain:

```
echo "start-domain
set configs.config.server-config.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size=1000
stop-domain" | asadmin
```

You can provide the script by mounting its directory to the `/opt/glassfish7/custom` directory in the container when running the container:

```
docker run -v ./custom:/opt/glassfish7/custom -p 8080:8080 -ti @docker.glassfish.repository@
```

However, it's recommended running any GlassFish asadmin configuration commands in a custom Docker image, to move the configuration to the image build time instead of runtime, which requires starting and stopping GlassFish, and starting it again.

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:

```
FROM @docker.glassfish.repository@

RUN printf "start-domain \n \
set configs.config.server-config.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size=1000 \n \
stop-domain" | asadmin
```

## Run an application with GlassFish in Docker

Expand Down
Loading