Run Wildfly and Postgresql in Docker
Tech-Today

Run Wildfly and Postgresql in Docker


Docker is a great tool to simulate a development environment. Not only that but it also makes that environment portable by having a docker/docker-compose configuration file. And that is what this blog is all about.

We will launch a pre-configured docker environment. This environment is your typical web-app with database access. In addition to that, we will also include an image for logging, database, and keycloak in case we need an authentication server.

Let's go through the configuration files and start from the main docker compose config. This configuration is responsible for building and starting all our images in the correct order.

version: '3'
services:
postgres:
image: postgres:10
container_name: postgres
ports:
- "5432:5432"
environment:
- LC_ALL=C.UTF-8
- POSTGRES_DB=terawhars
- POSTGRES_USER=terawhars
- POSTGRES_PASSWORD=terawhars
- POSTGRES_PORT=5432
volumes:
- $PWD/input_files/import-postgres.sql:/docker-entrypoint-initdb.d/import-postgres.sql
- $PWD/output_files/postgres_data:/var/lib/postgresql/data
adminer:
image: adminer
container_name: adminer
depends_on:
- postgres
ports:
- 8081:8080
wildfly:
image: terawhars
container_name: wildfly
build: .
ports:
- "8080:8080"
- "9990:9990"
environment:
- DB_HOST=postgres
- DB_PORT=5432
- DB_NAME=terawhars
- DB_USER=terawhars
- DB_PASS=terawhars
- DS_NAME=TeraWHARSDS
- JNDI_NAME=java:jboss/datasources/TeraWHARSDS
depends_on:
- postgres
volumes:
- $PWD/output_files/logs:/opt/jboss/wildfly/standalone/log
- $PWD/output_files/terawharsdata:/opt/jboss/wildfly/terawharsdata
- jboss-conf:/opt/jboss/wildfly/standalone/configuration
keycloak:
image: jboss/keycloak:4.0.0.Final
container_name: keycloak
ports:
- "8083:8080"
environment:
- DB_VENDOR=POSTGRES
- DB_ADDR=postgres
- DB_DATABASE=terawhars
- DB_USER=terawhars
- DB_PASSWORD=terawhars
- KEYCLOAK_USER=admin
- KEYCLOAK_PASSWORD=admin
depends_on:
- postgres
weblogs:
image: opencell/alpine-tailon
container_name: tailon
depends_on:
- wildfly
ports:
- 8082:8080
volumes:
- $PWD/output_files/logs:/logs/
volumes:
jboss-conf: {}
This configuration installs Postgresql, Adminer, Tailon, Wildfly, Keycloak. For a more detailed instruction on what each field means, please consult the Docker documentation, we're not here to teach that.

Things to take note of:

Most of the image we use only requires minimal configuration like in Postgres, we only need to define the database configuration. It could become more complicated if we need to do replication, etc. But I think our example is enough for this tutorial. Now the image that requires some more fiddling is our web app since we need to configure the data source and download the war file.
FROM jboss/wildfly:13.0.0.Final

LABEL com.terawhars.version="0.0.1-snapshot"
LABEL author="Edward P. Legaspi"
LABEL email="[email protected]"
LABEL vendor1="TeraWHARS"
LABEL com.terawhars.release-date="2018-07-24"

# Set Postgresql env variables
ENV DB_HOST postgres
ENV DB_PORT 5432
ENV DB_NAME terawhars
ENV DB_USER terawhars
ENV DB_PASS terawhars

ENV DS_NAME TeraWHARSDS
ENV JNDI_NAME java:jboss/datasources/TeraWHARSDS

USER root

ADD https://jdbc.postgresql.org/download/postgresql-42.2.4.jar /tmp/postgresql-42.2.4.jar

WORKDIR /tmp
COPY input_files/wildfly-command.sh ./
COPY input_files/module-install.cli ./
RUN sed -i -e 's/\r$//' ./wildfly-command.sh
RUN chmod +x ./wildfly-command.sh
RUN ./wildfly-command.sh \
&& rm -rf $JBOSS_HOME/standalone/configuration/standalone_xml_history/

# Download and deploy the war file
ADD https://github.com/czetsuya/javaee6-docker-web/releases/download/1.0.0/javaee6-webapp.war $JBOSS_HOME/standalone/deployments

# Create Wildfly admin user
RUN $JBOSS_HOME/bin/add-user.sh admin admin --silent

# Set the default command to run on boot
# This will boot WildFly in the standalone mode and bind to all interface
CMD ["/opt/jboss/wildfly/bin/standalone.sh", "-b", "0.0.0.0", "-bmanagement", "0.0.0.0"]

What it does:

Some other things to take note:
  • For PostgreSQL, the image we specified an SQL file that will be imported on startup. This will initialize our database.
  • We use a .env file to define the PWD variable that is not present in Windows.
To see how it works check out the complete code from https://github.com/czetsuya/Docker-Demo and then run:
>docker-compose up --build

It will take some time during the first run as it will download all the images locally first.

References:
  • https://github.com/czetsuya/Docker-Demo
  • https://github.com/czetsuya/javaee6-docker-web
I accept customization job for a minimum fee of $50, so don't hesitate to contact me when your lazy to try something :-)




- Docker Swarm Commands
This tutorial requires connection to the internet as swarm creation and management needs Docker Hub. Requirements WindowsDocker CEVirtualBox*Needless to say, a docker for windows must be running. docker run swarm create --Download the swarm image locally....

- Wildfly Server Provisioning Elastic Search Integration
I'm interested and would like to evaluate the integration of Elasticsearch to hibernate-search. I'm using the Wildfly container, however, Wildfly's hibernate-search library is a bit outdated: 5.5.8. so I need to find a way to outdate the jars...

- How To Setup Arquillian Testing With Wildfly
This tutorial requires: Knowledge with GITKnowledge with archetypeRequirements:WildflyeclipseWhat to do:In eclipse create a new maven project: File->New->Other, enter maven in the filter. Select Maven Project. Click next, then next. In the filter...

- Change Postgresql's Postgres Password
This is how you would change your postgresql's user postgres password, in case you forgot it. This is done in ubuntu 12.04. 1.) Logon to your postgres account in behalf of root: >sudo -u postgres psql template1 //enter your password Now you're...

- How To Setup Postgresql In Ubuntu 11.10 And Perform Dump/restore
This write up will try to explain how to install postgresql and attempt to perform database dump and restore. Which should be a basic functionality but is not present in pgadmin.  1.) Install postgresql (execute in terminal): sudo apt-get install...



Tech-Today








.