Cannot connect to DB from a process inside Docker container ? This could fix it.

I had recently hit a blocker to production deployment, when my processes just won’t connect to my Cassandra DB from within their containers.

This is how I could fix it –

  • Start by checking if the target port is open. For eg, if it is the Cassandra DB that the processes cannot access, make sure that its host port 9042 is not blocked by any firewall rules (or security group settings in case of AWS)
  • From the host machine where your docker’ed process has a connectivity issue, use telnet or netcat to check if you can access the target machine and port.

[bash] nc -vz dbhost 9042 [/bash]

  • Once you verify that you can access the port of the service you want to access, from the host machine where your docker process is failing, and still the problem persists, do these steps-
  • Verify the IP address of the problem container –
[bash] docker inspect -f ‘{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ your-container-name-or-id 172.32.15.36 [/bash]
  • If the IP address so obtained is found to be conflicting with the IP addresses of your network, provide a new sub-net for the the docker containers within the host as below-
    • vim /etc/docker/daemon.js
    • Add the following lines with the subnet address of your preference
[bash] { "bip": "10.0.0.1/16" } [/bash]
  • Restart docker service and start your containers.
  • Check your connectivity issue is solved.
  • If your application still cannot connect to your target service, lets reset docker by deleting the docker inventories and restarting it-
[bash] sudo service docker stop sudo rm -rf /var/lib/docker sudo service docker start #init your swarm if needed docker swarm init pull your images start your containers [/bash]
  • The above steps usually fixes the faulty bridge network created previously by deleting and recreating it.

Else, as it happens mosltly, you are on your own 🙂

Best of luck for your resolution, and please comment if you have have a different solution to the problem.

Kj
Kj [/.] Sreekumar programs computers as a hobby and profession. Into programming from his school days, Sree uses Codemarvels to key in facts and fixes he finds interesting while working on different projects. Some of the articles here give away a few shades of his philosophical leanings too.

Leave a Comment

Your email address will not be published. Required fields are marked *