Browse code

Merge pull request #2479 from dudebout/port-redirection-doc

Port redirection doc

Andy Rothfusz authored on 2013/11/02 05:43:53
Showing 3 changed files
... ...
@@ -41,7 +41,7 @@ To stop a container, use ``docker stop``
41 41
 To kill the container, use ``docker kill``
42 42
 
43 43
 .. _cli_attach_examples:
44
- 
44
+
45 45
 Examples:
46 46
 ~~~~~~~~~
47 47
 
... ...
@@ -55,8 +55,8 @@ Examples:
55 55
      Mem:    373572k total,   355560k used,    18012k free,    27872k buffers
56 56
      Swap:   786428k total,        0k used,   786428k free,   221740k cached
57 57
 
58
-     PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
59
-      1 root      20   0 17200 1116  912 R    0  0.3   0:00.03 top                
58
+     PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
59
+      1 root      20   0 17200 1116  912 R    0  0.3   0:00.03 top
60 60
 
61 61
       top - 02:05:55 up  3:05,  0 users,  load average: 0.01, 0.02, 0.05
62 62
       Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
... ...
@@ -64,8 +64,8 @@ Examples:
64 64
       Mem:    373572k total,   355244k used,    18328k free,    27872k buffers
65 65
       Swap:   786428k total,        0k used,   786428k free,   221776k cached
66 66
 
67
-        PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
68
-	    1 root      20   0 17208 1144  932 R    0  0.3   0:00.03 top                
67
+        PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
68
+	    1 root      20   0 17208 1144  932 R    0  0.3   0:00.03 top
69 69
 
70 70
 
71 71
       top - 02:05:58 up  3:06,  0 users,  load average: 0.01, 0.02, 0.05
... ...
@@ -74,9 +74,9 @@ Examples:
74 74
       Mem:    373572k total,   355780k used,    17792k free,    27880k buffers
75 75
       Swap:   786428k total,        0k used,   786428k free,   221776k cached
76 76
 
77
-      PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
78
-           1 root      20   0 17208 1144  932 R    0  0.3   0:00.03 top                
79
-     ^C$ 
77
+      PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
78
+           1 root      20   0 17208 1144  932 R    0  0.3   0:00.03 top
79
+     ^C$
80 80
      $ sudo docker stop $ID
81 81
 
82 82
 .. _cli_build:
... ...
@@ -157,7 +157,7 @@ by using the ``git://`` schema.
157 157
 
158 158
       -m="": Commit message
159 159
       -author="": Author (eg. "John Hannibal Smith <hannibal@a-team.com>"
160
-      -run="": Configuration to be applied when the image is launched with `docker run`. 
160
+      -run="": Configuration to be applied when the image is launched with `docker run`.
161 161
                (ex: '{"Cmd": ["cat", "/world"], "PortSpecs": ["22"]}')
162 162
 
163 163
 Full -run example (multiline is ok within a single quote ``'``)
... ...
@@ -239,7 +239,7 @@ Shell 1: Listening for events
239 239
 .............................
240 240
 
241 241
 .. code-block:: bash
242
-    
242
+
243 243
     $ sudo docker events
244 244
 
245 245
 Shell 2: Start and Stop a Container
... ...
@@ -405,7 +405,7 @@ Insert file from github
405 405
     Usage: docker kill CONTAINER [CONTAINER...]
406 406
 
407 407
     Kill a running container (Send SIGKILL)
408
-    
408
+
409 409
 The main process inside the container will be sent SIGKILL.
410 410
 
411 411
 .. _cli_login:
... ...
@@ -515,7 +515,7 @@ The main process inside the container will be sent SIGKILL.
515 515
 
516 516
     Remove one or more containers
517 517
         -link="": Remove the link instead of the actual container
518
- 
518
+
519 519
 
520 520
 Examples:
521 521
 ~~~~~~~~~
... ...
@@ -621,58 +621,51 @@ use-cases, like running Docker within Docker.
621 621
 
622 622
    docker  run -w /path/to/dir/ -i -t  ubuntu pwd
623 623
 
624
-The ``-w`` lets the command being executed inside directory given, 
625
-here /path/to/dir/. If the path does not exists it is created inside the 
624
+The ``-w`` lets the command being executed inside directory given,
625
+here /path/to/dir/. If the path does not exists it is created inside the
626 626
 container.
627 627
 
628 628
 .. code-block:: bash
629 629
 
630 630
    docker  run  -v `pwd`:`pwd` -w `pwd` -i -t  ubuntu pwd
631 631
 
632
-The ``-v`` flag mounts the current working directory into the container. 
633
-The ``-w`` lets the command being executed inside the current 
632
+The ``-v`` flag mounts the current working directory into the container.
633
+The ``-w`` lets the command being executed inside the current
634 634
 working directory, by changing into the directory to the value
635 635
 returned by ``pwd``. So this combination executes the command
636 636
 using the container, but inside the current working directory.
637 637
 
638 638
 .. code-block:: bash
639 639
 
640
-    docker run -p 127.0.0.0::80 ubuntu bash
641
-
642
-The ``-p`` flag now allows you to bind a port to a specific
643
-interface of the host machine.  In this example port ``80`` of the 
644
-container will have a dynamically allocated port bound to 127.0.0.1 
645
-of the host.
646
-
647
-.. code-block:: bash
648
-
649
-    docker run -p 127.0.0.1:80:80 ubuntu bash
640
+    docker run -p 127.0.0.0:80:8080 ubuntu bash
650 641
 
651
-This will bind port ``80`` of the container to port ``80`` on 127.0.0.1 of your
652
-host machine.
642
+This binds port ``8080`` of the container to port ``80`` on 127.0.0.1 of the
643
+host machine. :ref:`port_redirection` explains in detail how to manipulate ports
644
+in Docker.
653 645
 
654 646
 .. code-block:: bash
655 647
 
656 648
     docker run -expose 80 ubuntu bash
657 649
 
658
-This will expose port ``80`` of the container for use within a link
659
-without publishing the port to the host system's interfaces.  
650
+This exposes port ``80`` of the container for use within a link without
651
+publishing the port to the host system's interfaces. :ref:`port_redirection`
652
+explains in detail how to manipulate ports in Docker.
660 653
 
661 654
 .. code-block:: bash
662 655
 
663 656
     docker run -name console -t -i ubuntu bash
664 657
 
665
-This will create and run a new container with the container name 
658
+This will create and run a new container with the container name
666 659
 being ``console``.
667 660
 
668 661
 .. code-block:: bash
669 662
 
670 663
     docker run -link /redis:redis -name console ubuntu bash
671 664
 
672
-The ``-link`` flag will link the container named ``/redis`` into the 
665
+The ``-link`` flag will link the container named ``/redis`` into the
673 666
 newly created container with the alias ``redis``.  The new container
674 667
 can access the network and environment of the redis container via
675
-environment variables.  The ``-name`` flag will assign the name ``console`` 
668
+environment variables.  The ``-name`` flag will assign the name ``console``
676 669
 to the newly created container.
677 670
 
678 671
 .. _cli_search:
... ...
@@ -713,7 +706,7 @@ to the newly created container.
713 713
     Stop a running container (Send SIGTERM, and then SIGKILL after grace period)
714 714
 
715 715
       -t=10: Number of seconds to wait for the container to stop before killing it.
716
-      
716
+
717 717
 The main process inside the container will receive SIGTERM, and after a grace period, SIGKILL
718 718
 
719 719
 .. _cli_tag:
... ...
@@ -758,6 +751,3 @@ Show the version of the docker client, daemon, and latest released version.
758 758
     Usage: docker wait [OPTIONS] NAME
759 759
 
760 760
     Block until a container stops, then print its exit code.
761
-
762
-
763
-
... ...
@@ -174,10 +174,10 @@ override the default specified in CMD.
174 174
 
175 175
     ``EXPOSE <port> [<port>...]``
176 176
 
177
-The ``EXPOSE`` instruction sets ports to be publicly exposed when
178
-running the image. This is functionally equivalent to running ``docker
179
-commit -run '{"PortSpecs": ["<port>", "<port2>"]}'`` outside the
180
-builder. Take a look at :ref:`port_redirection` for more information.
177
+The ``EXPOSE`` instruction exposes ports for use within links. This is
178
+functionally equivalent to running ``docker commit -run '{"PortSpecs":
179
+["<port>", "<port2>"]}'`` outside the builder. Refer to
180
+:ref:`port_redirection` for detailed information.
181 181
 
182 182
 3.6 ENV
183 183
 -------
... ...
@@ -243,7 +243,7 @@ The copy obeys the following rules:
243 243
   considered a regular file and the contents of ``<src>`` will be
244 244
   written at ``<dest>``.
245 245
 * If ``<dest>`` doesn't exist, it is created along with all missing
246
-  directories in its path. 
246
+  directories in its path.
247 247
 
248 248
 .. _entrypoint_def:
249 249
 
... ...
@@ -8,29 +8,136 @@
8 8
 Port redirection
9 9
 ================
10 10
 
11
-Docker can redirect public TCP and UDP ports to your container, so it can be
12
-reached over the network.  Port redirection is done on ``docker run``
13
-using the -p flag.
11
+Interacting with a service is commonly done through a connection to a
12
+port. When this service runs inside a container, one can connect to
13
+the port after finding the IP address of the container as follows:
14 14
 
15
-A port redirect is specified as *PUBLIC:PRIVATE*, where TCP port
16
-*PUBLIC* will be redirected to TCP port *PRIVATE*. As a special case,
17
-the public port can be omitted, in which case a random public port
18
-will be allocated.
15
+.. code-block:: bash
16
+
17
+    # Find IP address of container with ID <container_id>
18
+    docker inspect <container_id> | grep IPAddress | cut -d '"' -f 4
19
+
20
+However, this IP address is local to the host system and the container
21
+port is not reachable by the outside world. Furthermore, even if the
22
+port is used locally, e.g. by another container, this method is
23
+tedious as the IP address of the container changes every time it
24
+starts.
25
+
26
+Docker addresses these two problems and give a simple and robust way
27
+to access services running inside containers.
28
+
29
+To allow non-local clients to reach the service running inside the
30
+container, Docker provide ways to bind the container port to an
31
+interface of the host system. To simplify communication between
32
+containers, Docker provides the linking mechanism.
33
+
34
+Binding a port to an host interface
35
+-----------------------------------
36
+
37
+To bind a port of the container to a specific interface of the host
38
+system, use the ``-p`` parameter of the ``docker run`` command:
19 39
 
20 40
 .. code-block:: bash
21 41
 
22
-    # A random PUBLIC port is redirected to PRIVATE port 80 on the container
23
-    sudo docker run -p 80 <image> <cmd>
42
+    # General syntax
43
+    docker run -p [([<host_interface>:[host_port]])|(<host_port>):]<container_port>[/udp] <image> <cmd>
44
+
45
+When no host interface is provided, the port is bound to all available
46
+interfaces of the host machine (aka INADDR_ANY, or 0.0.0.0).When no host port is
47
+provided, one is dynamically allocated. The possible combinations of options for
48
+TCP port are the following:
49
+
50
+.. code-block:: bash
24 51
 
25
-    # PUBLIC port 80 is redirected to PRIVATE port 80
26
-    sudo docker run -p 80:80 <image> <cmd>
52
+    # Bind TCP port 8080 of the container to TCP port 80 on 127.0.0.1 of the host machine.
53
+    docker run -p 127.0.0.1:80:8080 <image> <cmd>
27 54
 
28
-To redirect a UDP port the redirection must be expressed as *PUBLIC:PRIVATE/udp*:
55
+    # Bind TCP port 8080 of the container to a dynamically allocated TCP port on 127.0.0.1 of the host machine.
56
+    docker run -p 127.0.0.1::8080 <image> <cmd>
57
+
58
+    # Bind TCP port 8080 of the container to TCP port 80 on all available interfaces of the host machine.
59
+    docker run -p 80:8080 <image> <cmd>
60
+
61
+    # Bind TCP port 8080 of the container to a dynamically allocated TCP port on all available interfaces of the host machine.
62
+    docker run -p 8080 <image> <cmd>
63
+
64
+UDP ports can also be bound by adding a trailing ``/udp``. All the
65
+combinations described for TCP work. Here is only one example:
29 66
 
30 67
 .. code-block:: bash
31 68
 
32
-    # PUBLIC port 5300 is redirected to the PRIVATE port 53 using UDP
33
-    sudo docker run -p 5300:53/udp <image> <cmd>
69
+    # Bind UDP port 5353 of the container to UDP port 53 on 127.0.0.1 of the host machine.
70
+    docker run -p 127.0.0.1:53:5353/udp <image> <cmd>
71
+
72
+The command ``docker port`` lists the interface and port on the host
73
+machine bound to a given container port. It is useful when using
74
+dynamically allocated ports:
75
+
76
+.. code-block:: bash
77
+
78
+   # Bind to a dynamically allocated port
79
+   docker run -p 127.0.0.1::8080 -name dyn-bound <image> <cmd>
80
+
81
+   # Lookup the actual port
82
+   docker port dyn-bound 8080
83
+   127.0.0.1:49160
84
+
85
+
86
+Linking a container
87
+-------------------
88
+
89
+Communication between two containers can also be established in a
90
+docker-specific way called linking.
91
+
92
+To briefly present the concept of linking, let us consider two
93
+containers: ``server``, containing the service, and ``client``,
94
+accessing the service.  Once ``server`` is running, ``client`` is
95
+started and links to server. Linking sets environment variables in
96
+``client`` giving it some information about ``server``. In this sense,
97
+linking is a method of service discovery.
98
+
99
+Let us now get back to our topic of interest; communication between
100
+the two containers. We mentioned that the tricky part about this
101
+communication was that the IP address of ``server`` was not
102
+fixed. Therefore, some of the environment variables are going to be
103
+used to inform ``client`` about this IP address. This process called
104
+exposure, is possible because ``client`` is started after ``server``
105
+has been started.
106
+
107
+Here is a full example. On ``server``, the port of interest is
108
+exposed. The exposure is done either through the ``-expose`` parameter
109
+to the ``docker run`` command, or the ``EXPOSE`` build command in a
110
+Dockerfile:
111
+
112
+.. code-block:: bash
113
+
114
+    # Expose port 80
115
+    docker run -expose 80 -name server <image> <cmd>
116
+
117
+The ``client`` then links to the ``server``:
118
+
119
+.. code-block:: bash
120
+
121
+    # Link
122
+    docker run -name client -link server:linked-server <image> <cmd>
123
+
124
+``client`` locally refers to ``server`` as ``linked-server``. The
125
+following environment variables, among others, are available on
126
+``client``:
127
+
128
+.. code-block:: bash
129
+
130
+    # The default protocol, ip, and port of the service running in the container
131
+    LINKED-SERVER_PORT=tcp://172.17.0.8:80
132
+
133
+    # A specific protocol, ip, and port of various services
134
+    LINKED-SERVER_PORT_80_TCP=tcp://172.17.0.8:80
135
+    LINKED-SERVER_PORT_80_TCP_PROTO=tcp
136
+    LINKED-SERVER_PORT_80_TCP_ADDR=172.17.0.8
137
+    LINKED-SERVER_PORT_80_TCP_PORT=80
138
+
139
+This tells ``client`` that a service is running on port 80 of
140
+``server`` and that ``server`` is accessible at the IP address
141
+172.17.0.8
34 142
 
35
-Default port redirects can be built into a container with the
36
-``EXPOSE`` build command.
143
+Note: Using the ``-p`` parameter also exposes the port..