Docker-port mapping/container interconnection

One, port mapping

1. Concept

  • Docker port mapping is to map the service port of the application in the container to the local host machine

2. Implementation

Random mapping

  • When using the -P parameter, Docker will randomly map a port to the open network port of the internal container, and open an nginx service as follows
$ docker run -d -P nginx
e93349d539119dc48dc841e117f6388d6afa6a6065b75a5b4aedaf5fb2a051fc
$ 
$ docker ps
CONTAINER ID    IMAGE        COMMAND         CREATED       STATUS       PORTS          NAMES
e93349d53911    nginx        "nginx -g 'daemon ..."  11 seconds ago   Up 9 seconds    0.0.0.0:32769->80/tcp  zen_kirch

  • At this time, use docker ps to see that the 32769 port of the local host is mapped to the 80 port of the container. At this time, when we browse through the local host and visit http://localhost:32769, the nginx page will appear.

Specify port mapping

  • When using the -p parameter, you can specify the port to be mapped, and only one container can be bound to a specified port.
  • The supported formats are:
    IP:HostPort:ContainerPort
    IP:ContainerPort
    HostPort:ContainerPort
  • Let's start an nginx service and map the local port 8080 to port 80 of the container
$ docker run -d -p 8080:80 nginx
23e725098712d061a1382f33d6fe54da23ae37597a62f8debdd3731b5f9cc4b9
$ 
$ docker ps
CONTAINER ID    IMAGE        COMMAND         CREATED       STATUS       PORTS          NAMES
23e725098712    nginx        "nginx -g 'daemon ..."  8 seconds ago    Up 6 seconds    0.0.0.0:8080->80/tcp  frosty_ptolemy

  • At this time, use docker ps to see that the port 8080 of the local host is mapped to port 80 of the container. At this time, we visit http://localhost:8080 through the local browser and the nginx welcome page will appear.

3. View the mapped port

  • Use the docker port command to view the currently mapped port configuration, and you can also view the bound address. The command format is as follows:
docker port CONTAINER [PRIVATE_PORT[/PROTO]]
  • The container has its own internal network and IP address. You can use docker inspect + container ID to get specific information about the container.

4.Docker adds a mapped port to the running container

method one

Get the container IP

docker inspect `container_name` | grep IPAddress    //将container_name 换成实际环境中的容器名

iptables forwarding port

iptables -t nat -A  DOCKER -p tcp --dport 8001 -j DNAT --to-destination 172.17.0.19:8000
//将容器的8000端口映射到docker主机的8001端口

Method Two

Submit a running container as an image

docker commit containerid foo/live

Run mirror and add port

docker run -d -p 8000:80 foo/live /bin/bash

Second, container interconnection

  • Create and run the container named web1, and the port number is automatically mapped
docker run -itd -P --name web1 centos /bin/bash
  • Create and run container domain web2, link to web1 and communicate with it
docker run -itd -P --name web2 --link web1:web1 centos /bin/bash
  • Enter web2 container ping web1

Guess you like

Origin blog.csdn.net/weixin_45647891/article/details/115067654