Ephreal
Ephreal

Reputation: 2067

How to get PHPMyAdmin to see MySQL in two different docker instance

I am trying to start a MySQL docker instance, and want to interface to this server with PHPMyAdmin.

My server host name from where docker is running is <ServerName>

I am using the following command to start my MySQL docker container

docker run -P --name mysql-test -v storage-test:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=MyDataBase -e MYSQL_USER=me -e MYSQL_PASSWORD=mepass -d mysql:latest

And it seems to start and create the database correctly after that i start PHPmyAdmin container

docker run --name myadmin -d -e MYSQL_ROOT_PASSWORD=root -e PMA_HOST=ServerName -e PMA_VERBOSE=MyDataBase -e PMA_USER=me -e PMA_PASSWORD=mepass  -p 8080:80 phpmyadmin/phpmyadmin

I get this log from mySql container

MySQL init process done. Ready for start up.

2019-10-01T11:36:35.909758Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2019-10-01T11:36:35.909856Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.17) starting as process 1
2019-10-01T11:36:37.735204Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-10-01T11:36:37.761004Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2019-10-01T11:36:37.776949Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.17'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
2019-10-01T11:36:37.887863Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060

However when I access the PHPMyAdmin weblogin I get the following Error:

If I remove the -e PMA_USER=me -e PMA_PASSWORD=mepass from the run command I get to the login where I can enter my credentials however when trying to do that I just get the

Can anyone see what I'm doing wrong here ?

Here is a link to the two docker containers I am using

https://hub.docker.com/_/mysql

https://hub.docker.com/r/phpmyadmin/phpmyadmin/

Regards

Upvotes: 0

Views: 2065

Answers (1)

thaJeztah
thaJeztah

Reputation: 29007

First of all, don't use -P when running your mysql container. -P (or --publish-all) will publish the ports of the MySQL container, which means MySQL will be accessible publicly. This is likely not what you want.

Containers can connect with each-other using the docker container-container network; if both containers are connected to the same network, they can connect with each-other over that network, without having to make the ports publicly accessible. Only use -p (or -P) to make ports accessible that should be public.

For example;

1. Create a custom network;

docker network create myprivatenetwork

2. Start the mysql container

Start the mysql container, and connect it to the myprivatenetwork. I removed the -P option, which means the container is not publicly accessible, but it is accessible from network(s) it's connected to. (I wrapped the commands to make it easier to read)

docker run \
  --name mysql-test \
  --network myprivatenetwork \
  -v storage-test:/var/lib/mysql \
  -e MYSQL_ROOT_PASSWORD=root \
  -e MYSQL_DATABASE=MyDataBase \
  -e MYSQL_USER=me \
  -e MYSQL_PASSWORD=mepass \
  -d \
  mysql:5.7

Note: I used mysql:5.7, because mysql:latest (which currently is MySQL 8) requires some additional configuration; see PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client In general, it's recommended to be specific about the version you want to run, and don't run :latest, as it may change to newer versions, which can cause your setup to fail.

3. Start the PhpMyAdmin container

Start the PhpMyAdmin container, and connect it to the same network; phpmyadmin can connect with the mysql container using its name (mysql-test) as hostname, and using the default mysql port (3306). For the phpmyadmin container, I kept -p to allow accessing it publicly (although you may want to have it run with TLS/SSL)

docker run \
  --name myadmin \
  --network myprivatenetwork \
  -d \
  -e MYSQL_ROOT_PASSWORD=root \
  -e PMA_HOST=mysql-test \
  -e PMA_PORT=3306 \
  -e PMA_VERBOSE=mysql-test \
  -e PMA_USER=me \
  -e PMA_PASSWORD=mepass  \
  -p 8080:80 \
  phpmyadmin/phpmyadmin

4. Visit http://localhost:8080 in your browser

You should now be able to visit PhpMyAdmin in your browser, and see the MySQL database.

Note that PhpMyAdmin is not configured with a password or TLS/SSL, so if your machine is publicly accessible (internet, or your local network), this will allow others to access your database through phpmyadmin

Upvotes: 1

Related Questions