Nicolas
Nicolas

Reputation: 31

Cannot access dockerized MySQL instance from another container

When I start MySQL :

docker run --rm -d -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 -v /Docker/data/matos/mysql:/var/lib/mysql mysql:5.7

And start PHPMyAdmin :

docker run --rm -d -e PMA_HOST=172.17.0.1 phpmyadmin/phpmyadmin:latest

PMA cannot connect to the DB server. When I try with PMA_HOST=172.17.0.2 (which is the address assigned to the MySQL container), it works. But :

Am I wrong ?

(I know I can handle this with docker-compose, but prefer managing my containers one by one).

(My MySQL container is successfully telnetable from my laptop with telnet 172.17.0.1 3306).

(My docker version : Docker version 20.10.3, build 48d30b5).

Thanks for your help.

Upvotes: 0

Views: 345

Answers (3)

Nicolas
Nicolas

Reputation: 31

Just found out the problem.

My ufw was active on my laptop, and did not allow explicitly port 3306.

I managed to communicate between PMA container and MySQL, using 172.17.0.1, either by disabling ufw or adding a rule to explicitly accept port 3306.

Thanks @kidustiliksew for your quick reply, and the opportunity you gave me to test user-defined networks.

Upvotes: 1

kidustiliksew
kidustiliksew

Reputation: 463

Create a new docker network and start both containers with the network

docker network create my-network

docker run --rm -d --network my-network -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 -v /Docker/data/matos/mysql:/var/lib/mysql --name mysql mysql:5.7

docker run --rm -d --network my-network -e PMA_HOST=mysql phpmyadmin/phpmyadmin:latest

Notice in the command that I've given the mysql container a name 'mysql' and used it as the address for phpmyadmin

Upvotes: 2

George Poliovei
George Poliovei

Reputation: 1129

maybe it's a good idea to use docker-compose. Create a docker-compose.yml file and inside declare two services, one web and the other db, then you can reference them through their service names (web, db)

ex: PMA_HOST=db

Upvotes: 1

Related Questions