summaryrefslogtreecommitdiff
path: root/docs/port-bindings.md
blob: 7456b86f0809356dea9daa23cc2e18b627f07e37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Port bindings
Port bindings is done in two parts. Firstly, by providing a list of ports to
open inside the container in the `Client().create_container()` method.
Bindings are declared in the `host_config` parameter.

```python
container_id = c.create_container(
    'busybox', 'ls', ports=[1111, 2222],
    host_config=docker.utils.create_host_config(port_bindings={
        1111: 4567,
        2222: None
    })
)
```


You can limit the host address on which the port will be exposed like such:

```python
docker.utils.create_host_config(port_bindings={1111: ('127.0.0.1', 4567)})
```

Or without host port assignment:

```python
docker.utils.create_host_config(port_bindings={1111: ('127.0.0.1',)})
```

If you wish to use UDP instead of TCP (default), you need to declare ports
as such in both the config and host config:

```python
container_id = c.create_container(
	'busybox', 'ls', ports=[(1111, 'udp'), 2222],
    host_config=docker.utils.create_host_config(port_bindings={
        '1111/udp': 4567, 2222: None
    })
)
```