podman

Get Name of Server running Podman Containers


I've written a web app that relies on knowing the hostname of the server it is running on. The web app contains code that issues the hostname command on the container host. Using Docker, I accomplished this by adding hostname: "{{.Node.Hostname}}"

Full Compose file:

version: '3.7'
services:
    ui-api:
        image: pm-interface-dev:latest
        ports:
            - "40781:40781"
        hostname: "{{.Node.Hostname}}"
        restart: always
        networks:
          pm-network:
            aliases:
              - pm_interface

networks:
  pm-network:
    name: pm-network
    driver: overlay
    attachable: true

This works perfectly for Docker. We are transitioning off of CentOS and on to RHEL 8, so I'm trying to figure out how to do the same thing with podman run, but I'm not succeeding. I've tried adding the following flags to the podman run command with no luck: --hostname "{{.Node.Hostname}}", --hostname {{.Node.Hostname}}, and --hostname={{.Node.Hostname}}. But all these do is set the hostname to {{.Node.Hostname}} in the container.

I've also tried specifying the network using --network=shared_network where "shared_network" is a network I defined. But this has not helped either.

So my question is this. Is there a way to duplicate what Docker does with hostname: "{{.Node.Hostname}}" using podman run? My run command is: podman run -d -p 40781:40781 --name pm-interface --env-host pm-interface-dev:latest. And I need to mention these are rootful containers.


Solution

  • I was unaware you can use Go templates in Docker but uncovered create services using templates.

    Docker is written in Go and so I suspect it's using os.Hostname() (using OS-specific kernel|system calls) to acquire the hostname. On Linux (RHEL), you can hostname or cat /etc/hostname to obtain this value.

    Podman does not support using Go templates directly (though you could pre-process) but it does support the --hostname flag. So, equivalently you could podman run ... --hostname=$(hostname).

    If you're using podman-compose (!?), I think you won't get the pre-processing that applies the template so you would need to find a way to do this.

    Using --hostname is intended to set a specific hostname for the container and is not a specific reference to the container's host. Podman creates a unique hostname for the container. It is probably an anti-pattern that your app requires the container host's hostname.