I'd like to be able to connect to my docker container as if MySQL server were installed on my local machine. I test my connection with:
mysql -u root -proot -h 127.0.0.1 -P 3306 --protocol=tcp
If I create a container using docker, I can successfully do it. Like this:
docker run --name some-mysql-standalone -p 127.0.0.1:3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7.29
If I use a container as a service in docker-compose, I get an error:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 2
MySQL server is running inside the container and I can access it.
My docker compose snippet:
version: '2'
services:
mysql:
image: mysql:5.7.29
container_name: some_mysql
restart: unless-stopped
volumes:
- ./mysql/data:/var/lib/mysql
- ./mysql/init:/docker-entrypoint-initdb.d
ports:
- 3306:3306
environment:
MYSQL_DATABASE: some_mysql
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
The interesting part is that I've been using this docker-compose.yml
for some time now without any issues. I'm not really sure what's changed about my environment that caused it to stop working.
How can I make my mysql container in docker-compose accessible from the host machine?