Skip to content

Commit

Permalink
Support docker-compose like default network behavior by default_net_b…
Browse files Browse the repository at this point in the history
…ehavior_compat switch

Signed-off-by: Songmin Li <[email protected]>
  • Loading branch information
lisongmin committed Dec 17, 2024
1 parent 376f081 commit 45ac8c6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
20 changes: 20 additions & 0 deletions docs/Extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ x-podman:
By default `default_net_name_compat` is `false`. This will change to `true` at some point and the
setting will be removed.

## Compatibility of default network behavior between docker-compose and podman-compose

When there is no network defined (neither network-mode nor networks) in service,
The behavior of default network in docker-compose and podman-compose are different.

| Top-level networks | podman-compose | docker-compose |
| ------------------------------ | -------------------------- | -------------- |
| No networks | default | default |
| One network named net0 | net0 | default |
| Two networks named net0, net1 | podman(`--network=bridge`) | default |
| Contains network named default | default | default |

To enable compatibility between docker-compose and podman-compose, specify
`default_net_behavior_compat: true` under global `x-podman` key:

```yaml
x-podman:
default_net_behavior_compat: true
```

## Custom pods management

Podman-compose can have containers in pods. This can be controlled by extension key x-podman in_pod.
Expand Down
2 changes: 2 additions & 0 deletions newsfragments/default_net_behavior_compat.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Support docker-compose like default network behavior when no network defined in service via `default_net_behavior_compat` feature flag.

20 changes: 15 additions & 5 deletions podman_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -1998,13 +1998,23 @@ def _parse_compose_file(self):
nets = compose.get("networks", {})
if not nets:
nets["default"] = None

self.networks = nets
if len(self.networks) == 1:
self.default_net = list(nets.keys())[0]
elif "default" in nets:
self.default_net = "default"
if compose.get("x-podman", {}).get("default_net_behavior_compat", False):
# If there is no network_mode and networks in service,
# docker-compose will create default network named '<project_name>_default'
# and add the service to the default network.
# So we always set `default_net = 'default'` for compatibility
if "default" not in self.networks:
self.networks["default"] = None
else:
self.default_net = None
if len(self.networks) == 1:
self.default_net = list(nets.keys())[0]
elif "default" in nets:
self.default_net = "default"
else:
self.default_net = None

allnets = set()
for name, srv in services.items():
srv_nets = srv.get("networks", self.default_net)
Expand Down

0 comments on commit 45ac8c6

Please sign in to comment.