0

I have a MySql container and another docker container with Jython app.

Inside Jython app - this is a connection string to connect to MySql (it works on host): mysql_url_string jdbc:mysql://localhost/...

This does not work with 2 docker containers (1 Mysql, 2 Jython app). What IP address I should use for connection string (instead of localhost)?

Thanks.

Joe
  • 11,983
  • 31
  • 109
  • 183
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Mar 27 '18 at 19:11
  • "What IP address I should use for connection string (instead of localhost)?" The machine IP where MySQL is installed and running? ipconfig on a windows machine or ifconfig on a linux, unix or mac. – Raymond Nijland Mar 27 '18 at 19:12
  • I am running both docker containers on 1 host. Traffic is only local to that host machine (but localhost still did not work). – Joe Mar 27 '18 at 19:13
  • https://stackoverflow.com/questions/41768157/how-to-link-container-in-docker – Paul Mar 27 '18 at 19:13
  • You've presumably given a virtual address to the docker container, use that address. – Barmar Mar 27 '18 at 19:14
  • How should I test this easily/fast? (e.g. connect to App container and ping to IP address of MySql container?) What is the best way to do it? – Joe Mar 27 '18 at 19:32
  • you can use the name of container you need. – Meiram Chuzhenbayev Mar 27 '18 at 21:25
  • In which way? For MySQL URL connection or? I am looking for a way to test connection from within Container A (app) to Container B (MySql)... – Joe Mar 27 '18 at 21:39
  • `docker exec -it "nc -vz 3306"` will tell you if jython container can bind to the mysql port – 264nm Mar 28 '18 at 09:24
  • @jww I'm inclined to agree, but considering the weight of docker on the modern developer workflow these days it's a bit of grey area. – 264nm Mar 28 '18 at 09:26

1 Answers1

0

Instead of using an IP address (as they may change unless you specifically define network configuration), you can simply link the 2 containers together and refer to them by container name.

version: "3"
services:

  mysql:
    container_name: mysql
    image: somethingsomething/mysql:latest

  jython
    container_name: jython
    image: somethingsomething/jython:latest
    links:
      - mysql
    environment:
      jdbc_url: jdbc:mysql://mysql:3306

This linking can also be done via CLI (see: https://linuxconfig.org/basic-example-on-how-to-link-docker-containers)

If you simply must use IP addresses, you can obtain the IP address after linking by checking the /etc/hosts files inside the containers.

Edit Note:

There are alternative ways to approach this without 'linking' but without need more detailed information for how your containers are set up already it's difficult to provide this. i.e. whether they are standalone containers on host network or bridged network, or created as a docker service with an overlay, or something else! The different scenarios change the way addressing is created and used for inter container communication so the means of looking up the IP address won't be the same.

264nm
  • 725
  • 4
  • 13
  • Thanks.. So you put 'links:' tag for both containers? (Mysql and Jython) How are they connected in this case - do they act as 1 big container with these links? – Joe Mar 28 '18 at 02:55
  • You only really need to link it from the client to the server i.e. jython to mysql, but can link both ways for debugging. Docker does a lot of magic on the networking front. A good example is when you deploy a multi container docker service on a swarm cluster (over multiple servers) it automagically creates an overlay network so the containers can talk even if on different hosts. In this case though linking basically it gives one container the information it needs to connect to the other and updates /etc/hosts mapping the container name to its IP address. See https://docs.docker.com/network/ – 264nm Mar 28 '18 at 09:20
  • If I link from both sides - then it won't work.. Only from one side linking seems to work. – cikavladimir Mar 29 '18 at 18:26
  • That's strange, swear I've done both ways before. Hard to tell without seeing more on how you've configured everything, but don't worry about linking both ways if it's impeding you as it's not necessary. You'd just want jython -> mysql. I'll edit the response accordingly. – 264nm Apr 03 '18 at 01:32
  • @Joe have I answered your question? If so please reflect it on my answer, otherwise please let me know what gaps I need to fill in. – 264nm Apr 09 '18 at 06:32