Kubernetes
EKS
Docker
Networking
Container Management

how to get container host machine ip address and container name in EKS

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

In EKS, the information you want may live at different Kubernetes layers. The host machine IP is really node information, while the "container name" is part of the pod spec. Those details are not exposed in exactly the same way.

The easiest in-cluster solution is the Kubernetes downward API for values such as pod name, node name, host IP, and pod IP. Container name is the exception: Kubernetes does not expose it directly through the same downward API field set, so you usually provide it yourself or query the Kubernetes API.

Use the Downward API for Host IP and Pod Metadata

Inside a pod running on EKS, you can inject the node's IP address and the pod name as environment variables:

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4  name: demo
5spec:
6  replicas: 1
7  selector:
8    matchLabels:
9      app: demo
10  template:
11    metadata:
12      labels:
13        app: demo
14    spec:
15      containers:
16        - name: app
17          image: nginx:1.27
18          env:
19            - name: POD_NAME
20              valueFrom:
21                fieldRef:
22                  fieldPath: metadata.name
23            - name: NODE_NAME
24              valueFrom:
25                fieldRef:
26                  fieldPath: spec.nodeName
27            - name: HOST_IP
28              valueFrom:
29                fieldRef:
30                  fieldPath: status.hostIP
31            - name: POD_IP
32              valueFrom:
33                fieldRef:
34                  fieldPath: status.podIP

Once the container starts, your application can read those environment variables like any other configuration values.

For example, in Python:

python
1import os
2
3print("pod:", os.environ.get("POD_NAME"))
4print("node:", os.environ.get("NODE_NAME"))
5print("host ip:", os.environ.get("HOST_IP"))
6print("pod ip:", os.environ.get("POD_IP"))

This is the standard Kubernetes-native approach and works the same on EKS as it does on other clusters.

Container Name Is Different

The confusing part is container name. Kubernetes exposes pod metadata and selected status fields through the downward API, but not the current container name as a simple built-in fieldRef value.

That means these common assumptions are wrong:

  • 'HOSTNAME is not the container name; it is normally the pod hostname'
  • 'status.hostIP is the node IP, not the pod IP'
  • the downward API does not have a standard fieldPath for container name

If your application needs its container name, the simplest solution is to set it explicitly in the pod spec:

yaml
1containers:
2  - name: app
3    image: nginx:1.27
4    env:
5      - name: CONTAINER_NAME
6        value: app

This is manual duplication, but it is explicit and reliable.

Querying Kubernetes for More Details

If you need richer metadata, you can query the Kubernetes API or use kubectl from outside the cluster.

From your workstation:

bash
kubectl get pod demo-123456 -o wide
kubectl get pod demo-123456 -o jsonpath='{.spec.nodeName}{"\n"}{.status.hostIP}{"\n"}{.spec.containers[*].name}{"\n"}'

That command can print:

  • the node name
  • the host IP
  • the list of container names defined in the pod

From inside the pod, an application with appropriate RBAC permissions can call the Kubernetes API using its service account token and look up its own pod definition by POD_NAME.

When You Only Need the Node IP

If the actual goal is "what machine is this pod running on," status.hostIP is usually enough. You do not need hostNetwork: true, and you do not need Docker-specific tricks. In Kubernetes, the scheduler already knows which node hosts the pod, and the downward API exposes that information cleanly.

This is especially important on EKS because the node is an EC2 worker or managed node underneath, but your application should usually think in Kubernetes terms first, not Docker terms.

Common Pitfalls

  • Confusing pod IP with host IP. They are not the same address.
  • Expecting the container name to be available through the same downward API fields as pod metadata.
  • Reading HOSTNAME and assuming it is the container name.
  • Adding Docker-specific assumptions in EKS. Kubernetes abstractions are the source of truth here.
  • Querying the Kubernetes API from inside the pod without giving the service account the RBAC permissions it needs.

Summary

  • Use the downward API to get status.hostIP, status.podIP, metadata.name, and spec.nodeName.
  • In EKS, the host machine IP you want is usually status.hostIP.
  • Container name is not exposed as a standard downward API field, so set it manually or query the pod spec through the Kubernetes API.
  • Do not confuse HOSTNAME, pod IP, and node IP; they represent different things.
  • For most debugging and telemetry use cases, pod name plus host IP is enough to identify where the workload is running.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design