LMSouq
server-admin Open

Connect to mysql in a docker container from the host

GT
gturri
1 month ago
3 views
Problem Description
**In a nutshell** I want to run mysql in a docker container and connect to it from my host. So far, the best I have achieved is: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) **More details** I'm using the following `Dockerfile`: FROM ubuntu:14.04.3 RUN apt-get update && apt-get install -y mysql-server # Ensure we won't bind to localhost only RUN grep -v bind-address /etc/mysql/my.cnf > temp.txt \ && mv temp.txt /etc/mysql/my.cnf # It doesn't seem needed since I'll use -p, but it can't hurt EXPOSE 3306 CMD /etc/init.d/mysql start && tail -F /var/log/mysql.log In the directory where there is this file, I can succesfully build the image and run it with: > docker build -t my-image . > docker run -d -p 12345:3306 my-image When I attach to the image, it seems to work just fine: # from the host > docker exec -it <my_image_name> bash #inside of the container now $ mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. [...] However I don't have that much success from the host: > mysql -P 12345 -uroot ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) **Even more details** - I've seen that there's a question which [looks like mine][1]. However, it isn't the same (and it doesn't have any answers anyway) - I've seen that there are images [dedicated to mysql][2], but I didn't have more success with them - My `grep -v` may feel weird. Admittedly, there may be cleaner way to do it. But when I attach my image, I can observe it actually worked as expected (ie: removed the `bind-address`). And I can see in the container `/var/log/mysql/error.log`: > Server hostname (bind-address): '0.0.0.0'; port: 3306 - '0.0.0.0' resolves to '0.0.0.0'; Server socket created on IP: '0.0.0.0'. [1]: https://stackoverflow.com/q/25069860/1796345 [2]: https://github.com/sameersbn/docker-mysql

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base