40

I have setup docker container with mysql that expose 3306. I've specified database user, database password and create a test db and give the privileges to new user.
In another container i want to accesso to this db.
So i set up new container with a simply php script that create new table in this db.
I know that mysql container's ip is 172.17.0.2 so :

$mysqli = new mysqli("172.17.0.2", "mattia", "prova", "prova");

Than using mysqli i create new table and all works fine.
But i think that connect to container using his ip address is not good.
Is there another way to specify db host? I tryed with the hostname of the mysql container but it doens't work.

Smita Ahinave
  • 1,901
  • 7
  • 23
  • 42
Isky
  • 1,328
  • 2
  • 14
  • 33

5 Answers5

60

The --link flag is considered a legacy feature, you should use user-defined networks.

You can run both containers on the same network:

docker run -d --name php_container --network my_network my_php_image

docker run -d --name mysql_container --network my_network my_mysql_image

Every container on that network will be able to communicate with each other using the container name as hostname.

Tiago C
  • 776
  • 1
  • 6
  • 3
  • i had creat a new network as $ docker network create -d bridge mynetwork and run my app container and mysql container on this network i try connect my node app to mysql container by container IP and port but its not connect eventhough my database is running in workbench fine – Basit Feb 27 '19 at 08:49
  • What if you have several containers and each one should connect to its own mysql container? F.I: bamboo-agent1 -->mysql1 , bamboo-agent2 -->mysql2 – DimiDak Jun 18 '19 at 10:01
  • @DimiDak just have them on two different networks. One pair (client+db) on one network, and the second pair on the other. You can create multiple networks for different projects. – Tiago C Feb 08 '20 at 16:58
  • works, just tested in 2021 with mysql 5.7 and laravel 7 – user14850280 Jul 22 '21 at 20:47
  • i migrated laravel database successfully, changed DB_HOST=mysql_container – user14850280 Jul 22 '21 at 20:48
30

You need to link your docker containers together with --link flag in docker run command or using link feature in docker-compose. For instance:

docker run -d -name app-container-name --link mysql-container-name app-image-name

In this way docker will add the IP address of the mysql container into /etc/hosts file of your application container. For a complete document refer to: MySQL Docker Containers: Understanding the basics

sara
  • 1,130
  • 2
  • 11
  • 20
  • 3
    Links are now a [legacy option](https://docs.docker.com/compose/compose-file/compose-file-v2/#links), and networks should be used instead. – Aaron Bell Oct 23 '20 at 12:28
4

If you are using docker-compose, than the database will be accessible under the service name.

version: "3.9"
services:
  web:
    build: .
    ports:
      - "8000:8000"
  db:
    image: postgres
    ports:
      - "8001:5432"

Then the database is accessible using: postgres://db:5432. Here the service name is at the same time the hostname in the internal network.

Quote from docker docs:

When you run docker-compose up, the following happens:

  1. A network called myapp_default is created.
  2. A container is created using web’s configuration. It joins the network myapp_default under the name web.
  3. A container is created using db’s configuration. It joins the network myapp_default under the name db.

Source: https://docs.docker.com/compose/networking/

Bojan Hrnkas
  • 1,587
  • 16
  • 22
3

In your docker-compose.yml file add a link property to your webserver service: https://docs.docker.com/compose/networking/#links

Then in your query string, the host parameter's value is your database service name:

$mysqli = new mysqli("database", "mattia", "prova", "prova");
TvC
  • 169
  • 2
0
environment:
  - MYSQL_ROOT_PASSWORD=password
  - MYSQL_DATABASE=dbname
  - MYSQL_ROOT_HOST=%

--

MYSQL_ROOT_HOST=%

Setting root host to % in docker-compose.yml file solves this issue.

MYSQL_ROOT_HOST: By default, MySQL creates the 'root'@'localhost' account. This account can only be connected to from inside the container as described in Connecting to MySQL Server from within the Container. To allow root connections from other hosts, set this environment variable. For example, the value 172.17.0.1, which is the default Docker gateway IP, allows connections from the host machine that runs the container. The option accepts only one entry, but wildcards are allowed (for example, MYSQL_ROOT_HOST=172...* or MYSQL_ROOT_HOST=%).

source: https://dev.mysql.com/doc/refman/8.0/en/docker-mysql-more-topics.html#docker_var_mysql-root-host

hakki
  • 6,181
  • 6
  • 62
  • 106