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:

  • Build a Docker image on top of CodeBuild Docker Image
  • Docker for windows always getting stuck on extracting
  • How can I make a HTTP request from one docker container to another linked container?
  • Automating install of Docker and image pull using python or bash
  • Docker Swarm Linking
  • using docker plugin on jenkins -
  • 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?

  • Publishing a port in gitlab-ci-runner using docker
  • How can I setup #Spark/#Kafka on Docker ?
  • How containerd compares to runC
  • mongodump in mongodb container
  • How to set the container name used for --link option with Docker
  • Set PortBindings config for ContainerCreate function in golang sdk for docker api
  • 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
    172.17.45.232   1b6a491653e2
    127.0.0.1       localhost
    ::1     localhost ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    172.17.45.230   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 192.168.100.42
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.