How to map the docker container network port to the host port

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.

root@nginx:/# apt update
…

root@nginx:/# apt install nginx
…

root@nginx:/# service nginx restart
 * Restarting nginx nginx                                                                                                                    [ OK ]
root@nginx:/# 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

4. 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.

So

How to do port mapping for existing containers?

The container named u-lnmp did not perform port mapping with the mac host when it was created.

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
sha256:081b2232c327d30d0dcc79d93198ecc9dc46347d725622ca8b674a1bc0ce1e61
➜  ~
➜  ~ docker images

4. Delete container: e50b532a5017

➜  ~ docker rm e50b532a5017
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: ubuntu-lnmp

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:
~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/

➜  ~ cd ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/

➜  com.docker.driver.amd64-linux screen tty

After executing the screen tty command, you may see a blank window, and then press Enter, you will see the interaction.

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

a. use the cat command to print the contents of the configuration file: 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 sublime + json plugin.

b. Find “PortBingdings”:  , because the port 80 of the container has been mapped before, the following configuration exists:

"PortBindings": {
    "80/tcp": [
      {
        "HostIp": "",
        "HostPort": "9092"
      }
    ],
},

c. There is already content, add a comma after the content and add new content, change it like this:

"PortBindings": {
    "80/tcp": [
      {
        "HostIp": "",
        "HostPort": "9092"
      }
    ],
    "3306/tcp": [
      {
        "HostIp": "0.0.0.0",
        "HostPort": "9096"
      }
    ]
  },

d. Use json tool to compress configuration content and use vi command to replace modified content.

The config.v2.json configuration file is the same operation. It needs to be modified in two places.

Find ‘ “ExposedPorts”: , Add ports to be exposed as follows:

"ExposedPorts": {
      "3306/tcp": {},
      "80/tcp": {}
},

Find ‘ “Ports”: ‘, Add port mapping as follows:

"Ports": {
      "3306/tcp": [
        {
          "HostIp": "0.0.0.0",
          "HostPort": "9096"
        }
      ],
      "80/tcp": [
        {
          "HostIp": "0.0.0.0",
          "HostPort": "9092"
        }
      ]
    },

Save the above changes to the config.v2.json configuration file.

e. Close the screen (hotkey: Ctrl-a d), restart the docker service and the modified docker container, and then use docker ps to see that the ports have changed.

Add a Comment

Your email address will not be published. Required fields are marked *