Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localhost vs 0.0.0.0 with Docker on Mac OS

I am reading the docs here and I find myself a bit confused, since running
docker run --name some-mysql -p 3306:3306 -d mysql

or

docker run --name some-mysql -p 127.0.0.1:3306:3306 -d mysql

then mysql --host localhost --port 3306 -u root gives me the following error :

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2).

But running mysql -u root -p --host 0.0.0.0 works.

Does someone have an explanation ?

like image 907
Aymeric R. Avatar asked Oct 30 '17 23:10

Aymeric R.


People also ask

Can I run Docker locally on Mac?

Install and run Docker Desktop on MacDouble-click Docker.dmg to open the installer, then drag the Docker icon to the Applications folder. Double-click Docker.app in the Applications folder to start Docker.

What is the localhost of Docker?

docker run --network="host" Such a container will share the network stack with the docker host and from the container point of view, localhost (or 127.0. 0.1 ) will refer to the docker host.

How do I run a Docker container in localhost?

A simple solution to this in a Linux machine is to use the --network=”host” option along with the Docker run command. After that, the localhost (127.0. 0.1) in your Docker container will point to the host Linux machine. This runs a Docker container with the settings of the network set to host.

Where are Docker images locally on Mac?

On a Mac, the default location for Docker images is ~/Library/Containers/com. docker. docker/Data/vms/0/. Note than on Windows and Mac, Docker runs Linux containers in a virtual environment.


2 Answers

With docker port forwarding, there are two network namespaces you need to keep track of. The first is inside your container. If you listen on localhost inside the container, nothing outside the container can connect to your application. That includes blocking port forwarding from the docker host and container-to-container networking. So unless your container is talking to itself, you always listen on 0.0.0.0 with the application you are running inside the container.

The second network namespace is on your docker host. When you forward a port with docker run -p 127.0.0.1:1234:5678 ... that configures a listener on the docker host interface 127.0.0.1 port 1234, and forwards it to the container namespace port 5678 (that container must be listening on 0.0.0.0). If you leave off the ip, docker will publish the port on all interfaces on the host.

So when you configure mysql to listen on 127.0.0.1, there's no way to reach it from outside of the container's networking namespace. If you need to prevent others outside of your docker host from reaching the port, configure that restriction when publishing the port on the docker run cli.

like image 173
BMitch Avatar answered Sep 22 '22 23:09

BMitch


As described in the mysql documentation (https://dev.mysql.com/doc/refman/5.7/en/connecting.html), when you connect to 127.0.0.1 with the client, it'll try to use the unix sockets to perform this operation. Normally this would work fine since it's on the same host. In Docker the socket file is not available.

like image 28
Stefano Avatar answered Sep 21 '22 23:09

Stefano