Last Updated: September 30, 2021
·
1.387K
· stefanprodan

Docker for Windows cheat sheet

These scripts are compatible with Docker 1.12 and Docker for Windows.

I keep this cheat sheet updated on my blog here.

Enable swarm mode:

docker swarm init

Create a custom overlay network:

$network = "network_name"
docker network create --driver overlay $network

Create and start a service using a custom overlay network:

$service = "service_name"
$network = "network_name"
$image = "image_name"
$replicas = 5
docker service create --name $service --network $network --replicas $replicas $image

Start a service and expose a port on the host system:

$service = "service_name"
$network = "network_name"
$image = "image_name"
$hostPort = 8080
$servicePort = 80
docker service create --name $service --network $network --publish ${hostPort}:${servicePort} $image

# test port
Start-Sleep -s 5
Test-NetConnection -ComputerName "10.0.75.2" -Port $hostPort -InformationLevel "Detailed"

Start a service and mount a local system directory:

$service = "service_name"
$image = "image_name"
$hostDir = "/c/users/docker/data"
$serviceDir = "/data"
docker service create --mount type=bind,src=$hostDir,dst=$serviceDir --name $service $image

List all matching services:

$service = "service_name_part"
docker service ls --filter name=$service

Get CPU, memory, network and IO statistics for all running instances of a swarm service:

$service = "service_name"
docker stats $(docker ps -q -f "name=$service") --no-stream 

Get the virtual IP list of all running instances of a swarm service:

$service = "service_name"
docker inspect --format='{{.Name}}{{range .NetworkSettings.Networks}} IP: {{.IPAMConfig.IPv4Address}} {{end}}' $(docker ps -q -f "name=$service")

Get the virtual IP of all running instances of a swarm service for a specific overlay network:

$service = "service_name"
$network = "network_name"
$template = '{{.Name}}{{with index .NetworkSettings.Networks \"' + $network + '\"}} {{.IPAddress}}{{end}}'
docker inspect -f $template $(docker ps -q -f "name=$service")

Get the volume list of all running instances of a swarm service:

$service = "service_name"
docker inspect --format '{{.Name}}{{range .Mounts}} Source: {{.Source}} Destination: {{.Destination}}{{end}}' $(docker ps -q -f "name=$service")

Remove all instances of a swarm service that aren't currently running:

$service = "service_name"
docker rm $(docker ps -a -q -f "name=$service" -f "status=exited")

Update a service:

$service = "service_name"
$image = "image_name"
docker service update --image $image $service

Stop a service:

$service = "service_name"
docker service scale ${service}=0

Remove a service:

$service = "service_name"
docker service rm $service

Remove all stopped containers:

docker rm $(docker ps -a -q -f "status=exited")

Remove untagged images:

docker rmi $(docker images -q -f "dangling=true")

Remove orphaned volumes:

docker volume rm $(docker volume ls -q -f "dangling=true")

Stop and remove all containers by image name or image ID:

$image = "image_name"
docker rm $(docker stop $(docker ps -a -q -f "ancestor=$image"))