IT Log

Record various IT issues and difficulties.

How to check which Docker container a program is running on the Docker host?


To determine which Docker container a specific program is running on, you can use the following methods:

Method 1: Check Processes in All Containers Using docker top

  1. List all running containers to get their IDs and names:
    docker ps

  2. Check processes in each container for the target program (e.g., nginx):
    docker ps q | xargs I {} sh c ‘echo “Container {}:”; docker top {} | grep “nginx”‘

  3. This loops through all containers and uses docker top to list their processes, then filters for the target program.

Method 2: Find the Program’s PID on the Host and Map to a Container

  1. Find the PID of the program on the Docker host:
    pgrep a nginx  # Replace “nginx” with your program name

  2. Check the cgroup of the PID to extract the container ID:
    cat /proc/<PID>/cgroup | grep o E “docker-[0-9a-f]{64}”
    Example:
    cat /proc/1234/cgroup | grep o E “docker-[0-9a-f]{64}”

  3. Match the container ID to a running container:
    docker ps notrunc | grep “<container-id>”

Method 3: Automated Script to Check All Containers

Use this script to loop through containers and check for the program:

Example Output

Key Notes:

These methods help identify which container is running a specific program without needing access to the container’s internal tools.