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:

  • How to change docker ip address on centos 7?
  • Set `--net-alias` using Docker API?
  • Connect to Dockerized IIS remotely
  • Docker-Sync issue : Can't have simultaneous environments anymore since 0.4.2
  • Docker - How to Inspect Container for Volume Path?
  • docker unit test setup
  • 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 generate a host unique ID?
  • Postgres in Docker persistent data
  • Run Go app created in windows in Docker container (Ubuntu)
  • Is it possible and safe running Docker containers with Debian on CenstOS host machine?
  • networking options for a docker image in minikube
  • Docker set read timeout for push
  • 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.