Skip to content

keep pod hostname when a container is stopped#28494

Open
agp8x wants to merge 1 commit intocontainers:mainfrom
agp8x:fix/keep-pod-hostname
Open

keep pod hostname when a container is stopped#28494
agp8x wants to merge 1 commit intocontainers:mainfrom
agp8x:fix/keep-pod-hostname

Conversation

@agp8x
Copy link
Copy Markdown

@agp8x agp8x commented Apr 13, 2026

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

Signed-off-by: agp8x <git@agp8x.org>
Copy link
Copy Markdown
Member

@Luap99 Luap99 left a comment

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?

return etchosts.HostEntries{{IP: "127.0.0.1", Names: []string{c.Hostname(), c.config.Name}}}
}

func getContainerNameHostEntry(c *Container) etchosts.HostEntries {
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.

This name makes no sense, it is trival to misuse such a function based on that name. Since it is supposed to match the other one it should be named like that and then it needs a big comment why this exists and is different.

@agp8x
Copy link
Copy Markdown
Author

agp8x commented Apr 13, 2026

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.

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.

2 participants