Search code examples
mysqldockerdockerfileexpectjahia

Docker : accessing MySQL container during Tomcat image build


I'm trying to create a Docker installation of Jahia CMS (Digital Experience Manager).

I need :

  1. a MySQL container
  2. a Jahia container (embedded Tomcat)

The trick is that during the Jahia container build (product installation using Expect), I need to access the MySQL container (connection check required).

MySQL Dockerfile :

FROM mysql:5.6

Jahia Dockefile :

FROM centos:centos6

# Install dependencies
RUN yum -y update && yum -y install ...

# Download Digital Experience Manager 7.1.1
RUN wget -q https://www.jahia.com/downloads/jahia/digitalexperiencemanager7.1.1/DigitalExperienceManager-EnterpriseDistribution-7.1.1.0-r53717.3663.jar -O /tmp/DigitalExperienceManager.jar

# Download MySQL connector (only needed for standalone db installation)
RUN wget -q http://central.maven.org/maven2/mysql/mysql-connector-java/5.1.44/mysql-connector-java-5.1.44.jar -O /usr/lib/mysql-connector-java-5.1.44.jar

# Launch installation using Expect to automate user input
COPY jahia_conf.exp /tmp/configuration.exp
RUN expect /tmp/configuration.exp

# Start Jahia
/opt/DigitalExperienceManager-EnterpriseDistribution-7.1.1.0/tomcat/bin/catalina.sh jpda run

Expect script (jahia_conf.exp)

#!/bin/sh
#!/usr/bin/expect

spawn java -jar /tmp/DigitalExperienceManager.jar -console

# Installation directory
expect "Select target path"
send "/opt/DigitalExperienceManager-EnterpriseDistribution-7.1.1.0\r"

# MySQL connector JAR file
expect "Specify the path to the downloaded driver JAR file"
send "/usr/lib/mysql-connector-java-5.1.44.jar\r"

# Database configuration
expect "Database URL (*)"
send "jdbc:mysql://mysql:3306/jahia?useUnicode=true&characterEncoding=UTF-8&useServerPrepStmts=false\r"

Of course I get an error during image build because it checks the connection right after database URL input :

An error occurred while establishing the connection to the database com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server..

Indeed I'm just building the Jahia image, so the mysql container is not yet accessible (even if running).

How to deal with this kind of situation (when you need to access another container during build) ?


Solution

  • Try using docker commit. You may have to run the configuration.exp script to set up Jahia by exec'ing into your container. Then use docker commit to save the changes to the file system into a new image. That image should persist the initial configuration.

    Be mindful that volumes are not included in a docker commit, as they live outside Docker's union file system. It doesn't look like you're declaring any volumes in your Dockerfile, so it probably won't be a problem for you. This answer elaborates on docker commit and database volumes, but the premise is the same for any container.