Anuruddha
Anuruddha

Reputation: 3245

How to get container ID from within the container itself

I'm trying to get the container ID of the running container at the startup time. This is to use that information in service health check apis.

I got a loadbalancer sitting in front of a fleet of containers, and runs periodic health checks via https://service-n.api.com/health. Idea is to return the container information with the api responses.

I'm using docker-compose to spinup docker containers, it'd be great if there's a way to pass the container id as environment variable to the container, like below.

version: '2'
services:
  web:
    image: my.registry.com/pq-api:1.0.0
    container_name: my-container
    ports:
      - 80:80
      - 443:443
    network_mode: bridge
    environment:
      CONTAINER_ID: "{{.ID}}"

Upvotes: 5

Views: 5048

Answers (1)

yamenk
yamenk

Reputation: 51896

The container Id is already available by default to all containers inside the environment variable HOSTNAME

$ docker run alpine env

   HOSTNAME=....
   ....

Upvotes: 6

Related Questions