Skip to content

keep pod hostname when a container is stopped#28494

Merged
Luap99 merged 1 commit into
podman-container-tools:mainfrom
agp8x:fix/keep-pod-hostname
Apr 28, 2026
Merged

keep pod hostname when a container is stopped#28494
Luap99 merged 1 commit into
podman-container-tools:mainfrom
agp8x:fix/keep-pod-hostname

Conversation

@agp8x

@agp8x agp8x commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

Checklist

Ensure you have completed the following checklist for your pull request to be reviewed:

  • Certify you wrote the patch or otherwise have the right to pass it on as an open-source patch by signing all
    commits. (git commit -s). (If needed, use git commit -s --amend). The author email must match
    the sign-off email address. See CONTRIBUTING.md
    for more information.
  • Referenced issues using Fixes: #00000 in commit message (if applicable)
  • Tests have been added/updated (or no tests are needed)
  • Documentation has been updated (or no documentation changes are needed)
  • All commits pass make validatepr (format/lint checks)
  • Release note entered in the section below (or None if no user-facing changes)

Does this PR introduce a user-facing change?


Problem

When a Pod is configured with additional hosts containing an FQDN for the pod hostname, the FQDN entry is removed when any container is stopped.

Root Cause

etchosts.Remove() is called with an array containing the container hostname. In Pods, the container hostname is configured on pod level.

Fix

Omit the hostname during remove

Test

Preparations

# Containerfile for my-whoami
FROM traefik/whoami:v1.11 as whoami
FROM alpine:3.11
COPY --from=whoami /whoami /whoami
CMD /whoami

mkdir -p /my/run/

Create pod

HOST_IP=192.168.122.171
podman pod create --infra-conmon-pidfile /my/run/pod.pid --infra-name my-pod-infra --pod-id-file /my/run/pod.pod-id --name my-pod --hostname myhost--my-pod --add-host "myhost.my.domain;myhost:${HOST_IP}" --add-host "myhost--m
y-pod.my.domain;myhost--my-pod:127.0.0.1" --add-host "host.container.internal:${HOST_IP}" --replace

Start containers

podman run --cidfile=/my/run/container_a.id --pod-id-file=/my/run/pod.pod-id --cgroups=no-conmon --replace -d -t --name my-pod-contA my-whoami /whoami --port=80
podman run --cidfile=/my/run/container_b.id --pod-id-file=/my/run/pod.pod-id --cgroups=no-conmon --replace -d -t --name my-pod-contB my-whoami /whoami --port=81

Verify

podman exec my-pod-contA hostname -f-> myhost--my-pod.my.domain
podman stop my-pod-contB
podman exec my-pod-contA hostname -f-> hostname: myhost--my-pod: Host not found

@Luap99 Luap99 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, we need a test case at least.

But I don't believe fixing this like that is correct, you have just sidestep your personal problem. If a user would have the container name and ip in the hosts file it would still be the same problem.

Isn't the real problem that the etchosts.Remove() only matches one name instead of matching all names? I guess this is because Add() does not actually add all names when the hostname or container name is already present.
Since the main pod infra will always have the hostname as entry with the main ip the localhost entries for the container will never receive the hostname and thus we cannot use the full match like that.

But then if we never add the hostname in the entry that we might as well just drop it from getLocalhostHostEntry() and then it would work already for you?


Practically there is the question of why do you even set --add-host hostname:127.0.0.1 when there is a hostname entry by default?

Comment thread libpod/container_internal_common.go Outdated
@agp8x

agp8x commented Apr 13, 2026

Copy link
Copy Markdown
Contributor Author

Practically there is the question of why do you even set --add-host hostname:127.0.0.1 when there is a hostname entry by default?

I have applications running in multiple pods that need a FQDN, so the --hostname argument is not sufficient. To get the FQDN for the pod hostname, there has to be an entry 127.0.01 fqdn hostnamein /etc/hosts. That is possible with the new syntax of --add-hosts introduced in podman 5.3: --add-host=fdqn;hostname:127.0.0.1.

But then if we never add the hostname in the entry that we might as well just drop it from getLocalhostHostEntry() and then it would work already for you?

That sounds great, I'll add a test case and refactor it.

@agp8x agp8x force-pushed the fix/keep-pod-hostname branch from c139e84 to 08004a4 Compare April 14, 2026 07:10
@packit-as-a-service

Copy link
Copy Markdown

[NON-BLOCKING] Packit jobs failed. @containers/packit-build please check. Everyone else, feel free to ignore.

1 similar comment
@packit-as-a-service

Copy link
Copy Markdown

[NON-BLOCKING] Packit jobs failed. @containers/packit-build please check. Everyone else, feel free to ignore.

Comment thread test/system/500-networking.bats Outdated

@Honny1 Honny1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Additionally to @Luap99's comments: I would like to ask you to squash your commits and remove the merge commit (please rebase onto main instead). Also, if there is an existing issue related to this fix, please reference it in the description and commit message using Fixes: #<issue-number>.

@agp8x agp8x force-pushed the fix/keep-pod-hostname branch from 08004a4 to 3e97ccd Compare April 17, 2026 11:39

@Honny1 Honny1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@Honny1 Honny1 requested a review from Luap99 April 20, 2026 08:13
@Honny1

Honny1 commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

PTAL @containers/podman-maintainers

@giuseppe

Copy link
Copy Markdown
Contributor

there is a lot of information in the PR but that is not present in the git commit message itself, could you add it there?

@giuseppe

Copy link
Copy Markdown
Contributor

and also please use your real name for the git commit and signed-off-by line

Comment thread test/system/500-networking.bats Outdated
When a container in a pod is stopped, its container name is removed from /etc/hosts. etchosts.Remove() filters for any entry matching the container name or the pod hostname. A pod with additional host entries like --add-host FQDN;pod-hostname:127.0.0.1 is affected by this deletion, too.

Only the container name needs to be removed when a container is stopped.

Signed-off-by: Clemens Klug <git@agp8x.org>
@agp8x agp8x force-pushed the fix/keep-pod-hostname branch from 3e97ccd to 24130e2 Compare April 24, 2026 10:21
@agp8x

agp8x commented Apr 24, 2026

Copy link
Copy Markdown
Contributor Author

Added more details to commit message and updated to use the real name

@Luap99 Luap99 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@Luap99 Luap99 merged commit 241e59b into podman-container-tools:main Apr 28, 2026
82 of 83 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants