Running Liferay on Docker Database
Running Liferay on Docker Database
How to connect Liferay to a database in Docker
Problem
Recently I had to check some functionality for the old 6.2 project and run it locally. And, as it turned out, it was quite tricky just to run it, because I already have MySQL 8 in my environment, and Liferay 6.2 does not support it.
When I tried to run Liferay on my MySQL 8 database, I got some weird exception like this:
java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.
It seemed to be MySQL driver related. Indeed, the old com.mysql.jdbc.Driver driver is not supported by MySQL 8. So, I tried to change it to a new com.mysql.cj.jdbc.Driver one, replace mysql.jar library with a newer one, and adjust the connection properties.
But even after these adjustments there were weird errors that prevented successful start of Liferay 6.2 portal.
Finally, I’ve found a quite simple and straightforward solution for this problem - Docker.
Why Docker?
Docker is pretty fine for the cases like described above: when we need different versions of applications running in the same machine. So, we can install MySQL 5.7 in Docker, and it can run in parallel with the host’s MySQL.
Docker Configuration
One of simplest ways to configure Docker containers is the docker-compose. With that we can specify the source docker image, and the target container name, define environment variables - e.g. for DB connection properties, specify ports mapping - to be able to connect to database from the host machine, and volumes - to persistently store data on the host system. The docker-compose.yml file for MySQL 5.7 can look like this:
version: '3.1'
services:
liferay-mysql:
image: mysql:5.7
container_name: liferay-mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: liferay
MYSQL_DATABASE: liferay62
MYSQL_PASSWORD: liferay
volumes:
- ./data/mysql-liferay62:/var/lib/mysql
ports:
- 3307:3306
Here I mapped the host 3307 port to container’s 3306 one. This way, I can use both MySQL versions in my environment: MySQL 8 installed on my machine using the default 3306 port, and MySQL 5.7 from Docker container - using 3307 port.
Start Docker
After docker-compose.yml configuration MySQL 5.7 can be started using command: docker-compose up -d liferay-mysql
Note: this runs MySQL container in “detached” mode. To monitor logs you can run docker logs -f liferay-mysql command (or run container in “attached” mode, without -d option).
Connect to Docker Database
Once MySQL is running in Docker - you can connect to it using the following properties:
# Database
jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.url=jdbc:mysql://localhost:3307/liferay62?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.default.username=liferay
jdbc.default.password=liferay
Note: the port 3307 here is the host’s port mapped to container’s 3306 port in docker-compose.yml.
With that, I was able to run Liferay 6.2 without any issues, as it’s compatible with MySQL 5.7 running in Docker.
Enjoy 😏
Comments
Post a Comment