How to connect to a MySQL Docker Container via SSH? How to connect to a MySQL Docker Container via SSH? docker docker

How to connect to a MySQL Docker Container via SSH?


One simple way is to bind the MySQL port only to the localhost address. That assumes the host has a mysql client available outside of Docker.

ports:  - 127.0.0.1:3306:3306

You could also omit the ports section completely (no port binding at all), and use the mysql client that's already inside the container.

docker-compose exec mysql bash

Then run the mysql command inside the container to do whatever queries you want to do.


An easy way to achieve this is to forward the ssh port of the docker conatiner to some port on your host, i.e.

ports:   - 22:<some free host port>

and then access the container via ssh to the host port you used. Note, that it is a bad idea to use port 22, since that will cause a conflict with the ssh server running on your host.