I’m working with liquibase migration tool, it is working fine through terminal (update the table in scylla db).
Now I want to dockerize it, for this I do the following steps:
-
Create liquibase.properties file:
changeLogFile: changelog.sql
url: jdbc:cassandra://localhost:9041/mykeyspace
driver: com.simba.cassandra.jdbc42.Driver
defaultSchemaName: mykeyspace
My scylladb container is running at port 9041 with keyspace ‘mykeyspace’
-
Create changelog.sql file:
--liquibase formatted sql --changeset liquibase:1 CREATE KEYSPACE IF NOT EXISTS studentkeyspace WITH replication = {'class': 'NetworkTopologyStrategy', 'DC1': 3}; --changeset liquibase:2 CREATE TABLE IF NOT EXISTS studentkeyspace.studentData (name text, Id text PRIMARY KEY, email text)
-
Create docker compose file:
version: ‘3’
services:
liquibase:
image: liquibase/liquibase:4.1.1
container_name: liquibasecontainer
volumes:
- ./changelogs:/liquibase/changelogs
- ./liquibase.properties:/liquibase/liquibase.properties
- /home/asif/Liquibase/liquibase-4.1.1/lib:/liquibase/lib
command: ["--defaultsFile=/liquibase/liquibase.properties", "update"]
When I run command ‘docker-compose -f docker-compose.yml up’
It generate an error:
Unexpected error running Liquibase: liquibase.exception.DatabaseException: liquibase.exception.DatabaseException: Connection could not be created to jdbc:cassandra://localhost:9041/mykeyspace with driver com.simba.cassandra.jdbc42.Driver. [Simba][CassandraJDBCDriver](500150) Error setting/closing connection: All host(s) tried for query failed (tried: localhost/127.0.0.1:9041 (com.simba.cassandra.shaded.datastax.driver.core.exceptions.TransportException: [localhost/127.0.0.1:9041] Cannot connect)).
liquibasecontainer | For more information, please use the --logLevel flag
liquibasecontainer exited with code 255
How can I remove this error ?
2
Answers
You are trying to connect two containers, not two services on the host. If you use
localhost
in a container it is referring to inside the container. Inside yourliquibase
container nothing is listening on port 9041.The right way to do this is (when using docker-compose), by referencing the container that runs scylla directly. In your case that is
scylla node
.Now it is also important to note, that when doing that, you need to use the port that scylla is listening on, not the port that you expose to the host. For Scylla DB that is port 9042 as you can read in the documentation.
Your adjusted
properties
part would look like this:As the error states, the driver threw a
TransportException
because there is no connectivity tolocalhost
in the Scylla container, therefore "Cannot connect":By definition,
localhost
is local to the host so it can only be accessed withIN that operating system instance (OSI).To access services from "outside" the container, you need to expose (publish) the port to the outside world using the
--publish
(or-p
) flag.Once you’ve exposed the CQL port, you will need configure the JDBC URL with with the Scylla container’s hostname or IP that is accessible/reachable/routable from the Liquibase container. For details, see Docker Networking overview.
As a side note, the Simba JDBC driver for Apache Cassandra is proprietary software (not open-source) so its use is subject to terms and conditions.
Under the license terms, the Simba JDBC driver can only be used with (1) DataStax software and services such as DataStax Enterprise or (2) Apache Cassandra. It is otherwise a breach of license to use the driver to connect to other software or services including variants which are Cassandra-compatible. Cheers!