cqlsh is failing when executed from docker container - docker

I'm having trouble initializing a Cassandra database in a docker container.
Here's my Dockerfile :
FROM cassandra
COPY ./createTable.cql /tmp/createTable.cql
RUN cqlsh -f /tmp/createTable.cql
When I do docker build . -t cassandra, I get an error at cqlsh :
Connection error: ('Unable to connect to any servers', {'127.0.0.1': error(111, "Tried connecting to [('127.0.0.1', 9042)]. Last error: Connection refused")})
But the same commands work fine when I do it outside a Docker container.
Why is the connection refused ? If I'm doing it wrong, how can I initialize my
database ?
Thanks for any help or advice.

The RUN is executed when you build the container, not when the container executes.
You need to write custom script that will start Cassandra (same as in original container - look into Cassandra's Dockerfile), then wait until it starts, and only after that - execute commands via cqlsh.

cqlsh command needs the hostname where the Cassandra is running if the hostname is not provided it will take the localhost (127.0.0.1) as the hostname.
In your case, it's taking localhost as hostname, looks like your Cassandra is running outside of the container because of that reason it is working fine when you run from outside and failing from inside container.
Passing the hostname to cqlsh should resolve your problem when your run it inside container also.
cqlsh <hostname or ip> -f /tmp/createTable.cql

Related

Testcontainers runs tests in container (Jenkins container) could not connect to Ryuk

I have got a container with Jenkins. I am trying to run tests pipeline inside this container. Tests uses Testcontainers. I am struggling with error:
18:36:15.560 [testcontainers-ryuk] WARN org.testcontainers.utility.RyukResourceReaper - Cannot connect to Ryuk at 172.17.0.1:55592
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:412)
at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:255)
at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:237)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.base/java.net.Socket.connect(Socket.java:609)
at org.testcontainers.utility.RyukResourceReaper.lambda$null$0(RyukResourceReaper.java:92)
at org.rnorth.ducttape.ratelimits.RateLimiter.doWhenReady(RateLimiter.java:27)
at org.testcontainers.utility.RyukResourceReaper.lambda$maybeStart$1(RyukResourceReaper.java:88)
at java.base/java.lang.Thread.run(Thread.java:829)
Jenkins container was run by command
docker run -v /var/run/docker.sock:/var/run/docker.sock -v jenkins_home:/var/jenkins_home -p 8080:8080 -p 5000:5000 -d --restart always <img>
I've tried to run Jenkins container with --network="host" but same effect.
When I am running those tests directly on my local machine by mvn clean verify, tests passed and this error didn't occur.
EDIT:
I've changed network to bridge (--network="bridge") for Jenkins container. In test I used explicite network bridge:
new GenericContainer("mongodb:1..").withNetworkMode("bridge")...
Ryuk container is created (I can see three containers on my host docker - jenkins, mongo, ryuk) and when I run docker network inspect bridge I can see three containerers are attatched to it. But when I am trying connect to Ryuk container using gateway (172.17.0.1) from Jenkins container using telenet it is NOT possible. But when I am using Ryuk IP (172.17.0.2) instead of gateway's IP I can connect. Then I've create other container on my host and tryied to connect to it form Jenkins container via gateway's IP - could do it.
SOLUTION
Just run Jenkins container with -v /var/run/docker.sock.raw:/var/run/docker.sock instead of -v /var/run/docker.sock:/var/run/docker.sock.
Thanks #Kevin Wittek!
As mentioned in one of the comments, this originates from an issue in Testcontainers for Java regarding an upstream change in Docker Desktop (see this issue).
The current workaround for Mac is to mount the raw socket when creating the container:
-v /var/run/docker.sock.raw:/var/run/docker.sock

failed to initialize database, got error dial tcp 127.0.0.1:3306: connect: connection refused Problem

im learning docker and go now
but i got the problem when i docker run with this
docker run --rm -p 8080:8080/tcp --env-file .env my-project:latest
here are some of my .env code. i use docker desktop on windows, is it not possible to run docker on localhost in windows?
DB_HOST=127.0.0.1
DB_USERNAME=root
DB_NAME=mydbs
DB_PASS=root123
AUTH_GEN_URL=https://api.learning.mydbs.id
anyone have a clue? any answer would be appreciated
thank youu
The problem is that when you spin up the container it tries to connect to 127.0.0.1:3306 within the container and not the host, hence you are getting the error as connection refused since nothing is running on port 3306 at localhost in your container.
For Windows and Mac this can easily be fixed by using host.docker.internal instead of 127.0.0.1. This ensures that the service running inside your container correctly connects to the MySQL instance running on the host machine.
For Linux it's even more simple as all you have to do is pass --network="host" option to the docker run command

Unable to connect to localhost of machine from docker container

My GCP ES service is port forwarded to localhost:9200 of my machine. I am able to log ingest from the local machine.
I have created JAVA script to do the same task and It is also running fine when I am running local
Now, I have created docker Image of that Java project using dockerFile and when I am creating a container, then I am getting the following error
Connect to localhost:9200 [localhost/127.0.0.1] failed: Connection refused (Connection refused)
Docker command :
docker run -it --net="host" gcplogingest:latest mvn -f BEATLe/pom.xml test -Dcomponent=detect -DtestSuite=CommonXML/Detect_loginjectionGCP
Note :
I have tried using --network="host" but I am still getting the same error.
Please suggest.
Have you tried removing --net=host and then use in your docker host.docker.internal instead 127.0.0.1?
Consider that your host localhost should be different than your docker localhost, and once you get that removing network=host, you need to access from docker to
host.docker.internal:9200

Not Able to Connect to MariaDB Docker Container using IntelliJ

I have created a Docker container using the following command:
docker run --network host --name mariadb -e MYSQL_ROOT_PASSWORD=testpass -d mariadb:latest
I'm trying to connect to the MariaDB Docker container in my IntelliJ project and getting the following error:
The specified database user/password combination is rejected:
[28000][1045] Access denied for user 'root'#'127.0.0.1' (using password: YES)
When I run without --network host and use container's IP 172.18.0.2, I get the following error:
[08][-1] Could not connect to address=(host=172.18.0.2)(port=3306)(type=master) : connect timed out
java.net.SocketTimeoutException: connect timed out.
I even tried updating bind-address in my.cnf file in the container but no success.
Please help me.
Maybe this can help you out sir?
https://intellij-support.jetbrains.com/hc/en-us/community/posts/360000021659-The-specified-database-user-password-combination-is-rejected

How to connect to Cassandra within a docker container in order to view on DBVisualize on host machine?

I currently have a Docker container with Cassandra running within. I am trying to get a visual view using DBVisualizer of said Cassandra. I am using DataStax's Cassandra JDBC driver.
I run my container like so:
docker run --rm -it --net=host -p 9042:9042 [docker image from repo]
The container has an ip of 10.0.72.3
Outside of this container, I am trying to access this cassandra by connecting to:
jdbc:cassandra:Database=Test Cluster;Port=9042;Server=10.0.72.3;
However, I keep getting:
The connection failed: No Cluster node is available..
I have attempted to change cassandra.yaml and change ports, but I am still stuck. Any assistance is appreciated. Thank you

Resources