operations

reset docker..

docker rm -f $(docker ps -aq) 2>/dev/null || true;
docker image rm -f $(docker image ls -aq) 2>/dev/null || true;
docker volume rm -f $(docker volume ls -q) 2>/dev/null || true;
docker network rm -f $(docker network ls -q) 2>/dev/null || true;
docker builder prune -af 2>/dev/null || true;
docker buildx prune -af 2>/dev/null || true

view information about a container image..

skopeo inspect --config docker://docker.io/library/nginx:1.31.8

skopeo inspect --config \
  --override-os linux \
  --override-arch arm64 \
  docker://docker.io/library/nginx:1.31.8

get the digest of a container image..

docker buildx imagetools inspect alpine:3.24.4 --format '{{json .Manifest}}' | jq -r '.digest'

copy single file to a running container..

docker run -d --name web nginx
echo "hello!" > index.html
docker cp index.html web:/usr/share/nginx/html/index.html
docker exec web curl localhost

copy multiple files to a running container..

docker run -d --name web nginx
for file in ./*; do
  docker cp "${file}" web:/usr/share/nginx/html/
done
docker exec web ls /usr/share/nginx/html/

check how much storage docker uses..

docker system df

run stress-ng on a container..

docker run --rm --name stress-ng debian:stable bash -c "apt update && apt install -y stress-ng && stress-ng --cpu 2 --timeout 1h"

change the default behavior to publish containers on all interfaces..

cat << 'EOF' > /etc/docker/daemon.json
{
  "ip": "127.0.0.1"
}
EOF

enable the metrics endpoint for the docker daemon..

cat << 'EOF' > /etc/docker/daemon.json
{
  "metrics-addr": "0.0.0.0:9323"
}
EOF

access the filesystem of a container from the host..

CONTAINER_NAME="web"
CONTAINER_PID=$(docker inspect ${CONTAINER_NAME} --format "{{.State.Pid}}")

ls /proc/${CONTAINER_PID}/root/

run a container with the id and group of the host user..

docker run --rm debian:latest id
docker run --rm --user $(id -u):$(id -g) debian:latest id

run a container in the namespace of another container..

docker run -d --name c1 ubuntu sh -c 'sleep 1d'
docker run -d --name c2 ubuntu sh -c 'sleep 2d'

docker exec c1 ps aux | grep sleep
docker exec c2 ps aux | grep sleep

docker rm -f c2
docker run -d --name c2 --pid=container:c1 ubuntu sh -c 'sleep 2d'
docker exec c2 ps aux | grep sleep

get the veth of a running container..

CONTAINER_NAME="web"
CONTAINER_PID=$(docker inspect ${CONTAINER_NAME} --format "{{.State.Pid}}")

cat /proc/${CONTAINER_PID}/net/igmp # get the Idx of eth0
ip -br a # match the Idx on the veth interface
tcpdump -i veth97cad91

use netshoot on a running container..

docker run \
  --detach \
  --name web \
  --publish 0.0.0.0:3123:80 \
  nginx:latest

docker run \
  --rm \
  --interactive \
  --tty \
  --name netshoot \
  --network container:web \
  nicolaka/netshoot:latest
tcpdump -i eth0

use a regular debian image instead of netshoot on a running container..

docker run \
  --detach \
  --name web \
  --publish 0.0.0.0:3123:80 \
  nginx:latest

docker run \
  --rm \
  --interactive \
  --tty \
  --name tcpdump \
  --network container:web \
  debian:latest \
  /bin/bash -c "apt update && apt install -y tcpdump && tcpdump -i eth0"

use busybox utilities to troubleshoot minimal containers that lack common tools..

CONTAINER_NAME="test"

docker run -d --name ${CONTAINER_NAME} nginx

# method 1
docker run --rm busybox:latest cat /bin/busybox | \
  docker exec -i ${CONTAINER_NAME} sh -c 'cat > /tmp/busybox && chmod +x /tmp/busybox'

# method 2
docker cp $(docker create --rm busybox:latest):/bin/busybox /tmp/busybox && \
  docker cp /tmp/busybox ${CONTAINER_NAME}:/tmp/busybox && \
  rm /tmp/busybox

docker exec ${CONTAINER_NAME} /tmp/busybox

build and run an image with docker compose..

services:
  ${SERVICE}:
    build:
      args: {}
      context: .
      dockerfile_inline: |
        FROM debian:latest
        WORKDIR /app
        CMD ["sleep", "30"]
    image: ${IMAGE}:${TAG}
docker compose up --build

docker-socker-proxy

docker run \
  --detach \
  --privileged \
  --name docker-socket-proxy \
  --volume /var/run/docker.sock:/var/run/docker.sock \
  --publish 127.0.0.1:2375:2375 \
  --env CONTAINERS=1 \
  tecnativa/docker-socket-proxy

testssl.sh

docker run -it --rm drwetter/testssl.sh example.com

trippy

docker run -it --rm fujiapple/trippy example.com