Mar 31, 2018 - I wanted to access host port from a docker container. There're several names: docker.for.mac.localhost.
I have a docker mysql image running, following is what the docker-compose.yml file looks like:
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 themysql
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
Answers:
Since you published port
3306
on your docker host, from that host itself you would connect to 127.0.0.1:3306
. In that case the port mapping section of the
docker-compose.yml
file is ignored. To have the port mapping section considered, you have to add the --service-ports
option: Beware that by default, the mysql client tries to connect using a unix socket when you tell it to connect to
localhost
. So do use 127.0.0.1
and not localhost
:Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.6.26 MySQL Community Server (GPL)
Your MySQL connection id is 1
Server version: 5.6.26 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘h’ for help. Type ‘c’ to clear the current input statement.
mysql>
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)
Answers:
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!
Answers:
Your yml file looks good.
You can directly connect docker container directly as it already mapped with local port 3306.
Just goto terminal and run
Just goto terminal and run
Note: you must have access to mysql command line. If mysql command show an error, check ‘/usr/local/mysql/bin’ other wise you can not connect to mysql server. In other word you must have mysql client on your machine.
Answers:
Connect to MySQL via {host ip}:3306 since you’ve exposed the internal port to your host as 3306. If you need to access the MySQL CLI tools you will need to go
docker exec -it mycontainer bash
this will place you inside the container to access the tools installed with MySQL if you do not have them installed locally on the host o/s.Tags: docker, mysql, sql
If you're running MacOS or Windows you have a few options for installing Docker. There's even a third way too, and we'll compare them here.
Quick Jump: OS and Hardware Requirements|Pros and Cons|Which One Should I Use?
If you’re on MacOS or Windows you can install Docker with:
- Docker for Mac / Windows (now known as Docker Desktop)
- Docker Toolbox
- Running your own Virtual Machine and installing Docker yourself
All 3 of those options have their own pros and cons and in this article we’re going to cover them. If you’re looking for a high level overview of what Docker for Mac / Windows and Docker Toolbox is, then check out this article on getting to know Docker’s ecosystem.
OS and Hardware Requirements
It’s helpful to know what you can install before we compare everything, so let’s do that:
Docker for Mac (Docker Desktop)
Docker for Mac requires that you’re running Yosemite 10.10.3+ or newer but it’s worth mentioning that you should upgrade to 10.11+ because 10.10.x releases are considered “use at your own risk”.
You CAN run VirtualBox alongside Docker for Mac as long you’re using a somewhat new version of VirtualBox. All of the 5.x releases are ok. This is pretty useful because you might have some legacy apps running in Vagrant / VirtualBox to deal with (I know I do!).
Docker for Windows (Docker Desktop)
Docker for Windows requires that you’re running Windows Pro, Enterprise, or Education edition. Sorry, Home edition isn’t available and that’s because Microsoft doesn’t currently allow Hyper-V to be installed on Home editions.
Also, unlike Docker for Mac, you cannot run any version of VirtualBox, VMWare or any other Type 2 hypervisor along with Docker for Windows. That’s because Docker for Windows uses Hyper-V under the hood which is a Type 1 hypervisor.
Docker Toolbox
If you can’t run Docker for Mac or Docker for Windows then there’s the Docker Toolbox. It has much less strict requirements. As long as you can run VirtualBox then you’re good to go (Docker Toolbox manages this for you).
It works all the way back to MacOS 10.8 and Windows 7 (yep, even Home editions).
![Docker for mac connect to localhost password Docker for mac connect to localhost password](https://cdn-images-1.medium.com/max/1600/1*1xPWuEAw04-ybSZVCY1T2Q.png)
Your own Virtual Machine
Docker will happily run inside of VirtualBox, VMWare or any other Type 1 / 2 Hypervisor that’s running a major distribution of Linux.
So, similarly to the Docker Toolbox, rolling your own VM has the same requirements.
Pros and Cons
Now for the good stuff!
Docker for Mac / Docker for Windows (Docker Desktop)
Pros
- Offers the most “native” experience, you can easily use any terminal you you want since Docker is effectively running on
localhost
from MacOS / Windows’ POV. - Docker is heavily developing and polishing this solution.
Cons
- On Windows, if you have legacy apps that need a VM, you can’t reasonably do both.
- On Windows, volume mount performance is still quite poor, but it’ll improve in due time. Update Nov 2017: Volume mount performance is very usable now on Windows!
Docker Toolbox
Pros
- Offers an “out of the box” Docker experience if you have no other choice.
- May offer better volume mount performance under Windows (but this will change).
Cons
- You need to either use the Docker Quickstart Terminal, or configure your own terminal to connect to the Docker Daemon running a VM.
- Not a native solution, so you’ll need to access your Docker Machine’s IP address if you’re developing web apps. Example:
192.168.99.100
instead oflocalhost
. - Suffers from typical VirtualBox edge case bugs and mount performance issues.
Your own Virtual Machine
I’m not going to bother listing a pros and cons here because I wouldn’t recommend doing this UNLESS you plan to go all in with a Virtual Machine set up.
But more on that in a bit…
Which One Should I Use?
If you’re interested in Docker, you’re a smart person and you probably came to the conclusion that using Docker for Mac or Docker for Windows is a good idea (if you can run it).
My recommendation would be to try Docker for Mac / Windows first, and test it against your actual use cases. The performance issues may or may not be a concern, especially since everyone’s needs and computer specs are different.
On Windows and Like the Idea of Running Linux Too?
I want to mention a “roll your own VM” solution for Windows users because I feel like there’s an even better way to run Docker on Windows if you also like Linux.
I no longer use what is described below
It has been fully replaced with another Windows based set up that uses WSL and MobaXterm. It is super solid for full time development.
See The Updated Version!It involves running VMWare in a special mode called “Unity mode”. This basically allows you to run Windows and Linux together seamlessly as 1 operating system.
There’s no dual booting and Linux applications (even graphical apps) run in their own floating windows. Then you can install Docker natively on Linux inside of the VM.
The performance is excellent and the entire set up is free too.
This is what I use personally and have been doing it for years. It gives you the best of both worlds. For example, I run high end audio / video apps on Windows while recording courses and screencasts that cover Linux content. It all works great (even for full time development).
You can watch a video guide and see screenshots on how to do that in this post on creating an awesome Linux development environment in Windows.
Are you using the Docker Toolbox, Docker for Mac / Windows or your own VM?