Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize docker Port Mapping under MacOS

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to implement docker port mapping under MacOS. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Docker Network structure Diagram on MacOS

The order from the inside to the outside is: container-> boot2docker (virtual machine)-> physical machine

If you only develop and use it yourself, you can just map the port of the container to the boot2docker (virtual machine). The mapping method is also very simple, that is, it is said on the Internet, add the-p parameter when starting.

Docker run-d-p 234515 postgres 5432

It means to map port 2345 of boot2docker to port 5432 of the container. If you do not want to specify the port, you can directly use the-P parameter and assign the port at will.

Docker run-d-P postgres

After the mapping is successful, the container can be accessed by accessing the corresponding port of the boot2docker (virtual machine). Here is the newly started postgres, with the-P parameter, the randomly assigned port. Here is 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 docker-entrypoint 32770-> 5432/tcp postgres

Then use the boot2docker ip command to view the ip of the boot2docker (virtual machine)

Bash-3.2 $boot2docker ip192.168.59.103

This is 192.168.59.103, and now you can use this IP to access postgresql

You can see that the connection is successful, and the development environment here is ok. If you want to provide access to others, you need to map a layer, that is, the mapping from the boot2docker (virtual machine) to the physical machine.

Because of the boot2docker configuration operation, all running containers must be stopped, and then the boot2docker must be stopped. The container-to-boot2docker port must also be explicitly specified, not randomly, otherwise it will not match. Since I have a container that explicitly specifies the port of 2345, let's map this port of 2345 to port 5432 of the physical machine.

First check the port, then stop all 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 5432/tcp postgres bash-3.2 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 bash-3.2 $boot2docker stopbash-3.2 $

Next, start configuring the boot2docker (virtual machine) mapping

Find the virtual machine name corresponding to boot2docker. The default is boot2docker-vm. As a precaution, you'd better confirm it.

Bash-3.2$ VBoxManage list vms "boot2docker-vm" {edb4768e-b6f7-4122-8c67-c87d64e9b9fd} bash-3.2 $

Add corresponding nat rules for the boot2docker virtual machine

Bash-3.2$ VBoxManage modifyvm "boot2docker-vm"-natpf1 "tcp-port$i,tcp,5432,2345"

This means that the tcp 5432 port of the physical machine is mapped to the tcp 2345 port of the specified virtual machine. As for the meaning of other parameters, see help VBoxManage-help and then start the virtual machine, that 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 see if the mapping is 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 it is already listening, indicating 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 docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES1f926413f7a1 postgres:latest 2345-> 5432/tcp postgres bash-3.2 $

After the container starts successfully, you can connect to the database using local IP

You can see that the database has also been connected successfully, if you do not turn on the firewall, the machines in the local area network can access this container through your IP.

Thank you for reading! This is the end of the article on "how to achieve docker port mapping under MacOS". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report