Sam Nazarko

Accessing MySQL on the host instance via Docker

I was testing a new application before deploying it on a spare box which has Docker and MySQL installed on the host.

I wanted to leverage the MySQL service running on the host already, rather than containerise it. The MySQL service currently listens on localhost only, and I did not want to expose it globally.

Unfortunately it’s not possible for MySQL to bind to multiple interfaces, but what we can do is pass the socket through to the Docker container, and use it this way.

docker run -v /var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock

This also has the benefit of avoiding TCP overhead.

Posted May 9, 2017

Written by Sam Nazarko, a 20s London chap who is an open source developer by night. Follow me on Twitter