Caleb Caleb - 4 months ago 76
MySQL Question

Connect to Docker MySQL container from localhost?

I have a docker mysql image running, following is what the docker-compose.yml file looks like:

db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: ""
MYSQL_ALLOW_EMPTY_PASSWORD: yes
ports:
- "3306:3306"


This works fine.

My question is: How can I connect to the MySQL instance running on that container from the command line mysql client on my the host (my macbook)?

To clarify:


  • I have a macbook with Docker installed

  • I have a docker container with mysql

  • I want to connect to the mysql instance running on the aforementioned container from the Terminal on my macbook

  • I do NOT want to user a
    docker
    command to make this possible. Rather, I want to use the
    mysql
    client directly from the Terminal (without tunneling in through a docker container).



I don't have MySQL running locally, so port 3306 should be open and ready to use.

The command I am using to start the container is:
docker-compose run

Answer

I got it!! The answer is to use the --service-ports option when running docker-compose:

docker-compose run --service-ports db (the original docker-compose.yml file works fine)

Thanks to all for the help!