April 02, 2017

Publishing Ports

Publishing Ports

Exposing and publishing network ports is a way to allow the container communicate with other containers and the external world. The container ports can be bound or published to specific or random host ports.
  • Publish all exposed ports to random ports
    • -P or --publish-all
  • Publish or bind a container port or group of ports to the host
    • -p, --publish list
    • Publish or bind to specific port (<publicPort>:<privatePort>)
      • e.g. -p 8080:80
    • Publish or bind to random port (<privatePort>)
      • e.g. -p 80
      • This binds container port 80 to a random host port, e.g. port 32768
    • You can optionally specify which IP to bind on: <hostInterface>:<publicPort>:<privatePort>, e.g. 127.0.0.1:6379:6379.
      This limits the connection to this port from the localhost (127.0.0.1) only.
    • Examples
      • $ docker run -d -P redis
        • Run redis detached and publish all exposed ports to random ports. The container port, 6379, is exposed through the random port, 32768, to the host
        • Docker communicates through this random port to the default port in the container
        • The container is listening on this exposed port.
      • $ docker run -d -P nginx
        • Run nginx server, detached and publish all exposed ports. In this case ports 80 and 443 are  available through the exposed random port(s), e.g.:
      • -P publishes all exposed ports to random port numbers on the host
      • With -p (lower case p) you can pick which container port to publish and which host port to map it to
      • -p syntax is -p <container port> or -p <host port>:<container port> or -p <host interface>:<host port>:<container port>
        • -p <container port>

      • Publish container port to a random host port. E.g. container port 80 published to host port 32771.
        Note that only port 80 is published, container port 443 is not available on the host.
      • Use telnet to verify service (HTTP) is bound to the random port, 32771 on the host:
      • -p <host port>:<container port>

      • The container port 80 is published as port 8080 to the host.
        A connection to port 8080 on the host is mapped to port 80 in the container

 

No comments:

Post a Comment