Parsing docker inspect and Docker JSON Output
docker inspect, docker ps, docker image ls and docker compose ps can all print JSON. fx lets you browse it or pull out one field with a short JavaScript expression. Install fx and pipe Docker's output into it.
Explore docker inspect Output Interactively
Pipe docker inspect into fx to open the interactive viewer:
docker inspect shop-db-1 | fxCollapse and expand nodes with arrow or Vim keys. Press @ to fuzzy-search paths (type IPAddress), / to search values with a regex, and P to print the selected value to stdout. More in the TUI guide.
docker inspect: Get Container IP Address
docker inspect always returns an array, even for one container, so start with .[0]:
docker inspect shop-db-1 | fx '.[0].NetworkSettings.Networks.shop_default.IPAddress'Input (trimmed):
[
{
"Name": "/shop-db-1",
"NetworkSettings": {
"Networks": {
"shop_default": {
"IPAddress": "172.19.0.2"
}
}
}
}
]Output:
172.19.0.2Without knowing the network name, take the first one. Works for many containers, e.g. docker inspect $(docker ps -q):
docker inspect shop-db-1 | fx '@{name: x.Name.slice(1), ip: Object.values(x.NetworkSettings.Networks)[0].IPAddress}'[
{ "name": "shop-db-1", "ip": "172.19.0.2" }
]List Published Ports of a Container
NetworkSettings.Ports maps each container port to its host bindings. Unpublished ports have null bindings:
docker inspect shop-db-1 | fx '.[0].NetworkSettings.Ports' 'Object.entries(x).flatMap(([port, binds]) => (binds || []).map(b => `${b.HostIp}:${b.HostPort} -> ${port}`))'Output:
[
"0.0.0.0:5433 -> 5432/tcp",
":::5433 -> 5432/tcp"
]Show Container Mounts and Volumes
Print one line per entry in Mounts:
docker inspect shop-db-1 | fx '.[0].Mounts' '@`${x.Type}\t${x.Source} -> ${x.Destination} (${x.RW ? "rw" : "ro"})`' listOutput:
volume /var/lib/docker/volumes/shop_pgdata/_data -> /var/lib/postgresql/data (rw)
bind /home/deploy/shop/init.sql -> /docker-entrypoint-initdb.d/init.sql (ro)Convert Docker Env Vars to a Key/Value Object
Config.Env is an array of KEY=value strings. Split each one on the first = to get an object:
docker inspect shop-db-1 | fx '.[0].Config.Env' 'x => Object.fromEntries(x.map(e => e.split(/=(.*)/s)))'Output:
{
"POSTGRES_USER": "shop",
"POSTGRES_DB": "shop",
"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"PG_MAJOR": "16",
"PGDATA": "/var/lib/postgresql/data"
}Add '.PGDATA' as another argument to read one variable. To filter the raw strings instead, use '?.startsWith("POSTGRES_")'.
Read Container Labels and Health Status
Labels are a plain object under Config.Labels. Keys contain dots, so use bracket access:
docker inspect shop-db-1 | fx '.[0].Config.Labels["com.docker.compose.service"]'dbThe healthcheck result is in State.Health. Log holds the last few probe runs:
docker inspect shop-db-1 | fx '.[0].State.Health' '{status: x.Status, streak: x.FailingStreak, last: x.Log.at(-1).Output.trim()}'{
"status": "healthy",
"streak": 0,
"last": "/var/run/postgresql:5432 - accepting connections"
}Filter docker ps JSON Output by Status or Image
docker ps -a --format '{{json .}}' prints one JSON object per line. fx processes each line separately (streaming mode):
docker ps -a --format '{{json .}}' | fx 'x.State === "running" ? x.Names : skip'Input (one of five lines):
{"ID":"c4e7a9b1d2f3","Image":"shop/api:1.8.0","Labels":"com.docker.compose.project=shop,com.docker.compose.service=api","Names":"shop-api-1","Ports":"3000/tcp","State":"restarting","Status":"Restarting (1) 12 seconds ago"}Output:
shop-db-1
shop-web-1
shop-worker-1skip prints nothing for that line. Or use -s to collect all lines into an array and filter it with ?, here by image:
docker ps -a --format '{{json .}}' | fx -s '?.Image.startsWith("shop/")' '@.Names' listshop-api-1
shop-worker-1docker ps has no health field, but the Status text contains it. To find unhealthy containers, use '?.Status.includes("(unhealthy)")'.
Sort Docker Images by Size
docker image ls --format '{{json .}}' reports Size as text like "1.12GB" or "47.9MB". Convert it to bytes, then sort with sortBy:
docker image ls --format '{{json .}}' | fx -s 'x => {
const units = {B: 1, kB: 1e3, MB: 1e6, GB: 1e9}
const bytes = s => parseFloat(s) * units[s.replace(/[\d.]/g, "")]
return x.map(i => ({image: `${i.Repository}:${i.Tag}`, size: i.Size, bytes: bytes(i.Size)}))
}' 'sortBy(x => -x.bytes)' '@`${x.size}\t${x.image}`' listOutput:
1.12GB shop/api:1.8.0
980MB <none>:<none>
435MB postgres:16
212MB shop/worker:1.8.0
47.9MB nginx:1.27-alpine
41.4MB redis:7-alpineParse docker compose ps --format json
docker compose ps --format json prints one object per service with Service, State, Health and a structured Publishers array. Map each service to its health, or to its state if it has no healthcheck:
docker compose ps -a --format json | fx -s '.flat()' 'x => Object.fromEntries(x.map(c => [c.Service, c.Health || c.State]))'Output:
{
"api": "restarting",
"db": "healthy",
"web": "running",
"worker": "unhealthy"
}INFO
Docker Compose 2.21 and newer print one JSON object per line. Older versions print a single JSON array. With -s '.flat()' the same command works for both.
List published ports per service. Publishers is null for services without ports, and PublishedPort is 0 for ports that are exposed but not published:
docker compose ps --format json | fx -s '.flat()' '.flatMap(x => (x.Publishers || []).map(p => ({service: x.Service, ...p})))' '?.PublishedPort > 0' '@`${x.service}\t${x.URL}:${x.PublishedPort} -> ${x.TargetPort}/${x.Protocol}`' listdb 0.0.0.0:5433 -> 5432/tcp
web 0.0.0.0:8080 -> 80/tcp