0

I started a container for mysql-server via

docker run --name mysql_server -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql

Then I looked up the ip-adress of this container via

docker inspect mysql_server

Afterwards a started a further container to act as mysqlclient (to connect to server) via

docker run -it mysql mysql -h172.17.0.3 -uroot -p

This works fine. But looking up the ip-address every time after starting the mysql-server container is not what i want. So I tried the approach from the docker hub description (https://hub.docker.com/_/mysql) which says that I can pass the mysql-server container name to the -h flag. So I tried the following:

docker run -it mysql mysql -hmysql_server -uroot -p

But the I'm getting the following error

ERROR 2005 (HY000): Unknown MySQL server host 'mysql_server' (-2)

What is a best-practice approach to connect from one 'client' container to a 'server' container? Do I have to specify a fix ip-address when starting the server-container or is there a better generic way

  • 1
    `docker network create` a network, the `docker run --net` both containers on the same network, and the `docker run --name mysql_server` will be usable as a host name. (You should never need the `docker inspect` IP address.) – David Maze Jun 02 '23 at 13:48
  • Thank you. Thats working. One thing I don't understand is that i thought that both container runs in the same network by default. But that was obviously a wrong interpretation from my side – Bernhard Eischer Jun 02 '23 at 16:04
  • The first-generation Docker networking used a very different setup, and that's still used by default if you `docker run` a container without a `--net` option. Of note Docker Compose creates a `default` network per project and you do not need to specify network options there. – David Maze Jun 05 '23 at 11:14

0 Answers0