no

How to 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/d...

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:

  • You need to create the folder specified in the volumes. It will copy the files from docker container to your local machine. For docker to do that, you need to share your drive (Windows).
  • Don't use the same local port for different images.
  • Notice that we use a single database for Keycloak and the Web app/Wildfly, while we can use another image that allows multiple databases, it's easier this way. The downside is all the tables will be created on a single database for both Keycloak and our Web app.
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="czetsuya@gmail.com"
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:

  • Download PostgreSQL driver
  • Setup data source
  • Download the war file in Wildfly's deployment directory
  • Create a default admin user
  • Note that for this particular example, we are using the standalone.xml WF configuration
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:
I accept customization job for a minimum fee of $50, so don't hesitate to contact me when your lazy to try something :-)

Related

docker 4370778998061661532

Post a Comment Default Comments

item