0

I have an app in python that I want to run in a docker container and it has a line:

h2o.connect(ip='127.0.0.1', port='54321')

The h2o server is running in docker container and it always has different ip. One time it was started on 172.19.0.5, the other time 172.19.0.3, sometimes 172.17.0.3. So it is always random, and I can't connect the python app. I tried to expose the port of h2o server to localhost and then connect the python (the code above), but it is not working.

Milan Panic
  • 456
  • 1
  • 8
  • 25
  • Possible duplicate of [How to reach docker containers by name instead of IP address?](https://stackoverflow.com/questions/31149501/how-to-reach-docker-containers-by-name-instead-of-ip-address) – David Maze Jul 04 '18 at 10:58

1 Answers1

0

You dont connect two docker containers though ip addresses. Instead, you want to use docker internal network aliases:

version: '3'
services:
  server:
    ...
    depends_on:
      - database
  database:
    ...
    expose:
      - 54321:54321

then you can define your connectio in server as: h2o.connect(ip='127.0.0.1', port='54321')

Simas Joneliunas
  • 2,705
  • 15
  • 25
  • 32
  • 1
    correction: version: '3' services: server: ... depends_on: - database database: ... expose: - 54321 and h2o.connect(ip='database', port='54321') – cjdcordeiro Jul 04 '18 at 09:02
  • correct me if i am wrong, but i think that `ports:` declaration is sufficient when communicating between two linked docker images. You would want to `expose:` the port only when you want to open the container to external connections? – Simas Joneliunas Jul 04 '18 at 09:09
  • Thank you very much, it worked, I used depends_on: *theThingItDependsOn* and then in my python code I wrote ip=*theThingItDependsOn* – Milan Panic Jul 04 '18 at 10:30
  • But it is programming and now I have a different problem. Now because the h2o server and python are running in the docker, the windows path C:/Desktop/Data/data.csv is not visible, because is somewhere in a container. How do I insert the csv file in the container and specify the path? Do I use volume or is there an easiear solution? – Milan Panic Jul 04 '18 at 10:36
  • you should use a shared volume. that is the easiest solution. You can also insert it when creating the container in `dockerfile` but in that case you would only have a static file that you could only modify by repackaging the container. Alternitively, you can expose the port per @cjdcordeiro advise and create a web api to upload it to the container. This approach would only work while container is alive and the file would be lost unless you implemented volumes. – Simas Joneliunas Jul 04 '18 at 13:19
  • 1
    Simas, it's the opposite. "ports" will publish the port to the host - that's why the format is "dest:source" – cjdcordeiro Jul 05 '18 at 08:40
  • seems I also learnt something new from this question. Thanks for clarifying! – Simas Joneliunas Jul 05 '18 at 08:42