In the previous article, we showed
how to use docker to run the Ubuntu system.
In this article, we will show how to
map the docker container port to the host port and provide services through the host.
Here take webserver
nginx as an example, port 9091 of the Mac host is mapped to port 80 of the container.
1. Check Mac host 9091 port.
➜ ~ lsof -i :9091
OK, port 9091 is not used.
2. Run a new docker container and map the port.
➜ ~ docker run -i -t -h nginx --name webserver -p 9091:80 ccc6e87d482b /bin/bash
➜ ~ docker ps -a
3. Install nginx on the docker container and start service.
[email protected]:/# apt update
[email protected]:/# apt install nginx
[email protected]:/# service nginx restart
* Restarting nginx nginx [ OK ]
[email protected]:/# ps -ef | grep nginx
root 749 1 0 08:03 ? 00:00:00 nginx: master process /usr/sbin/nginx
www-data 750 749 0 08:03 ? 00:00:00 nginx: worker process
www-data 751 749 0 08:03 ? 00:00:00 nginx: worker process
root 754 1 0 08:03 pts/0 00:00:00 grep --color=auto nginx
Finally, open the safer browser on the MAC host and enter: http://127.0.0.1:9091
OK, here we have done the port mapping for the Mac host and the new container.
How to do port mapping for existing containers?
The container named
did not perform port mapping with the mac host when it was created. u-lnmp
Now let’s perform port mapping on existing containers and mac hosts.
1. List docker containers
➜ ~ docker ps
2. Stop container
➜ ~ docker stop e50b532a5017
3. Create a new image from a container’s changes
➜ ~ docker commit e50b532a5017 ubuntu-lnmp
➜ ~ docker images
4. Delete container：
➜ ~ docker rm e50b532a5017
5. Create a new container from the image Ubuntu LNMP and map the port
➜ ~ docker run -i -t -h lnmp --name ubuntu-lnmp -p 9092:80 081b2232c327 /bin/bash
6. Similarly, you can open a browser on the mac host to access the container service.
The port mapping to the existing container can also be realized by modifying the configuration file.
For example, we map a new port to the container named：
1. Stop the container.
➜ ~ docker stop 5e9df9e91ffc
2. Go to the docker directory and use the screen command to enter the virtual machine window.
Because, I use Docker for Mac Application, it appears that the containers are stored within the VM located at:
➜ ~ cd ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/
➜ com.docker.driver.amd64-linux screen tty
After executing the
command, you may see a blank window, and then press Enter, you will see the interaction. screen tty
If the interactive window cannot enter the interactive window or the window is garbled, you can operate it after restarting the docker service.
3. Enter the container folder that needs to be modified according to the container id
Two configuration files need to be modified to map the port through the configuration file:
hostconfig.json and config.v2.json use the cat command to print the contents of the configuration file: a. hostconfig.json
docker-desktop:/var/lib/docker/containers/5e9df9e91ffcb03d693360b4582d194e9661515095d209f7d579e5800e947513# cat hostconfig.json
The content of the
hostconfig.json file is a json format configuration file, which is more troublesome to operate in the terminal; use the cat command to print the file content and copy the content to the json conversion tool. I use the + json plugin. sublime Find b. “PortBingdings”: , because the port 80 of the container has been mapped before, the following configuration exists：
There is already content, add a comma after the content and add new content, change it like this: c.
Use json tool to compress configuration content and use vi command to replace modified content. d.
configuration file is the same operation. It needs to be modified in two places. config.v2.json
, Add ports to be exposed as follows： “ExposedPorts”: ‘
Add port mapping as follows: “Ports”: ‘,
Save the above changes to the
configuration file. config.v2.json Close the screen (hotkey: e. Ctrl-a d), restart the docker service and the modified docker container, and then use docker ps to see that the ports have changed.