0

I want to make a script that assesses Docker infrastructure and documents processes that are running there.

The first method I use is: docker inspect <container_id> | jq '.[].Args' and grep processes from there (php-fpm for example).

The second is: docker top <container_id> | awk '{$1=$2=$3=$4=$5=$6=$7=""; print $0}' | sed 's/^\s*//g'

Commands after pipeline help to build output for parsing.

What are other methods of obtaining information on Docker infrastructure?

1
  • 1
    It's not really clear what you want. The best way to find processes running inside a container is to get the Pid of the container (docker container inspect <container_id> -f docker container inspect uefibooter-http-1 -f '{{.State.Pid}}') and then look for processes that have the pid as their parent (and so forth recursively) (or use a tool like pstree).
    – larsks
    Commented Jun 17 at 21:00

1 Answer 1

0
#!/bin/bash

# Function to inspect a container
inspect_container() {
  local container_id=$1
  echo "Inspecting container: $container_id"
  
  echo "Container Configuration:"
  docker inspect "$container_id" | jq '.[].Config'
  
  echo "Container State:"
  docker inspect "$container_id" | jq '.[].State'
  
  echo "Container Network Settings:"
  docker inspect "$container_id" | jq '.[].NetworkSettings'
  
  echo "Container Processes:"
  docker top "$container_id" | awk '{$1=$2=$3=$4=$5=$6=$7=""; print $0}' | sed 's/^\s*//g'
  
  echo "Container Logs:"
  docker logs "$container_id" | tail -n 20  # Show last 20 lines of logs
}

# List all containers and inspect each one
containers=$(docker ps -q)
for container in $containers; do
  inspect_container "$container"
done
New contributor
warashi nguyen is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .