Docker MySQL Connectivity in MacBook

Purpose of this post is to share my experiences in connecting to a MySQL server running in a Docker container (MacBook Pro)

Issue: The issue is, in MacBook Pro, you cannot directly connect to the MySQL server started through docker (below steps). 

Solution: You need to find the IP Address of your ethernet or network adapter and using that IP Address you should connect

Environment setup:

MacBook Air
Docker Desktop (2.1.0.3 version) + Docker Compose
MySQL 5.7 image

Commands Executed:

Step 1: Develop a docker-compose.yml file
version: '3'

services:

  mysql5:
    container_name: poc_mysql
    image: mysql:5.7
    ports:
      - '3306:3306'
    volumes: 
      - ./mysql-data-volume:/var/lib/mysql
      - ./mysql_startup_scripts:/docker-entrypoint-initdb.d
    environment:
      MYSQL_ROOT_PASSWORD: 'password'
      MYSQL_USER: 'poc_user'
      MYSQL_PASS: 'poc_password'
Step 2: Run the docker-compose.yml file

docker-compose up

For the above command, you will see below output

Step 3: Connect to MySQL running Docker container

From MacBook Air command line when you try to connect to the above MySQL container it will not work and you will see MySQL command is hanging (i.e. mysql -u root -p ) hangs (even if you give -h argument still it fails)

By running “docker ps“, obtain the CONTAINER ID of the above MySQL running container and type the command “docker logs <CONTAINER_ID>“, everything looks fine but, still you will not be able to connect to the MySQL

Step 4: Solution

(A) Obtain the IP Address of your Macbook “en0” interface or whatever network interface you are connected to. In my case, it is “en0” and the IP Address is 192.168.1.16

(B) Now connect using the above network interface IP Address. It works!!!

mysql -u root -h 192.168.1.16 -p

Hope this helps!!!