Open thaJeztah opened 1 year ago
For what it's worth, here's the code in Compose v2 (modified July 2021) that populates ExposedPorts
: https://github.com/docker/compose/blob/6bedc196cc74730522123e159d3e49f411b5c3cd/pkg/compose/create.go#L685-L696
So I think it's possible to have duplicates, e.g.
services:
mariadb:
image: mariadb
expose:
- 3306
ports:
- 3306:3306
In practice, I could not tell you how common that is, as I'm not really sure how expose
is used in Compose YAML.
@milas do you know why compose is merging exposed ports with Published ports? Those should be completely separate things?
I see it was added in https://github.com/docker/compose/commit/94997be6331ab6c8aa53e3516f9408d81cee7b45, but that's probably ported from compose-cli.
Looks like it was;
Trying to think what exactly is done there; does that mean that any exposed
port is automatically published (so --publish-all
when using compose?
Oh! I think I got it; it's the reverse; for a port to be published, it also has to be (implicitly) exposed.
As an example; here's creating an alpine
container, which by itself does not have any ports "exposed" in its config;
docker create --name foo -p 80:80 alpine
But the port-mapping also set an explicit "expose";
DEBU[2023-03-23T09:19:41.786405173Z] Calling POST /v1.42/containers/create?name=foo
DEBU[2023-03-23T09:19:41.786524715Z] form data: {"AttachStderr":true,"AttachStdin":false,"AttachStdout":true,"Cmd":null,"Domainname":"","Entrypoint":null,"Env":null,"ExposedPorts":{"80/tcp":{}},"HostConfig":{"AutoRemove":false,"Binds":null,"BlkioDeviceReadBps":[],"BlkioDeviceReadIOps":[],"BlkioDeviceWriteBps":[],"BlkioDeviceWriteIOps":[],"BlkioWeight":0,"BlkioWeightDevice":[],"CapAdd":null,"CapDrop":null,"Cgroup":"","CgroupParent":"","CgroupnsMode":"","ConsoleSize":[60,216],"ContainerIDFile":"","CpuCount":0,"CpuPercent":0,"CpuPeriod":0,"CpuQuota":0,"CpuRealtimePeriod":0,"CpuRealtimeRuntime":0,"CpuShares":0,"CpusetCpus":"","CpusetMems":"","DeviceCgroupRules":null,"DeviceRequests":null,"Devices":[],"Dns":[],"DnsOptions":[],"DnsSearch":[],"ExtraHosts":null,"GroupAdd":null,"IOMaximumBandwidth":0,"IOMaximumIOps":0,"IpcMode":"","Isolation":"","Links":null,"LogConfig":{"Config":{},"Type":""},"MaskedPaths":null,"Memory":0,"MemoryReservation":0,"MemorySwap":0,"MemorySwappiness":-1,"NanoCpus":0,"NetworkMode":"default","OomKillDisable":false,"OomScoreAdj":0,"PidMode":"","PidsLimit":0,"PortBindings":{"80/tcp":[{"HostIp":"","HostPort":"80"}]},"Privileged":false,"PublishAllPorts":false,"ReadonlyPaths":null,"ReadonlyRootfs":false,"RestartPolicy":{"MaximumRetryCount":0,"Name":"no"},"SecurityOpt":null,"ShmSize":0,"UTSMode":"","Ulimits":null,"UsernsMode":"","VolumeDriver":"","VolumesFrom":null},"Hostname":"","Image":"alpine","Labels":{},"NetworkingConfig":{"EndpointsConfig":{}},"OnBuild":null,"OpenStdin":false,"StdinOnce":false,"Tty":false,"User":"","Volumes":{},"WorkingDir":""}
docker container inspect --format='{{ json .Config.ExposedPorts }}' foo
{"80/tcp":{}}
docker container inspect --format='{{ json .HostConfig.PortBindings }}' foo
{"80/tcp":[{"HostIp":"","HostPort":"80"}]}
/cc @akerouanton
Description
When creating a container using the CLI, duplicate exposed ports are de-duplicated;
When looking at API call that's logged in the daemon debug logs;
That contains
"ExposedPorts":{"3306/tcp":{}}
.Looks like
docker build
also de-duplicates;However, when directly using the API, the duplicate ports are neither rejected, nor normalized;
Reproduce
See above
Expected behavior
We should either;
Warning
so that the duplicate is surfaced on the CLIWe likely cannot do
1.
as doing so would be a breaking change, and could potentially break existing use-cases / clients.docker version
docker info
Additional Info
Reproduced on both 20.10 and 23.0, but likely has been around much longer.