To setup a docker stack with Glassfish/Payara Server and a PostgreSQL Database Server is really easy, as the Payara project provides a good designed docker container. In the following short tutorial I will explain how to setup a simple development environment with a Glassfish/Payara server and a PostgreSQL Database.
Create a Docker file
First step is to create a custom docker file in your workspace. The goal of the custom container is to provide a payara server with the postgres JDBC driver and also a pre-configured domain.xml file.
So first create a working directory for your project. Into your working directory copy the latest version of the official jdbc driver for postgres. Now you can create a custom docker file ‘Docker’ like this:
FROM payara/server-full:173 # Setup configuration USER payara COPY postgresql-9.4.1212.jar /opt/payara41/glassfish/domains/domain1/lib # COPY domain.xml /opt/payara41/glassfish/domains/domain1/config
This docker container derives form the office payara container with payra version 4.1. With the ‘COPY’ instruction we copy the jdbc driver into the payara lib directory. The second ‘COPY’ instruction in this example is comment because we have not yet a domain.xml file.
Now build the docker image:
docker build --tag=my-payara-project .
Docker-Compose
Now as you have a docker image with your custom payara server you can setup a docker-compose file to define your environment. Create a docker-compose.yml file like this:
version: "3.1" services: db: image: postgres:9.6.1 environment: POSTGRES_PASSWORD: adminadmin POSTGRES_DB: test my-payara-project: image: my-payara-project environment: ports: - "8080:8080" - "4848:4848" - "8181:8181" volumes: - ~/my-payara-project/deployments:/opt/payara41/deployments
In this example we use the postgres docker image version 9.6.1 and create a empty database named ‘test’. The second service definition is our custom payara container which we created before.
The volume: definition for the payara server is useful to map the deployments directory from payara outside into your workspace. This means you can later easily deploy applications into this directory.
To start the environment run:
docker-compose up
That’s it. Now you can log into the payara web console: http://localhost:4848
The default account is ‘admin’ with password ‘admin’
Creating a JDBC connection pool
From within the payra administration web console you can now create a JDBC connection pool:
- select ‘Resources/JDBC/JDBC Connection Pools’
- create a new pool with the following settings:
Pool Name=test
Resource Type=javax.sql.DataSource
Database Driver Vendor=Postgresql - On the next you can enter additional properties provided by the postgres JDBC diver
URL=jdbc:postgresql://db/test
User=root
Password=adminadmin
ServerName=db - Finish the setup
You can test your connection with the ‘Ping’ function provided by teh JDBC Connection Pool configuration.
Note: The host name for the postgres server in this example is ‘db’. This is the service name of the postgres service as we defined it in our docker-compose file.
Provide a domain.xml configuration file
To avoid the need to setup the jdbc connection each time you build your container, you can now copy the domain.xml file from the running container into your project folder.
To get into the running payara container run:
docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 735bea222e31 my-payara-project "/bin/sh -c '${PAYAR…" 25 minutes ago Up 25 minutes 0.0.0.0:4848->4848/tcp, 0.0.0.0:8080->8080/tcp, 0.0.0.0:8181->8181/tcp, 8009/tcp db 43554b7c49dd postgres:9.6.1 "/docker-entrypoint.…" About an hour ago Up 25 minutes 5432/tcp my-payaraproject-postgres_1 docker exec -it 735bea222e31 bash payara@732bea144e31:~$cat glassfish/domains/domain1/config/domain.xml
The ‘docker exec’ comand opens a bash from your running container (you need to copy the correct container ID which you can ask by ‘docker ps’. The cat command shows you the content from the current configuration. Copy the content now into a file ‘domain.xml’ in your workspace.
Now you can uncomment the 2nd COPY instruction in your Docker file
COPY domain.xml /opt/payara41/glassfish/domains/domain1/config
This will copy the domain.xml with the JDBC connection pool from your workspace into your docker image. Rebuild the image once again and restart your docker stack:
docker build --tag=my-payara-project . ..... Step 4/4 : COPY domain.xml /opt/payara41/glassfish/domains/domain1/config ---> Using cache ---> 640cfda9f653 Successfully built 640cfda9f653 Successfully tagged my-payara-project:latest docker-compose up
Now you can easily change the configuration of payara directly from your workspace in the domain.xml file.