External access to the container, port mapping

External access to the container

Vessel can run some network applications, to make external applications can also access these, you can be -Por -pbe designated port mapping parameters.

When -Pthe marker, Docker randomly mapping a 49000~49900port into the interior of the container open network ports.

Use docker container lscan be seen, the local host 49155 is mapped to a port 5000 of the container. In this case the machine access port 49155 to access the web application interface provided by the container.

$ docker run -d -P training/webapp python app.py

$ docker container ls -l
CONTAINER ID  IMAGE                   COMMAND       CREATED        STATUS        PORTS                    NAMES
bc533791f3f5  training/webapp:latest  python app.py 5 seconds ago  Up 2 seconds  0.0.0.0:49155->5000/tcp  nostalgic_morse

Similarly, you can docker logsview information application commands.

$ docker logs -f nostalgic_morse
* Running on http://0.0.0.0:5000/
10.0.2.2 - - [23/May/2014 20:16:31] "GET / HTTP/1.1" 200 -
10.0.2.2 - - [23/May/2014 20:16:31] "GET /favicon.ico HTTP/1.1" 404 -

-pYou can specify the port to be mapped, and, only on a designated port can be bound to a container. Supported formats are ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort.

All mapping interface address

Use hostPort:containerPortformat mapped local port 5000 to port 5000 of container, may be performed

$ docker run -d -p 5000:5000 training/webapp python app.py

At this time, the default will bind all local addresses on all interfaces.

Mapped to the specified port specified address

You can use ip:hostPort:containerPortthe format used to specify a particular address map, such as localhost address 127.0.0.1

$ docker run -d -p 127.0.0.1:5000:5000 training/webapp python app.py

Port mapped to any specified address

Use ip::containerPortof any binding localhost port to the container port 5000, local host automatically assigns a port.

$ docker run -d -p 127.0.0.1::5000 training/webapp python app.py

You can also use the udptag to specify the udpport

$ docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py

View map port configuration

Use docker portto view the current port configuration mapping, you can also view the address binding

$ docker port nostalgic_morse 5000
127.0.0.1:49155.

note:

  • Container and has its own internal network ip address (used docker inspectmay acquire all variables, Docker can also have a variable configuration of the network.)

  • -p Tag can be used multiple times to bind multiple ports

E.g

$ docker run -d \
    -p 5000:5000 \
    -p 3000:80 \
    training/webapp \
    python app.py

Guess you like

Origin www.cnblogs.com/justart/p/11687287.html