How to setup docker to talk to non-dockerized service on same host

I’m trying to set up a docker container (call it docker-client) that will access a MySQL server on mysql-server:3306. mysql-server is the same host that docker-client is on.

I thought I could do this using links and the ambassador pattern, so I tried this:

  • kubernetes on ubuntu 16.04
  • Build .NET Core Console Application into Docker
  • Replacing a particular instance on AWS Elastic BeanStalk?
  • docker api, attaching container returns extra characters
  • How to map the IP address of docker container to another container dynamically
  • Deploying a minimal flask app in docker - server connection issues
  • mysql-server$ docker run -d --name mysql_ambassador -e MYSQL_PORT_3306_TCP=tcp://mysql-server:3306 svendowideit/ambassador

    Then I launched my client like this:

    docker run --link mysql_ambassador:mysql-server -i -t ubuntu /bin/bash

    Unfortunately, trying to run mysql on the client gives an error:

    docker-client# mysql -h mysql-server
    ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0

    Is there any way for the client to stay dockerized, the server to stay non-dockerized, and the MySQL server to continue running on the standard port 3306?

  • How to specify an iterator in the volume path when using docker-compose to scale up service?
  • Docker CLI env var value override not respected by container on startup
  • Confluent Platform : Update Schema Registry to use Avro 1.8.1 and use new build in docker and host
  • docker-compose error what is this?
  • Pod cannot get IP in PodCIDR, get the docker ip
  • How to edit files in a Confluence docker container? There is no vim and apt-get in it
  • 2 Solutions collect form web for “How to setup docker to talk to non-dockerized service on same host”

    If you need to use hostname there are several options:

    1) use dns with docker ips. Just run with –dns option.

    2) you can use names and alias:
    first run container with name
    docker run -i -t --name test1 test_docker /bin/bash
    than run next container with –link option
    run -i -t --link=test1:test_hostname test_docker /bin/bash
    after that name will be assosiated with proper ip:

    `root@1b6a491653e2:/# cat /etc/hosts   1b6a491653e2       localhost
    ::1     localhost ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters   test_hostname`

    Try using the IP of the host in the environment variable MYSQL_PORT_3306_TCP when starting the ambassador container, instead of the hostname mysql-server.

    However, if you just want to connect to a service on the same host that the docker container is running on, you can do so by using the IP of the host (no need for ambassador), e g

    docker-client# mysql -h
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.