Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 8 additions & 39 deletions compose/service.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,7 @@
import six
from docker.errors import APIError
from docker.utils import create_host_config, LogConfig
from docker.utils.ports import build_port_bindings, split_port

from . import __version__
from .config import DOCKER_CONFIG_KEYS, merge_environment
Expand DownExpand Up@@ -598,13 +599,13 @@ def _get_container_create_options(
if 'ports' in container_options or 'expose' in self.options:
ports = []
all_ports = container_options.get('ports', []) + self.options.get('expose', [])
for port in all_ports:
port = str(port)
if ':' in port:
port = port.split(':')[-1]
if '/' in port:
port = tuple(port.split('/'))
ports.append(port)
for port_range in all_ports:
internal_range, _ = split_port(port_range)
for port in internal_range:
port = str(port)
if '/' in port:
port = tuple(port.split('/'))
ports.append(port)
container_options['ports'] = ports

override_options['binds'] = merge_volume_bindings(
Expand DownExpand Up@@ -855,38 +856,6 @@ def parse_volume_spec(volume_config):

return VolumeSpec(*parts)


# Ports


def build_port_bindings(ports):
port_bindings = {}
for port in ports:
internal_port, external = split_port(port)
if internal_port in port_bindings:
port_bindings[internal_port].append(external)
else:
port_bindings[internal_port] = [external]
return port_bindings


def split_port(port):
parts = str(port).split(':')
if not 1 <= len(parts) <= 3:
raise ConfigError('Invalid port "%s", should be '
'[[remote_ip:]remote_port:]port[/protocol]' % port)

if len(parts) == 1:
internal_port, = parts
return internal_port, None
if len(parts) == 2:
external_port, internal_port = parts
return internal_port, external_port

external_ip, external_port, internal_port = parts
return internal_port, (external_ip, external_port or None)


# Labels


Expand Down
6 changes: 5 additions & 1 deletion docs/yml.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -102,7 +102,7 @@ An entry with the ip address and hostname will be created in `/etc/hosts` inside
### ports

Expose ports. Either specify both ports (`HOST:CONTAINER`), or just the container

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uvgroovy thanks for the contribution. We just sorted this same material out on a docker/docker pr. So, let's try it here:

---> https://gist.github.com/moxiegirl/3dded271198e4107c4d2

ports

Makes an exposed port accessible on a host and the port is available to
any client that can reach that host. Docker binds the exposed port to a random
port on the host within an ephemeral port range defined by
/proc/sys/net/ipv4/ip_local_port_range. You can also map to a specific port or range of ports.

Acceptable formats for a ports value are:

  • containerPort
  • ip:hostPort:containerPort
  • ip::containerPort
  • hostPort:containerPort

You can specify a range for both the hostPort and the containerPort values.
When specifying ranges, the container port values in the range must match the
number of host port values in the range, for example,
1234-1236:1234-1236/tcp. Once a host is running, use the 'docker-compose port' command
to see the actual mapping.

The following configuration shows examples of the port formats in use:

ports:
- "3000"
- "3000-3005"
- "8000:8000"
- "9090-9091:8080-8081"
- "49100:22"
- "127.0.0.1:8001:8001"
- "127.0.0.1:5000-5010:5000-5010"

When mapping ports using the hostPort:containerPort format, you may
experience erroneous results when specifying a container port lower than 60. This
happens because YAML parses numbers in the format xx:yy as sexagesimal (base
60). To avoid this problem, always explicitly specify your port
mappings as strings.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. The only thing I'd change is this line:

Once a host is running, use the docker port command to see the actual mapping.

This should instead read docker-compose port.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aanand updated comment and the gist.

port (a random host port will be chosen).
port (a random host port will be chosen). You can specify a port range instead of a single port (`START-END`). If you use a range for the container ports, you may specify a range for the host ports as well. both ranges must be of equal size.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @moxiegirl

My take: this seems to suggest on first reading that you can specify a range of container ports but a single host port, which I don't think is correct. Perhaps:


Expose ports. You can specify just a container port (which will be mapped to a random host port), or both (HOST:CONTAINER).

You can optionally pass in ranges of port numbers. If you're specifying host ports as well as container ports, the host and container port ranges must be the same size.


> **Note:** When mapping ports in the `HOST:CONTAINER` format, you may experience
> erroneous results when using a container port lower than 60, because YAML will
Expand All@@ -111,9 +111,12 @@ port (a random host port will be chosen).

ports:
- "3000"
- "3000-3005"
- "8000:8000"
- "9090-9091:8080-8081"
- "49100:22"
- "127.0.0.1:8001:8001"
- "127.0.0.1:5000-5010:5000-5010"

### expose

Expand DownExpand Up@@ -371,3 +374,4 @@ Each of these is a single value, analogous to its
- [Command line reference](cli.md)
- [Compose environment variables](env.md)
- [Compose command line completion](completion.md)

1 change: 1 addition & 0 deletions tests/fixtures/ports-composefile/docker-compose.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,3 +5,4 @@ simple:
ports:
- '3000'
- '49152:3001'
- '49153-49154:3002-3003'
5 changes: 4 additions & 1 deletion tests/integration/cli_test.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -334,6 +334,7 @@ def test_run_service_with_map_ports(self, __):
# get port information
port_random = container.get_local_port(3000)
port_assigned = container.get_local_port(3001)
port_range = container.get_local_port(3002), container.get_local_port(3003)

# close all one off containers we just created
container.stop()
Expand All@@ -342,6 +343,8 @@ def test_run_service_with_map_ports(self, __):
self.assertNotEqual(port_random, None)
self.assertIn("0.0.0.0", port_random)
self.assertEqual(port_assigned, "0.0.0.0:49152")
self.assertEqual(port_range[0], "0.0.0.0:49153")
self.assertEqual(port_range[1], "0.0.0.0:49154")

def test_rm(self):
service = self.project.get_service('simple')
Expand DownExpand Up@@ -456,7 +459,7 @@ def get_port(number, mock_stdout):

self.assertEqual(get_port(3000), container.get_local_port(3000))
self.assertEqual(get_port(3001), "0.0.0.0:49152")
self.assertEqual(get_port(3002), "")
self.assertEqual(get_port(3002), "0.0.0.0:49153")

def test_port_with_scale(self):

Expand Down
44 changes: 0 additions & 44 deletions tests/unit/service_test.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,13 +14,11 @@
ConfigError,
NeedsBuildError,
NoSuchImageError,
build_port_bindings,
build_volume_binding,
get_container_data_volumes,
merge_volume_bindings,
parse_repository_tag,
parse_volume_spec,
split_port,
)


Expand DownExpand Up@@ -108,48 +106,6 @@ def test_get_volumes_from_service_no_container(self):
self.assertEqual(service._get_volumes_from(), [container_id])
from_service.create_container.assert_called_once_with()

def test_split_port_with_host_ip(self):
internal_port, external_port = split_port("127.0.0.1:1000:2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, ("127.0.0.1", "1000"))

def test_split_port_with_protocol(self):
internal_port, external_port = split_port("127.0.0.1:1000:2000/udp")
self.assertEqual(internal_port, "2000/udp")
self.assertEqual(external_port, ("127.0.0.1", "1000"))

def test_split_port_with_host_ip_no_port(self):
internal_port, external_port = split_port("127.0.0.1::2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, ("127.0.0.1", None))

def test_split_port_with_host_port(self):
internal_port, external_port = split_port("1000:2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, "1000")

def test_split_port_no_host_port(self):
internal_port, external_port = split_port("2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, None)

def test_split_port_invalid(self):
with self.assertRaises(ConfigError):
split_port("0.0.0.0:1000:2000:tcp")

def test_build_port_bindings_with_one_port(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")])

def test_build_port_bindings_with_matching_internal_ports(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000", "127.0.0.1:2000:1000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000"), ("127.0.0.1", "2000")])

def test_build_port_bindings_with_nonmatching_internal_ports(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000", "127.0.0.1:2000:2000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")])
self.assertEqual(port_bindings["2000"], [("127.0.0.1", "2000")])

def test_split_domainname_none(self):
service = Service('foo', image='foo', hostname='name', client=self.mock_client)
self.mock_client.containers.return_value = []
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 8 additions & 39 deletions compose/service.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,7 @@
import six
from docker.errors import APIError
from docker.utils import create_host_config, LogConfig
from docker.utils.ports import build_port_bindings, split_port

from . import __version__
from .config import DOCKER_CONFIG_KEYS, merge_environment
Expand DownExpand Up@@ -598,13 +599,13 @@ def _get_container_create_options(
if 'ports' in container_options or 'expose' in self.options:
ports = []
all_ports = container_options.get('ports', []) + self.options.get('expose', [])
for port in all_ports:
port = str(port)
if ':' in port:
port = port.split(':')[-1]
if '/' in port:
port = tuple(port.split('/'))
ports.append(port)
for port_range in all_ports:
internal_range, _ = split_port(port_range)
for port in internal_range:
port = str(port)
if '/' in port:
port = tuple(port.split('/'))
ports.append(port)
container_options['ports'] = ports

override_options['binds'] = merge_volume_bindings(
Expand DownExpand Up@@ -855,38 +856,6 @@ def parse_volume_spec(volume_config):

return VolumeSpec(*parts)


# Ports


def build_port_bindings(ports):
port_bindings = {}
for port in ports:
internal_port, external = split_port(port)
if internal_port in port_bindings:
port_bindings[internal_port].append(external)
else:
port_bindings[internal_port] = [external]
return port_bindings


def split_port(port):
parts = str(port).split(':')
if not 1 <= len(parts) <= 3:
raise ConfigError('Invalid port "%s", should be '
'[[remote_ip:]remote_port:]port[/protocol]' % port)

if len(parts) == 1:
internal_port, = parts
return internal_port, None
if len(parts) == 2:
external_port, internal_port = parts
return internal_port, external_port

external_ip, external_port, internal_port = parts
return internal_port, (external_ip, external_port or None)


# Labels


Expand Down
6 changes: 5 additions & 1 deletion docs/yml.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -102,7 +102,7 @@ An entry with the ip address and hostname will be created in `/etc/hosts` inside
### ports

Expose ports. Either specify both ports (`HOST:CONTAINER`), or just the container

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uvgroovy thanks for the contribution. We just sorted this same material out on a docker/docker pr. So, let's try it here:

---> https://gist.github.com/moxiegirl/3dded271198e4107c4d2

ports

Makes an exposed port accessible on a host and the port is available to
any client that can reach that host. Docker binds the exposed port to a random
port on the host within an ephemeral port range defined by
/proc/sys/net/ipv4/ip_local_port_range. You can also map to a specific port or range of ports.

Acceptable formats for a ports value are:

  • containerPort
  • ip:hostPort:containerPort
  • ip::containerPort
  • hostPort:containerPort

You can specify a range for both the hostPort and the containerPort values.
When specifying ranges, the container port values in the range must match the
number of host port values in the range, for example,
1234-1236:1234-1236/tcp. Once a host is running, use the 'docker-compose port' command
to see the actual mapping.

The following configuration shows examples of the port formats in use:

ports:
- "3000"
- "3000-3005"
- "8000:8000"
- "9090-9091:8080-8081"
- "49100:22"
- "127.0.0.1:8001:8001"
- "127.0.0.1:5000-5010:5000-5010"

When mapping ports using the hostPort:containerPort format, you may
experience erroneous results when specifying a container port lower than 60. This
happens because YAML parses numbers in the format xx:yy as sexagesimal (base
60). To avoid this problem, always explicitly specify your port
mappings as strings.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. The only thing I'd change is this line:

Once a host is running, use the docker port command to see the actual mapping.

This should instead read docker-compose port.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aanand updated comment and the gist.

port (a random host port will be chosen).
port (a random host port will be chosen). You can specify a port range instead of a single port (`START-END`). If you use a range for the container ports, you may specify a range for the host ports as well. both ranges must be of equal size.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @moxiegirl

My take: this seems to suggest on first reading that you can specify a range of container ports but a single host port, which I don't think is correct. Perhaps:


Expose ports. You can specify just a container port (which will be mapped to a random host port), or both (HOST:CONTAINER).

You can optionally pass in ranges of port numbers. If you're specifying host ports as well as container ports, the host and container port ranges must be the same size.


> **Note:** When mapping ports in the `HOST:CONTAINER` format, you may experience
> erroneous results when using a container port lower than 60, because YAML will
Expand All@@ -111,9 +111,12 @@ port (a random host port will be chosen).

ports:
- "3000"
- "3000-3005"
- "8000:8000"
- "9090-9091:8080-8081"
- "49100:22"
- "127.0.0.1:8001:8001"
- "127.0.0.1:5000-5010:5000-5010"

### expose

Expand DownExpand Up@@ -371,3 +374,4 @@ Each of these is a single value, analogous to its
- [Command line reference](cli.md)
- [Compose environment variables](env.md)
- [Compose command line completion](completion.md)

1 change: 1 addition & 0 deletions tests/fixtures/ports-composefile/docker-compose.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,3 +5,4 @@ simple:
ports:
- '3000'
- '49152:3001'
- '49153-49154:3002-3003'
5 changes: 4 additions & 1 deletion tests/integration/cli_test.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -334,6 +334,7 @@ def test_run_service_with_map_ports(self, __):
# get port information
port_random = container.get_local_port(3000)
port_assigned = container.get_local_port(3001)
port_range = container.get_local_port(3002), container.get_local_port(3003)

# close all one off containers we just created
container.stop()
Expand All@@ -342,6 +343,8 @@ def test_run_service_with_map_ports(self, __):
self.assertNotEqual(port_random, None)
self.assertIn("0.0.0.0", port_random)
self.assertEqual(port_assigned, "0.0.0.0:49152")
self.assertEqual(port_range[0], "0.0.0.0:49153")
self.assertEqual(port_range[1], "0.0.0.0:49154")

def test_rm(self):
service = self.project.get_service('simple')
Expand DownExpand Up@@ -456,7 +459,7 @@ def get_port(number, mock_stdout):

self.assertEqual(get_port(3000), container.get_local_port(3000))
self.assertEqual(get_port(3001), "0.0.0.0:49152")
self.assertEqual(get_port(3002), "")
self.assertEqual(get_port(3002), "0.0.0.0:49153")

def test_port_with_scale(self):

Expand Down
44 changes: 0 additions & 44 deletions tests/unit/service_test.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,13 +14,11 @@
ConfigError,
NeedsBuildError,
NoSuchImageError,
build_port_bindings,
build_volume_binding,
get_container_data_volumes,
merge_volume_bindings,
parse_repository_tag,
parse_volume_spec,
split_port,
)


Expand DownExpand Up@@ -108,48 +106,6 @@ def test_get_volumes_from_service_no_container(self):
self.assertEqual(service._get_volumes_from(), [container_id])
from_service.create_container.assert_called_once_with()

def test_split_port_with_host_ip(self):
internal_port, external_port = split_port("127.0.0.1:1000:2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, ("127.0.0.1", "1000"))

def test_split_port_with_protocol(self):
internal_port, external_port = split_port("127.0.0.1:1000:2000/udp")
self.assertEqual(internal_port, "2000/udp")
self.assertEqual(external_port, ("127.0.0.1", "1000"))

def test_split_port_with_host_ip_no_port(self):
internal_port, external_port = split_port("127.0.0.1::2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, ("127.0.0.1", None))

def test_split_port_with_host_port(self):
internal_port, external_port = split_port("1000:2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, "1000")

def test_split_port_no_host_port(self):
internal_port, external_port = split_port("2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, None)

def test_split_port_invalid(self):
with self.assertRaises(ConfigError):
split_port("0.0.0.0:1000:2000:tcp")

def test_build_port_bindings_with_one_port(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")])

def test_build_port_bindings_with_matching_internal_ports(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000", "127.0.0.1:2000:1000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000"), ("127.0.0.1", "2000")])

def test_build_port_bindings_with_nonmatching_internal_ports(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000", "127.0.0.1:2000:2000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")])
self.assertEqual(port_bindings["2000"], [("127.0.0.1", "2000")])

def test_split_domainname_none(self):
service = Service('foo', image='foo', hostname='name', client=self.mock_client)
self.mock_client.containers.return_value = []
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 8 additions & 39 deletions compose/service.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,7 @@
import six
from docker.errors import APIError
from docker.utils import create_host_config, LogConfig
from docker.utils.ports import build_port_bindings, split_port

from . import __version__
from .config import DOCKER_CONFIG_KEYS, merge_environment
Expand DownExpand Up@@ -598,13 +599,13 @@ def _get_container_create_options(
if 'ports' in container_options or 'expose' in self.options:
ports = []
all_ports = container_options.get('ports', []) + self.options.get('expose', [])
for port in all_ports:
port = str(port)
if ':' in port:
port = port.split(':')[-1]
if '/' in port:
port = tuple(port.split('/'))
ports.append(port)
for port_range in all_ports:
internal_range, _ = split_port(port_range)
for port in internal_range:
port = str(port)
if '/' in port:
port = tuple(port.split('/'))
ports.append(port)
container_options['ports'] = ports

override_options['binds'] = merge_volume_bindings(
Expand DownExpand Up@@ -855,38 +856,6 @@ def parse_volume_spec(volume_config):

return VolumeSpec(*parts)


# Ports


def build_port_bindings(ports):
port_bindings = {}
for port in ports:
internal_port, external = split_port(port)
if internal_port in port_bindings:
port_bindings[internal_port].append(external)
else:
port_bindings[internal_port] = [external]
return port_bindings


def split_port(port):
parts = str(port).split(':')
if not 1 <= len(parts) <= 3:
raise ConfigError('Invalid port "%s", should be '
'[[remote_ip:]remote_port:]port[/protocol]' % port)

if len(parts) == 1:
internal_port, = parts
return internal_port, None
if len(parts) == 2:
external_port, internal_port = parts
return internal_port, external_port

external_ip, external_port, internal_port = parts
return internal_port, (external_ip, external_port or None)


# Labels


Expand Down
6 changes: 5 additions & 1 deletion docs/yml.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -102,7 +102,7 @@ An entry with the ip address and hostname will be created in `/etc/hosts` inside
### ports

Expose ports. Either specify both ports (`HOST:CONTAINER`), or just the container

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uvgroovy thanks for the contribution. We just sorted this same material out on a docker/docker pr. So, let's try it here:

---> https://gist.github.com/moxiegirl/3dded271198e4107c4d2

ports

Makes an exposed port accessible on a host and the port is available to
any client that can reach that host. Docker binds the exposed port to a random
port on the host within an ephemeral port range defined by
/proc/sys/net/ipv4/ip_local_port_range. You can also map to a specific port or range of ports.

Acceptable formats for a ports value are:

  • containerPort
  • ip:hostPort:containerPort
  • ip::containerPort
  • hostPort:containerPort

You can specify a range for both the hostPort and the containerPort values.
When specifying ranges, the container port values in the range must match the
number of host port values in the range, for example,
1234-1236:1234-1236/tcp. Once a host is running, use the 'docker-compose port' command
to see the actual mapping.

The following configuration shows examples of the port formats in use:

ports:
- "3000"
- "3000-3005"
- "8000:8000"
- "9090-9091:8080-8081"
- "49100:22"
- "127.0.0.1:8001:8001"
- "127.0.0.1:5000-5010:5000-5010"

When mapping ports using the hostPort:containerPort format, you may
experience erroneous results when specifying a container port lower than 60. This
happens because YAML parses numbers in the format xx:yy as sexagesimal (base
60). To avoid this problem, always explicitly specify your port
mappings as strings.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. The only thing I'd change is this line:

Once a host is running, use the docker port command to see the actual mapping.

This should instead read docker-compose port.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aanand updated comment and the gist.

port (a random host port will be chosen).
port (a random host port will be chosen). You can specify a port range instead of a single port (`START-END`). If you use a range for the container ports, you may specify a range for the host ports as well. both ranges must be of equal size.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @moxiegirl

My take: this seems to suggest on first reading that you can specify a range of container ports but a single host port, which I don't think is correct. Perhaps:


Expose ports. You can specify just a container port (which will be mapped to a random host port), or both (HOST:CONTAINER).

You can optionally pass in ranges of port numbers. If you're specifying host ports as well as container ports, the host and container port ranges must be the same size.


> **Note:** When mapping ports in the `HOST:CONTAINER` format, you may experience
> erroneous results when using a container port lower than 60, because YAML will
Expand All@@ -111,9 +111,12 @@ port (a random host port will be chosen).

ports:
- "3000"
- "3000-3005"
- "8000:8000"
- "9090-9091:8080-8081"
- "49100:22"
- "127.0.0.1:8001:8001"
- "127.0.0.1:5000-5010:5000-5010"

### expose

Expand DownExpand Up@@ -371,3 +374,4 @@ Each of these is a single value, analogous to its
- [Command line reference](cli.md)
- [Compose environment variables](env.md)
- [Compose command line completion](completion.md)

1 change: 1 addition & 0 deletions tests/fixtures/ports-composefile/docker-compose.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,3 +5,4 @@ simple:
ports:
- '3000'
- '49152:3001'
- '49153-49154:3002-3003'
5 changes: 4 additions & 1 deletion tests/integration/cli_test.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -334,6 +334,7 @@ def test_run_service_with_map_ports(self, __):
# get port information
port_random = container.get_local_port(3000)
port_assigned = container.get_local_port(3001)
port_range = container.get_local_port(3002), container.get_local_port(3003)

# close all one off containers we just created
container.stop()
Expand All@@ -342,6 +343,8 @@ def test_run_service_with_map_ports(self, __):
self.assertNotEqual(port_random, None)
self.assertIn("0.0.0.0", port_random)
self.assertEqual(port_assigned, "0.0.0.0:49152")
self.assertEqual(port_range[0], "0.0.0.0:49153")
self.assertEqual(port_range[1], "0.0.0.0:49154")

def test_rm(self):
service = self.project.get_service('simple')
Expand DownExpand Up@@ -456,7 +459,7 @@ def get_port(number, mock_stdout):

self.assertEqual(get_port(3000), container.get_local_port(3000))
self.assertEqual(get_port(3001), "0.0.0.0:49152")
self.assertEqual(get_port(3002), "")
self.assertEqual(get_port(3002), "0.0.0.0:49153")

def test_port_with_scale(self):

Expand Down
44 changes: 0 additions & 44 deletions tests/unit/service_test.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,13 +14,11 @@
ConfigError,
NeedsBuildError,
NoSuchImageError,
build_port_bindings,
build_volume_binding,
get_container_data_volumes,
merge_volume_bindings,
parse_repository_tag,
parse_volume_spec,
split_port,
)


Expand DownExpand Up@@ -108,48 +106,6 @@ def test_get_volumes_from_service_no_container(self):
self.assertEqual(service._get_volumes_from(), [container_id])
from_service.create_container.assert_called_once_with()

def test_split_port_with_host_ip(self):
internal_port, external_port = split_port("127.0.0.1:1000:2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, ("127.0.0.1", "1000"))

def test_split_port_with_protocol(self):
internal_port, external_port = split_port("127.0.0.1:1000:2000/udp")
self.assertEqual(internal_port, "2000/udp")
self.assertEqual(external_port, ("127.0.0.1", "1000"))

def test_split_port_with_host_ip_no_port(self):
internal_port, external_port = split_port("127.0.0.1::2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, ("127.0.0.1", None))

def test_split_port_with_host_port(self):
internal_port, external_port = split_port("1000:2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, "1000")

def test_split_port_no_host_port(self):
internal_port, external_port = split_port("2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, None)

def test_split_port_invalid(self):
with self.assertRaises(ConfigError):
split_port("0.0.0.0:1000:2000:tcp")

def test_build_port_bindings_with_one_port(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")])

def test_build_port_bindings_with_matching_internal_ports(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000", "127.0.0.1:2000:1000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000"), ("127.0.0.1", "2000")])

def test_build_port_bindings_with_nonmatching_internal_ports(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000", "127.0.0.1:2000:2000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")])
self.assertEqual(port_bindings["2000"], [("127.0.0.1", "2000")])

def test_split_domainname_none(self):
service = Service('foo', image='foo', hostname='name', client=self.mock_client)
self.mock_client.containers.return_value = []
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 8 additions & 39 deletions compose/service.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,7 @@
import six
from docker.errors import APIError
from docker.utils import create_host_config, LogConfig
from docker.utils.ports import build_port_bindings, split_port

from . import __version__
from .config import DOCKER_CONFIG_KEYS, merge_environment
Expand DownExpand Up@@ -598,13 +599,13 @@ def _get_container_create_options(
if 'ports' in container_options or 'expose' in self.options:
ports = []
all_ports = container_options.get('ports', []) + self.options.get('expose', [])
for port in all_ports:
port = str(port)
if ':' in port:
port = port.split(':')[-1]
if '/' in port:
port = tuple(port.split('/'))
ports.append(port)
for port_range in all_ports:
internal_range, _ = split_port(port_range)
for port in internal_range:
port = str(port)
if '/' in port:
port = tuple(port.split('/'))
ports.append(port)
container_options['ports'] = ports

override_options['binds'] = merge_volume_bindings(
Expand DownExpand Up@@ -855,38 +856,6 @@ def parse_volume_spec(volume_config):

return VolumeSpec(*parts)


# Ports


def build_port_bindings(ports):
port_bindings = {}
for port in ports:
internal_port, external = split_port(port)
if internal_port in port_bindings:
port_bindings[internal_port].append(external)
else:
port_bindings[internal_port] = [external]
return port_bindings


def split_port(port):
parts = str(port).split(':')
if not 1 <= len(parts) <= 3:
raise ConfigError('Invalid port "%s", should be '
'[[remote_ip:]remote_port:]port[/protocol]' % port)

if len(parts) == 1:
internal_port, = parts
return internal_port, None
if len(parts) == 2:
external_port, internal_port = parts
return internal_port, external_port

external_ip, external_port, internal_port = parts
return internal_port, (external_ip, external_port or None)


# Labels


Expand Down
6 changes: 5 additions & 1 deletion docs/yml.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -102,7 +102,7 @@ An entry with the ip address and hostname will be created in `/etc/hosts` inside
### ports

Expose ports. Either specify both ports (`HOST:CONTAINER`), or just the container

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uvgroovy thanks for the contribution. We just sorted this same material out on a docker/docker pr. So, let's try it here:

---> https://gist.github.com/moxiegirl/3dded271198e4107c4d2

ports

Makes an exposed port accessible on a host and the port is available to
any client that can reach that host. Docker binds the exposed port to a random
port on the host within an ephemeral port range defined by
/proc/sys/net/ipv4/ip_local_port_range. You can also map to a specific port or range of ports.

Acceptable formats for a ports value are:

  • containerPort
  • ip:hostPort:containerPort
  • ip::containerPort
  • hostPort:containerPort

You can specify a range for both the hostPort and the containerPort values.
When specifying ranges, the container port values in the range must match the
number of host port values in the range, for example,
1234-1236:1234-1236/tcp. Once a host is running, use the 'docker-compose port' command
to see the actual mapping.

The following configuration shows examples of the port formats in use:

ports:
- "3000"
- "3000-3005"
- "8000:8000"
- "9090-9091:8080-8081"
- "49100:22"
- "127.0.0.1:8001:8001"
- "127.0.0.1:5000-5010:5000-5010"

When mapping ports using the hostPort:containerPort format, you may
experience erroneous results when specifying a container port lower than 60. This
happens because YAML parses numbers in the format xx:yy as sexagesimal (base
60). To avoid this problem, always explicitly specify your port
mappings as strings.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. The only thing I'd change is this line:

Once a host is running, use the docker port command to see the actual mapping.

This should instead read docker-compose port.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aanand updated comment and the gist.

port (a random host port will be chosen).
port (a random host port will be chosen). You can specify a port range instead of a single port (`START-END`). If you use a range for the container ports, you may specify a range for the host ports as well. both ranges must be of equal size.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @moxiegirl

My take: this seems to suggest on first reading that you can specify a range of container ports but a single host port, which I don't think is correct. Perhaps:


Expose ports. You can specify just a container port (which will be mapped to a random host port), or both (HOST:CONTAINER).

You can optionally pass in ranges of port numbers. If you're specifying host ports as well as container ports, the host and container port ranges must be the same size.


> **Note:** When mapping ports in the `HOST:CONTAINER` format, you may experience
> erroneous results when using a container port lower than 60, because YAML will
Expand All@@ -111,9 +111,12 @@ port (a random host port will be chosen).

ports:
- "3000"
- "3000-3005"
- "8000:8000"
- "9090-9091:8080-8081"
- "49100:22"
- "127.0.0.1:8001:8001"
- "127.0.0.1:5000-5010:5000-5010"

### expose

Expand DownExpand Up@@ -371,3 +374,4 @@ Each of these is a single value, analogous to its
- [Command line reference](cli.md)
- [Compose environment variables](env.md)
- [Compose command line completion](completion.md)

1 change: 1 addition & 0 deletions tests/fixtures/ports-composefile/docker-compose.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,3 +5,4 @@ simple:
ports:
- '3000'
- '49152:3001'
- '49153-49154:3002-3003'
5 changes: 4 additions & 1 deletion tests/integration/cli_test.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -334,6 +334,7 @@ def test_run_service_with_map_ports(self, __):
# get port information
port_random = container.get_local_port(3000)
port_assigned = container.get_local_port(3001)
port_range = container.get_local_port(3002), container.get_local_port(3003)

# close all one off containers we just created
container.stop()
Expand All@@ -342,6 +343,8 @@ def test_run_service_with_map_ports(self, __):
self.assertNotEqual(port_random, None)
self.assertIn("0.0.0.0", port_random)
self.assertEqual(port_assigned, "0.0.0.0:49152")
self.assertEqual(port_range[0], "0.0.0.0:49153")
self.assertEqual(port_range[1], "0.0.0.0:49154")

def test_rm(self):
service = self.project.get_service('simple')
Expand DownExpand Up@@ -456,7 +459,7 @@ def get_port(number, mock_stdout):

self.assertEqual(get_port(3000), container.get_local_port(3000))
self.assertEqual(get_port(3001), "0.0.0.0:49152")
self.assertEqual(get_port(3002), "")
self.assertEqual(get_port(3002), "0.0.0.0:49153")

def test_port_with_scale(self):

Expand Down
44 changes: 0 additions & 44 deletions tests/unit/service_test.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,13 +14,11 @@
ConfigError,
NeedsBuildError,
NoSuchImageError,
build_port_bindings,
build_volume_binding,
get_container_data_volumes,
merge_volume_bindings,
parse_repository_tag,
parse_volume_spec,
split_port,
)


Expand DownExpand Up@@ -108,48 +106,6 @@ def test_get_volumes_from_service_no_container(self):
self.assertEqual(service._get_volumes_from(), [container_id])
from_service.create_container.assert_called_once_with()

def test_split_port_with_host_ip(self):
internal_port, external_port = split_port("127.0.0.1:1000:2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, ("127.0.0.1", "1000"))

def test_split_port_with_protocol(self):
internal_port, external_port = split_port("127.0.0.1:1000:2000/udp")
self.assertEqual(internal_port, "2000/udp")
self.assertEqual(external_port, ("127.0.0.1", "1000"))

def test_split_port_with_host_ip_no_port(self):
internal_port, external_port = split_port("127.0.0.1::2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, ("127.0.0.1", None))

def test_split_port_with_host_port(self):
internal_port, external_port = split_port("1000:2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, "1000")

def test_split_port_no_host_port(self):
internal_port, external_port = split_port("2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, None)

def test_split_port_invalid(self):
with self.assertRaises(ConfigError):
split_port("0.0.0.0:1000:2000:tcp")

def test_build_port_bindings_with_one_port(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")])

def test_build_port_bindings_with_matching_internal_ports(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000", "127.0.0.1:2000:1000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000"), ("127.0.0.1", "2000")])

def test_build_port_bindings_with_nonmatching_internal_ports(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000", "127.0.0.1:2000:2000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")])
self.assertEqual(port_bindings["2000"], [("127.0.0.1", "2000")])

def test_split_domainname_none(self):
service = Service('foo', image='foo', hostname='name', client=self.mock_client)
self.mock_client.containers.return_value = []
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 8 additions & 39 deletions compose/service.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,7 @@
import six
from docker.errors import APIError
from docker.utils import create_host_config, LogConfig
from docker.utils.ports import build_port_bindings, split_port

from . import __version__
from .config import DOCKER_CONFIG_KEYS, merge_environment
Expand DownExpand Up@@ -598,13 +599,13 @@ def _get_container_create_options(
if 'ports' in container_options or 'expose' in self.options:
ports = []
all_ports = container_options.get('ports', []) + self.options.get('expose', [])
for port in all_ports:
port = str(port)
if ':' in port:
port = port.split(':')[-1]
if '/' in port:
port = tuple(port.split('/'))
ports.append(port)
for port_range in all_ports:
internal_range, _ = split_port(port_range)
for port in internal_range:
port = str(port)
if '/' in port:
port = tuple(port.split('/'))
ports.append(port)
container_options['ports'] = ports

override_options['binds'] = merge_volume_bindings(
Expand DownExpand Up@@ -855,38 +856,6 @@ def parse_volume_spec(volume_config):

return VolumeSpec(*parts)


# Ports


def build_port_bindings(ports):
port_bindings = {}
for port in ports:
internal_port, external = split_port(port)
if internal_port in port_bindings:
port_bindings[internal_port].append(external)
else:
port_bindings[internal_port] = [external]
return port_bindings


def split_port(port):
parts = str(port).split(':')
if not 1 <= len(parts) <= 3:
raise ConfigError('Invalid port "%s", should be '
'[[remote_ip:]remote_port:]port[/protocol]' % port)

if len(parts) == 1:
internal_port, = parts
return internal_port, None
if len(parts) == 2:
external_port, internal_port = parts
return internal_port, external_port

external_ip, external_port, internal_port = parts
return internal_port, (external_ip, external_port or None)


# Labels


Expand Down
6 changes: 5 additions & 1 deletion docs/yml.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -102,7 +102,7 @@ An entry with the ip address and hostname will be created in `/etc/hosts` inside
### ports

Expose ports. Either specify both ports (`HOST:CONTAINER`), or just the container

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uvgroovy thanks for the contribution. We just sorted this same material out on a docker/docker pr. So, let's try it here:

---> https://gist.github.com/moxiegirl/3dded271198e4107c4d2

ports

Makes an exposed port accessible on a host and the port is available to
any client that can reach that host. Docker binds the exposed port to a random
port on the host within an ephemeral port range defined by
/proc/sys/net/ipv4/ip_local_port_range. You can also map to a specific port or range of ports.

Acceptable formats for a ports value are:

  • containerPort
  • ip:hostPort:containerPort
  • ip::containerPort
  • hostPort:containerPort

You can specify a range for both the hostPort and the containerPort values.
When specifying ranges, the container port values in the range must match the
number of host port values in the range, for example,
1234-1236:1234-1236/tcp. Once a host is running, use the 'docker-compose port' command
to see the actual mapping.

The following configuration shows examples of the port formats in use:

ports:
- "3000"
- "3000-3005"
- "8000:8000"
- "9090-9091:8080-8081"
- "49100:22"
- "127.0.0.1:8001:8001"
- "127.0.0.1:5000-5010:5000-5010"

When mapping ports using the hostPort:containerPort format, you may
experience erroneous results when specifying a container port lower than 60. This
happens because YAML parses numbers in the format xx:yy as sexagesimal (base
60). To avoid this problem, always explicitly specify your port
mappings as strings.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. The only thing I'd change is this line:

Once a host is running, use the docker port command to see the actual mapping.

This should instead read docker-compose port.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aanand updated comment and the gist.

port (a random host port will be chosen).
port (a random host port will be chosen). You can specify a port range instead of a single port (`START-END`). If you use a range for the container ports, you may specify a range for the host ports as well. both ranges must be of equal size.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @moxiegirl

My take: this seems to suggest on first reading that you can specify a range of container ports but a single host port, which I don't think is correct. Perhaps:


Expose ports. You can specify just a container port (which will be mapped to a random host port), or both (HOST:CONTAINER).

You can optionally pass in ranges of port numbers. If you're specifying host ports as well as container ports, the host and container port ranges must be the same size.


> **Note:** When mapping ports in the `HOST:CONTAINER` format, you may experience
> erroneous results when using a container port lower than 60, because YAML will
Expand All@@ -111,9 +111,12 @@ port (a random host port will be chosen).

ports:
- "3000"
- "3000-3005"
- "8000:8000"
- "9090-9091:8080-8081"
- "49100:22"
- "127.0.0.1:8001:8001"
- "127.0.0.1:5000-5010:5000-5010"

### expose

Expand DownExpand Up@@ -371,3 +374,4 @@ Each of these is a single value, analogous to its
- [Command line reference](cli.md)
- [Compose environment variables](env.md)
- [Compose command line completion](completion.md)

1 change: 1 addition & 0 deletions tests/fixtures/ports-composefile/docker-compose.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,3 +5,4 @@ simple:
ports:
- '3000'
- '49152:3001'
- '49153-49154:3002-3003'
5 changes: 4 additions & 1 deletion tests/integration/cli_test.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -334,6 +334,7 @@ def test_run_service_with_map_ports(self, __):
# get port information
port_random = container.get_local_port(3000)
port_assigned = container.get_local_port(3001)
port_range = container.get_local_port(3002), container.get_local_port(3003)

# close all one off containers we just created
container.stop()
Expand All@@ -342,6 +343,8 @@ def test_run_service_with_map_ports(self, __):
self.assertNotEqual(port_random, None)
self.assertIn("0.0.0.0", port_random)
self.assertEqual(port_assigned, "0.0.0.0:49152")
self.assertEqual(port_range[0], "0.0.0.0:49153")
self.assertEqual(port_range[1], "0.0.0.0:49154")

def test_rm(self):
service = self.project.get_service('simple')
Expand DownExpand Up@@ -456,7 +459,7 @@ def get_port(number, mock_stdout):

self.assertEqual(get_port(3000), container.get_local_port(3000))
self.assertEqual(get_port(3001), "0.0.0.0:49152")
self.assertEqual(get_port(3002), "")
self.assertEqual(get_port(3002), "0.0.0.0:49153")

def test_port_with_scale(self):

Expand Down
44 changes: 0 additions & 44 deletions tests/unit/service_test.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,13 +14,11 @@
ConfigError,
NeedsBuildError,
NoSuchImageError,
build_port_bindings,
build_volume_binding,
get_container_data_volumes,
merge_volume_bindings,
parse_repository_tag,
parse_volume_spec,
split_port,
)


Expand DownExpand Up@@ -108,48 +106,6 @@ def test_get_volumes_from_service_no_container(self):
self.assertEqual(service._get_volumes_from(), [container_id])
from_service.create_container.assert_called_once_with()

def test_split_port_with_host_ip(self):
internal_port, external_port = split_port("127.0.0.1:1000:2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, ("127.0.0.1", "1000"))

def test_split_port_with_protocol(self):
internal_port, external_port = split_port("127.0.0.1:1000:2000/udp")
self.assertEqual(internal_port, "2000/udp")
self.assertEqual(external_port, ("127.0.0.1", "1000"))

def test_split_port_with_host_ip_no_port(self):
internal_port, external_port = split_port("127.0.0.1::2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, ("127.0.0.1", None))

def test_split_port_with_host_port(self):
internal_port, external_port = split_port("1000:2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, "1000")

def test_split_port_no_host_port(self):
internal_port, external_port = split_port("2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, None)

def test_split_port_invalid(self):
with self.assertRaises(ConfigError):
split_port("0.0.0.0:1000:2000:tcp")

def test_build_port_bindings_with_one_port(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")])

def test_build_port_bindings_with_matching_internal_ports(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000", "127.0.0.1:2000:1000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000"), ("127.0.0.1", "2000")])

def test_build_port_bindings_with_nonmatching_internal_ports(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000", "127.0.0.1:2000:2000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")])
self.assertEqual(port_bindings["2000"], [("127.0.0.1", "2000")])

def test_split_domainname_none(self):
service = Service('foo', image='foo', hostname='name', client=self.mock_client)
self.mock_client.containers.return_value = []
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 8 additions & 39 deletions compose/service.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,7 @@
import six
from docker.errors import APIError
from docker.utils import create_host_config, LogConfig
from docker.utils.ports import build_port_bindings, split_port

from . import __version__
from .config import DOCKER_CONFIG_KEYS, merge_environment
Expand DownExpand Up@@ -598,13 +599,13 @@ def _get_container_create_options(
if 'ports' in container_options or 'expose' in self.options:
ports = []
all_ports = container_options.get('ports', []) + self.options.get('expose', [])
for port in all_ports:
port = str(port)
if ':' in port:
port = port.split(':')[-1]
if '/' in port:
port = tuple(port.split('/'))
ports.append(port)
for port_range in all_ports:
internal_range, _ = split_port(port_range)
for port in internal_range:
port = str(port)
if '/' in port:
port = tuple(port.split('/'))
ports.append(port)
container_options['ports'] = ports

override_options['binds'] = merge_volume_bindings(
Expand DownExpand Up@@ -855,38 +856,6 @@ def parse_volume_spec(volume_config):

return VolumeSpec(*parts)


# Ports


def build_port_bindings(ports):
port_bindings = {}
for port in ports:
internal_port, external = split_port(port)
if internal_port in port_bindings:
port_bindings[internal_port].append(external)
else:
port_bindings[internal_port] = [external]
return port_bindings


def split_port(port):
parts = str(port).split(':')
if not 1 <= len(parts) <= 3:
raise ConfigError('Invalid port "%s", should be '
'[[remote_ip:]remote_port:]port[/protocol]' % port)

if len(parts) == 1:
internal_port, = parts
return internal_port, None
if len(parts) == 2:
external_port, internal_port = parts
return internal_port, external_port

external_ip, external_port, internal_port = parts
return internal_port, (external_ip, external_port or None)


# Labels


Expand Down
6 changes: 5 additions & 1 deletion docs/yml.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -102,7 +102,7 @@ An entry with the ip address and hostname will be created in `/etc/hosts` inside
### ports

Expose ports. Either specify both ports (`HOST:CONTAINER`), or just the container

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uvgroovy thanks for the contribution. We just sorted this same material out on a docker/docker pr. So, let's try it here:

---> https://gist.github.com/moxiegirl/3dded271198e4107c4d2

ports

Makes an exposed port accessible on a host and the port is available to
any client that can reach that host. Docker binds the exposed port to a random
port on the host within an ephemeral port range defined by
/proc/sys/net/ipv4/ip_local_port_range. You can also map to a specific port or range of ports.

Acceptable formats for a ports value are:

  • containerPort
  • ip:hostPort:containerPort
  • ip::containerPort
  • hostPort:containerPort

You can specify a range for both the hostPort and the containerPort values.
When specifying ranges, the container port values in the range must match the
number of host port values in the range, for example,
1234-1236:1234-1236/tcp. Once a host is running, use the 'docker-compose port' command
to see the actual mapping.

The following configuration shows examples of the port formats in use:

ports:
- "3000"
- "3000-3005"
- "8000:8000"
- "9090-9091:8080-8081"
- "49100:22"
- "127.0.0.1:8001:8001"
- "127.0.0.1:5000-5010:5000-5010"

When mapping ports using the hostPort:containerPort format, you may
experience erroneous results when specifying a container port lower than 60. This
happens because YAML parses numbers in the format xx:yy as sexagesimal (base
60). To avoid this problem, always explicitly specify your port
mappings as strings.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. The only thing I'd change is this line:

Once a host is running, use the docker port command to see the actual mapping.

This should instead read docker-compose port.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aanand updated comment and the gist.

port (a random host port will be chosen).
port (a random host port will be chosen). You can specify a port range instead of a single port (`START-END`). If you use a range for the container ports, you may specify a range for the host ports as well. both ranges must be of equal size.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @moxiegirl

My take: this seems to suggest on first reading that you can specify a range of container ports but a single host port, which I don't think is correct. Perhaps:


Expose ports. You can specify just a container port (which will be mapped to a random host port), or both (HOST:CONTAINER).

You can optionally pass in ranges of port numbers. If you're specifying host ports as well as container ports, the host and container port ranges must be the same size.


> **Note:** When mapping ports in the `HOST:CONTAINER` format, you may experience
> erroneous results when using a container port lower than 60, because YAML will
Expand All@@ -111,9 +111,12 @@ port (a random host port will be chosen).

ports:
- "3000"
- "3000-3005"
- "8000:8000"
- "9090-9091:8080-8081"
- "49100:22"
- "127.0.0.1:8001:8001"
- "127.0.0.1:5000-5010:5000-5010"

### expose

Expand DownExpand Up@@ -371,3 +374,4 @@ Each of these is a single value, analogous to its
- [Command line reference](cli.md)
- [Compose environment variables](env.md)
- [Compose command line completion](completion.md)

1 change: 1 addition & 0 deletions tests/fixtures/ports-composefile/docker-compose.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,3 +5,4 @@ simple:
ports:
- '3000'
- '49152:3001'
- '49153-49154:3002-3003'
5 changes: 4 additions & 1 deletion tests/integration/cli_test.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -334,6 +334,7 @@ def test_run_service_with_map_ports(self, __):
# get port information
port_random = container.get_local_port(3000)
port_assigned = container.get_local_port(3001)
port_range = container.get_local_port(3002), container.get_local_port(3003)

# close all one off containers we just created
container.stop()
Expand All@@ -342,6 +343,8 @@ def test_run_service_with_map_ports(self, __):
self.assertNotEqual(port_random, None)
self.assertIn("0.0.0.0", port_random)
self.assertEqual(port_assigned, "0.0.0.0:49152")
self.assertEqual(port_range[0], "0.0.0.0:49153")
self.assertEqual(port_range[1], "0.0.0.0:49154")

def test_rm(self):
service = self.project.get_service('simple')
Expand DownExpand Up@@ -456,7 +459,7 @@ def get_port(number, mock_stdout):

self.assertEqual(get_port(3000), container.get_local_port(3000))
self.assertEqual(get_port(3001), "0.0.0.0:49152")
self.assertEqual(get_port(3002), "")
self.assertEqual(get_port(3002), "0.0.0.0:49153")

def test_port_with_scale(self):

Expand Down
44 changes: 0 additions & 44 deletions tests/unit/service_test.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,13 +14,11 @@
ConfigError,
NeedsBuildError,
NoSuchImageError,
build_port_bindings,
build_volume_binding,
get_container_data_volumes,
merge_volume_bindings,
parse_repository_tag,
parse_volume_spec,
split_port,
)


Expand DownExpand Up@@ -108,48 +106,6 @@ def test_get_volumes_from_service_no_container(self):
self.assertEqual(service._get_volumes_from(), [container_id])
from_service.create_container.assert_called_once_with()

def test_split_port_with_host_ip(self):
internal_port, external_port = split_port("127.0.0.1:1000:2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, ("127.0.0.1", "1000"))

def test_split_port_with_protocol(self):
internal_port, external_port = split_port("127.0.0.1:1000:2000/udp")
self.assertEqual(internal_port, "2000/udp")
self.assertEqual(external_port, ("127.0.0.1", "1000"))

def test_split_port_with_host_ip_no_port(self):
internal_port, external_port = split_port("127.0.0.1::2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, ("127.0.0.1", None))

def test_split_port_with_host_port(self):
internal_port, external_port = split_port("1000:2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, "1000")

def test_split_port_no_host_port(self):
internal_port, external_port = split_port("2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, None)

def test_split_port_invalid(self):
with self.assertRaises(ConfigError):
split_port("0.0.0.0:1000:2000:tcp")

def test_build_port_bindings_with_one_port(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")])

def test_build_port_bindings_with_matching_internal_ports(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000", "127.0.0.1:2000:1000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000"), ("127.0.0.1", "2000")])

def test_build_port_bindings_with_nonmatching_internal_ports(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000", "127.0.0.1:2000:2000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")])
self.assertEqual(port_bindings["2000"], [("127.0.0.1", "2000")])

def test_split_domainname_none(self):
service = Service('foo', image='foo', hostname='name', client=self.mock_client)
self.mock_client.containers.return_value = []
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 8 additions & 39 deletions compose/service.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,7 @@
import six
from docker.errors import APIError
from docker.utils import create_host_config, LogConfig
from docker.utils.ports import build_port_bindings, split_port

from . import __version__
from .config import DOCKER_CONFIG_KEYS, merge_environment
Expand DownExpand Up@@ -598,13 +599,13 @@ def _get_container_create_options(
if 'ports' in container_options or 'expose' in self.options:
ports = []
all_ports = container_options.get('ports', []) + self.options.get('expose', [])
for port in all_ports:
port = str(port)
if ':' in port:
port = port.split(':')[-1]
if '/' in port:
port = tuple(port.split('/'))
ports.append(port)
for port_range in all_ports:
internal_range, _ = split_port(port_range)
for port in internal_range:
port = str(port)
if '/' in port:
port = tuple(port.split('/'))
ports.append(port)
container_options['ports'] = ports

override_options['binds'] = merge_volume_bindings(
Expand DownExpand Up@@ -855,38 +856,6 @@ def parse_volume_spec(volume_config):

return VolumeSpec(*parts)


# Ports


def build_port_bindings(ports):
port_bindings = {}
for port in ports:
internal_port, external = split_port(port)
if internal_port in port_bindings:
port_bindings[internal_port].append(external)
else:
port_bindings[internal_port] = [external]
return port_bindings


def split_port(port):
parts = str(port).split(':')
if not 1 <= len(parts) <= 3:
raise ConfigError('Invalid port "%s", should be '
'[[remote_ip:]remote_port:]port[/protocol]' % port)

if len(parts) == 1:
internal_port, = parts
return internal_port, None
if len(parts) == 2:
external_port, internal_port = parts
return internal_port, external_port

external_ip, external_port, internal_port = parts
return internal_port, (external_ip, external_port or None)


# Labels


Expand Down
6 changes: 5 additions & 1 deletion docs/yml.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -102,7 +102,7 @@ An entry with the ip address and hostname will be created in `/etc/hosts` inside
### ports

Expose ports. Either specify both ports (`HOST:CONTAINER`), or just the container

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uvgroovy thanks for the contribution. We just sorted this same material out on a docker/docker pr. So, let's try it here:

---> https://gist.github.com/moxiegirl/3dded271198e4107c4d2

ports

Makes an exposed port accessible on a host and the port is available to
any client that can reach that host. Docker binds the exposed port to a random
port on the host within an ephemeral port range defined by
/proc/sys/net/ipv4/ip_local_port_range. You can also map to a specific port or range of ports.

Acceptable formats for a ports value are:

  • containerPort
  • ip:hostPort:containerPort
  • ip::containerPort
  • hostPort:containerPort

You can specify a range for both the hostPort and the containerPort values.
When specifying ranges, the container port values in the range must match the
number of host port values in the range, for example,
1234-1236:1234-1236/tcp. Once a host is running, use the 'docker-compose port' command
to see the actual mapping.

The following configuration shows examples of the port formats in use:

ports:
- "3000"
- "3000-3005"
- "8000:8000"
- "9090-9091:8080-8081"
- "49100:22"
- "127.0.0.1:8001:8001"
- "127.0.0.1:5000-5010:5000-5010"

When mapping ports using the hostPort:containerPort format, you may
experience erroneous results when specifying a container port lower than 60. This
happens because YAML parses numbers in the format xx:yy as sexagesimal (base
60). To avoid this problem, always explicitly specify your port
mappings as strings.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. The only thing I'd change is this line:

Once a host is running, use the docker port command to see the actual mapping.

This should instead read docker-compose port.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aanand updated comment and the gist.

port (a random host port will be chosen).
port (a random host port will be chosen). You can specify a port range instead of a single port (`START-END`). If you use a range for the container ports, you may specify a range for the host ports as well. both ranges must be of equal size.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @moxiegirl

My take: this seems to suggest on first reading that you can specify a range of container ports but a single host port, which I don't think is correct. Perhaps:


Expose ports. You can specify just a container port (which will be mapped to a random host port), or both (HOST:CONTAINER).

You can optionally pass in ranges of port numbers. If you're specifying host ports as well as container ports, the host and container port ranges must be the same size.


> **Note:** When mapping ports in the `HOST:CONTAINER` format, you may experience
> erroneous results when using a container port lower than 60, because YAML will
Expand All@@ -111,9 +111,12 @@ port (a random host port will be chosen).

ports:
- "3000"
- "3000-3005"
- "8000:8000"
- "9090-9091:8080-8081"
- "49100:22"
- "127.0.0.1:8001:8001"
- "127.0.0.1:5000-5010:5000-5010"

### expose

Expand DownExpand Up@@ -371,3 +374,4 @@ Each of these is a single value, analogous to its
- [Command line reference](cli.md)
- [Compose environment variables](env.md)
- [Compose command line completion](completion.md)

1 change: 1 addition & 0 deletions tests/fixtures/ports-composefile/docker-compose.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,3 +5,4 @@ simple:
ports:
- '3000'
- '49152:3001'
- '49153-49154:3002-3003'
5 changes: 4 additions & 1 deletion tests/integration/cli_test.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -334,6 +334,7 @@ def test_run_service_with_map_ports(self, __):
# get port information
port_random = container.get_local_port(3000)
port_assigned = container.get_local_port(3001)
port_range = container.get_local_port(3002), container.get_local_port(3003)

# close all one off containers we just created
container.stop()
Expand All@@ -342,6 +343,8 @@ def test_run_service_with_map_ports(self, __):
self.assertNotEqual(port_random, None)
self.assertIn("0.0.0.0", port_random)
self.assertEqual(port_assigned, "0.0.0.0:49152")
self.assertEqual(port_range[0], "0.0.0.0:49153")
self.assertEqual(port_range[1], "0.0.0.0:49154")

def test_rm(self):
service = self.project.get_service('simple')
Expand DownExpand Up@@ -456,7 +459,7 @@ def get_port(number, mock_stdout):

self.assertEqual(get_port(3000), container.get_local_port(3000))
self.assertEqual(get_port(3001), "0.0.0.0:49152")
self.assertEqual(get_port(3002), "")
self.assertEqual(get_port(3002), "0.0.0.0:49153")

def test_port_with_scale(self):

Expand Down
44 changes: 0 additions & 44 deletions tests/unit/service_test.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,13 +14,11 @@
ConfigError,
NeedsBuildError,
NoSuchImageError,
build_port_bindings,
build_volume_binding,
get_container_data_volumes,
merge_volume_bindings,
parse_repository_tag,
parse_volume_spec,
split_port,
)


Expand DownExpand Up@@ -108,48 +106,6 @@ def test_get_volumes_from_service_no_container(self):
self.assertEqual(service._get_volumes_from(), [container_id])
from_service.create_container.assert_called_once_with()

def test_split_port_with_host_ip(self):
internal_port, external_port = split_port("127.0.0.1:1000:2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, ("127.0.0.1", "1000"))

def test_split_port_with_protocol(self):
internal_port, external_port = split_port("127.0.0.1:1000:2000/udp")
self.assertEqual(internal_port, "2000/udp")
self.assertEqual(external_port, ("127.0.0.1", "1000"))

def test_split_port_with_host_ip_no_port(self):
internal_port, external_port = split_port("127.0.0.1::2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, ("127.0.0.1", None))

def test_split_port_with_host_port(self):
internal_port, external_port = split_port("1000:2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, "1000")

def test_split_port_no_host_port(self):
internal_port, external_port = split_port("2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, None)

def test_split_port_invalid(self):
with self.assertRaises(ConfigError):
split_port("0.0.0.0:1000:2000:tcp")

def test_build_port_bindings_with_one_port(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")])

def test_build_port_bindings_with_matching_internal_ports(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000", "127.0.0.1:2000:1000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000"), ("127.0.0.1", "2000")])

def test_build_port_bindings_with_nonmatching_internal_ports(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000", "127.0.0.1:2000:2000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")])
self.assertEqual(port_bindings["2000"], [("127.0.0.1", "2000")])

def test_split_domainname_none(self):
service = Service('foo', image='foo', hostname='name', client=self.mock_client)
self.mock_client.containers.return_value = []
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 8 additions & 39 deletions compose/service.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,7 @@
import six
from docker.errors import APIError
from docker.utils import create_host_config, LogConfig
from docker.utils.ports import build_port_bindings, split_port

from . import __version__
from .config import DOCKER_CONFIG_KEYS, merge_environment
Expand DownExpand Up@@ -598,13 +599,13 @@ def _get_container_create_options(
if 'ports' in container_options or 'expose' in self.options:
ports = []
all_ports = container_options.get('ports', []) + self.options.get('expose', [])
for port in all_ports:
port = str(port)
if ':' in port:
port = port.split(':')[-1]
if '/' in port:
port = tuple(port.split('/'))
ports.append(port)
for port_range in all_ports:
internal_range, _ = split_port(port_range)
for port in internal_range:
port = str(port)
if '/' in port:
port = tuple(port.split('/'))
ports.append(port)
container_options['ports'] = ports

override_options['binds'] = merge_volume_bindings(
Expand DownExpand Up@@ -855,38 +856,6 @@ def parse_volume_spec(volume_config):

return VolumeSpec(*parts)


# Ports


def build_port_bindings(ports):
port_bindings = {}
for port in ports:
internal_port, external = split_port(port)
if internal_port in port_bindings:
port_bindings[internal_port].append(external)
else:
port_bindings[internal_port] = [external]
return port_bindings


def split_port(port):
parts = str(port).split(':')
if not 1 <= len(parts) <= 3:
raise ConfigError('Invalid port "%s", should be '
'[[remote_ip:]remote_port:]port[/protocol]' % port)

if len(parts) == 1:
internal_port, = parts
return internal_port, None
if len(parts) == 2:
external_port, internal_port = parts
return internal_port, external_port

external_ip, external_port, internal_port = parts
return internal_port, (external_ip, external_port or None)


# Labels


Expand Down
6 changes: 5 additions & 1 deletion docs/yml.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -102,7 +102,7 @@ An entry with the ip address and hostname will be created in `/etc/hosts` inside
### ports

Expose ports. Either specify both ports (`HOST:CONTAINER`), or just the container

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uvgroovy thanks for the contribution. We just sorted this same material out on a docker/docker pr. So, let's try it here:

---> https://gist.github.com/moxiegirl/3dded271198e4107c4d2

ports

Makes an exposed port accessible on a host and the port is available to
any client that can reach that host. Docker binds the exposed port to a random
port on the host within an ephemeral port range defined by
/proc/sys/net/ipv4/ip_local_port_range. You can also map to a specific port or range of ports.

Acceptable formats for a ports value are:

  • containerPort
  • ip:hostPort:containerPort
  • ip::containerPort
  • hostPort:containerPort

You can specify a range for both the hostPort and the containerPort values.
When specifying ranges, the container port values in the range must match the
number of host port values in the range, for example,
1234-1236:1234-1236/tcp. Once a host is running, use the 'docker-compose port' command
to see the actual mapping.

The following configuration shows examples of the port formats in use:

ports:
- "3000"
- "3000-3005"
- "8000:8000"
- "9090-9091:8080-8081"
- "49100:22"
- "127.0.0.1:8001:8001"
- "127.0.0.1:5000-5010:5000-5010"

When mapping ports using the hostPort:containerPort format, you may
experience erroneous results when specifying a container port lower than 60. This
happens because YAML parses numbers in the format xx:yy as sexagesimal (base
60). To avoid this problem, always explicitly specify your port
mappings as strings.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. The only thing I'd change is this line:

Once a host is running, use the docker port command to see the actual mapping.

This should instead read docker-compose port.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aanand updated comment and the gist.

port (a random host port will be chosen).
port (a random host port will be chosen). You can specify a port range instead of a single port (`START-END`). If you use a range for the container ports, you may specify a range for the host ports as well. both ranges must be of equal size.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @moxiegirl

My take: this seems to suggest on first reading that you can specify a range of container ports but a single host port, which I don't think is correct. Perhaps:


Expose ports. You can specify just a container port (which will be mapped to a random host port), or both (HOST:CONTAINER).

You can optionally pass in ranges of port numbers. If you're specifying host ports as well as container ports, the host and container port ranges must be the same size.


> **Note:** When mapping ports in the `HOST:CONTAINER` format, you may experience
> erroneous results when using a container port lower than 60, because YAML will
Expand All@@ -111,9 +111,12 @@ port (a random host port will be chosen).

ports:
- "3000"
- "3000-3005"
- "8000:8000"
- "9090-9091:8080-8081"
- "49100:22"
- "127.0.0.1:8001:8001"
- "127.0.0.1:5000-5010:5000-5010"

### expose

Expand DownExpand Up@@ -371,3 +374,4 @@ Each of these is a single value, analogous to its
- [Command line reference](cli.md)
- [Compose environment variables](env.md)
- [Compose command line completion](completion.md)

1 change: 1 addition & 0 deletions tests/fixtures/ports-composefile/docker-compose.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,3 +5,4 @@ simple:
ports:
- '3000'
- '49152:3001'
- '49153-49154:3002-3003'
5 changes: 4 additions & 1 deletion tests/integration/cli_test.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -334,6 +334,7 @@ def test_run_service_with_map_ports(self, __):
# get port information
port_random = container.get_local_port(3000)
port_assigned = container.get_local_port(3001)
port_range = container.get_local_port(3002), container.get_local_port(3003)

# close all one off containers we just created
container.stop()
Expand All@@ -342,6 +343,8 @@ def test_run_service_with_map_ports(self, __):
self.assertNotEqual(port_random, None)
self.assertIn("0.0.0.0", port_random)
self.assertEqual(port_assigned, "0.0.0.0:49152")
self.assertEqual(port_range[0], "0.0.0.0:49153")
self.assertEqual(port_range[1], "0.0.0.0:49154")

def test_rm(self):
service = self.project.get_service('simple')
Expand DownExpand Up@@ -456,7 +459,7 @@ def get_port(number, mock_stdout):

self.assertEqual(get_port(3000), container.get_local_port(3000))
self.assertEqual(get_port(3001), "0.0.0.0:49152")
self.assertEqual(get_port(3002), "")
self.assertEqual(get_port(3002), "0.0.0.0:49153")

def test_port_with_scale(self):

Expand Down
44 changes: 0 additions & 44 deletions tests/unit/service_test.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,13 +14,11 @@
ConfigError,
NeedsBuildError,
NoSuchImageError,
build_port_bindings,
build_volume_binding,
get_container_data_volumes,
merge_volume_bindings,
parse_repository_tag,
parse_volume_spec,
split_port,
)


Expand DownExpand Up@@ -108,48 +106,6 @@ def test_get_volumes_from_service_no_container(self):
self.assertEqual(service._get_volumes_from(), [container_id])
from_service.create_container.assert_called_once_with()

def test_split_port_with_host_ip(self):
internal_port, external_port = split_port("127.0.0.1:1000:2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, ("127.0.0.1", "1000"))

def test_split_port_with_protocol(self):
internal_port, external_port = split_port("127.0.0.1:1000:2000/udp")
self.assertEqual(internal_port, "2000/udp")
self.assertEqual(external_port, ("127.0.0.1", "1000"))

def test_split_port_with_host_ip_no_port(self):
internal_port, external_port = split_port("127.0.0.1::2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, ("127.0.0.1", None))

def test_split_port_with_host_port(self):
internal_port, external_port = split_port("1000:2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, "1000")

def test_split_port_no_host_port(self):
internal_port, external_port = split_port("2000")
self.assertEqual(internal_port, "2000")
self.assertEqual(external_port, None)

def test_split_port_invalid(self):
with self.assertRaises(ConfigError):
split_port("0.0.0.0:1000:2000:tcp")

def test_build_port_bindings_with_one_port(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")])

def test_build_port_bindings_with_matching_internal_ports(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000", "127.0.0.1:2000:1000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000"), ("127.0.0.1", "2000")])

def test_build_port_bindings_with_nonmatching_internal_ports(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000", "127.0.0.1:2000:2000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")])
self.assertEqual(port_bindings["2000"], [("127.0.0.1", "2000")])

def test_split_domainname_none(self):
service = Service('foo', image='foo', hostname='name', client=self.mock_client)
self.mock_client.containers.return_value = []
Expand Down