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 :-)

COMMENTS

mas template
Name

amazon,1,angular,8,bigdata,2,business,1,course-spring,27,courses,6,database,4,docker,3,java,50,kafka,1,keycloak,4,microservices,5,mysql,1,neworking,1,nosql,2,php,1,pinned,2,react,3,server management,7,shared drive,1,spring,7,synology,1,troubleshooting,2,web,1,wordpress,1,
ltr
item
toztech: Run Wildfly and Postgresql in Docker
Run Wildfly and Postgresql in Docker
toztech
https://toztech.blogspot.com/2018/07/run-wildfly-and-postgresql-in-docker.html
https://toztech.blogspot.com/
https://toztech.blogspot.com/
https://toztech.blogspot.com/2018/07/run-wildfly-and-postgresql-in-docker.html
true
2554149350007112447
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content