mysql docker connection error: Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’

I started docker mysql container and try to connect it outside docker using mysql command but got error:

It's because mysql clinet will use unix socket protocol as default protocol value to connect to mysql server if not specifying protocol or host argument, but mysql docker container is listening on tcp socket.

Solution

Specify IP address as host argument value in mysql command will make mysql to connect using TCP protocol instead of default unix socket.
(Note that using localhost as host argument value will still using unix socket protocol)

Or use

This command will specify protocol argument explicitly