I am MacOS, can not provide Docker running environment, so need to use boot2docker, the problem is here, because Boot2docker is actually using vbox virtual machine, so actually external if want to access to the container, need two layers of map, can access to.
Docker network structure diagram on MacOS
The order from inside to outside is: container->boot2docker (virtual machine), physical machine
If you are only using your own development, it is sufficient to map the port of the container to the Boot2docker (virtual machine).
The mapping method is also very simple, that is, on-line, with the parameters at startup -p
docker run -d -p 2345:5432 postgres
This means that the Boot2docker 2345 port is mapped to port 5432 of the container, and if you do not want to specify a port you can use -P
parameters directly, the port is randomly assigned
docker run -d -P postgres
Once the mapping is successful, you can access the container by accessing the port corresponding to the Boot2docker (virtual machine)
Here is the postgres that just started, with the -P
parameters, the randomly assigned port, here is the 32770
bash-3.2$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES44882d23ec12 postgres:latest “/docker-entrypoint. About an hour ago Up 3 seconds 0.0.0.0:32770->5432/tcp postgres
Then use boot2docker ip
the command to view the IP of the Boot2docker (virtual machine)
bash-3.2$ boot2docker ip192.168.59.103
This is 192.168.59.103, now you can use this IP to access PostgreSQL.
You can see that the connection has been successful, the development environment here is OK, if you want to provide access to others, but also to map a layer, that is, from the Boot2docker (virtual machine) to the physical machine mapping
Due to the Boot2docker configuration operation, it is necessary to stop all the running containers, and then stop the Boot2docker, the container to Boot2docker port must also be explicitly specified, not random, otherwise it will not correspond, Since I previously had a container explicitly specifying a port of 2345, the following will map this 2345 port to the 5432 port of the physical machine
First look at the lower port, then stop all the containers, and finally stop Boot2docker
bash-3.2$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES1f926413f7a1 postgres:latest “/docker-entrypoint. 11 seconds ago Up 10 seconds 0.0.0.0:2345->5432/tcp postgres bash-3.2$ docker stop 1f91f9bash-3.2$ docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES1f926413f7a1 postgres:latest “/docker-entrypoint. 9 minutes ago Exited (0) 3 seconds ago postgres
Next, start configuring Boot2docker (virtual machine) mappings
Find the Boot2docker corresponding virtual machine name, the default is BOOT2DOCKER-VM, in order to prevent in case, it is best to confirm the next
To add a corresponding NAT rule for a boot2docker virtual machine
bash-3.2$ VBoxManage modifyvm “boot2docker-vm” --natpf1 “tcp-port$i,tcp,,5432,,2345”;
This indicates that the TCP 5432 port of the physical machine is mapped to the TCP 2345 port of the specified virtual machine, and as for the other parameters, you can see the HelpVBoxManage --help
Then start the virtual machine, which is Boot2docker
bash-3.2$ boot2docker startWaiting for VM and Docker daemon to start............oooStarted.Writing /Users/sinyenn/.boot2docker/certs/boot2docker-vm/ca.pemWriting /Users/sinyenn/.boot2docker/certs/boot2docker-vm/cert.pemWriting /Users/sinyenn/.boot2docker/certs/boot2docker-vm/key.pemYour environment variables are already set correctly.
After starting the virtual machine, let's see if the virtual machine is listening on this port to know if the mapping was successful.
bash-3.2$ lsof -i:5432COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEVBoxHeadl 8762 sinyenn 20u IPv4 0x355348c0c406e2a1 0t0 TCP *:postgresql (LISTEN)
You can see that the mapping is successful, and the next step is to start the container.
bash-3.2$ docker start 1f91f9bash-3.2$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES1f926413f7a1 postgres:latest “/docker-entrypoint. 40 minutes ago Up 3 seconds 0.0.0.0:2345->5432/tcp postgres
Once the container has started successfully, you can connect to the database using the local IP
You can see that the database is also connected successfully, if you do not open the firewall, the LAN machine is able to access the container through your IP.
Docker Port mapping under MacOS