External access to the container
Vessel can run some network applications, to make external applications can also access these, you can be -P
or -p
be designated port mapping parameters.
When -P
the marker, Docker randomly mapping a 49000~49900
port into the interior of the container open network ports.
Use docker container ls
can 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 logs
view 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 -
-p
You 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:containerPort
format 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:containerPort
the 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::containerPort
of 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 udp
tag to specify the udp
port
$ docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py
View map port configuration
Use docker port
to 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 inspect
may 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