1

I'm having difficulty using liquibase at build time to create my schema and seed the database in an image that extends the mysql 5.7 base image. Specifically, I'm receiving a timeout because I can't connect to my database within the same singular image.

The last line in the Dockerfile below will not execute. I get the error:

Starting Liquibase at Thu, 16 May 2019 22:09:50 UTC (version 3.6.3 built at 2019-01-29 11:34:48)
Unexpected error running Liquibase: com.mysql.cj.jdbc.exceptions.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.
liquibase.exception.DatabaseException: liquibase.exception.DatabaseException: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

...

Caused by: java.net.ConnectException: Connection refused (Connection refused)
        at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
        at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
        at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
        at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
        at java.base/java.net.Socket.connect(Socket.java:591)
        at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
        ... 12 common frames omitted
FROM mysql:5.7

ENV MYSQL_DATABASE dbname

# Install wget, jdk, liquibase, mysql connector/j
...

RUN liquibase --changeLogFile=/path/to/script/schema.mysql.sql \
        --username=root \
        --password=password \
        --url=jdbc:mysql://localhost:3306/dbname" \
        --driver=com.mysql.cj.jdbc.Driver \
        --classpath=/path/to/liquibase/liquibase.jar:/path/to/mysql-connector/mysql-connector-java-8.0.16.jar \
        --contexts="Initialization" \
        update

I'd expect for the sql script to execute to create my database schema.

Quinn Vissak
  • 131
  • 1
  • 8

1 Answers1

1

I figured out that mysql isn't actually running yet at build time. I added this line as the last line of the dockerfile and commented out the bit that wasn't working.

Step 14/14 : RUN /etc/init.d/mysql status
 ---> Running in 7701598d1eff
MySQL Community Server 5.7.25 is not running.

Docker won't execute the entrypoint of the base image (which actually initializes the database) until the very end of the build (unless I define my own entrypoint). Source: What happens to entrypoint of Docker parent image when child defines another one?

So one option would be to define my own entrypoint and copy the stuff from the base image. Probably not best practice...

Alternatively, I can use an intermediate container to seed the database after this one comes up with docker compose.

Quinn Vissak
  • 131
  • 1
  • 8