Docker Command Reference

Version: 19.03

Building Images

Build with single tag:

docker build -t bustroker.notes.webui:v1 .

Build with multiple tags:

docker build -t bustroker.notes.webui:v1 -t bustroker.notes.webui:latest .

Debug build by viewing command output:

docker build -t bustroker.notes.webui --progress=plain --no-cache .

Tagging Images

Tag for registry:

docker tag [localImage] [registryServer]/[imageName]:[version]
## example
docker tag bustroker.notes.webui:v1 bustrokeracr.azurecr.io/bustroker.notes.webui:v1

Add tag to existing image:

docker tag bustroker.notes.webui:v1 bustroker.notes.webui:latest

Managing Images

List images:

docker images

Remove all images (add -f to force):

## Linux
docker rmi $(docker images -q)

## Windows (PowerShell)
docker images -a -q | % { docker image rm $_ -f }

Running Containers

docker run --rm -d `
-p [HOST_PORT]:[CONTAINER_PORT] `
--network host `
-e [ENV_VAR_NAME]=[ENV_VAR_VALUE] `
-v /host/directory:/container/directory `
[IMAGE_NAME]

Flags:

Run with interactive bash:

docker run -it <IMAGE-NAME> bash

Run with volume mount:

docker run -v /host/directory:/container/directory <IMAGE-NAME>

Working with Running Containers

List containers:

docker ps

View logs:

docker logs --follow <CONTAINER>

Execute bash in running container:

docker exec -it <containerId> bash

Stop all containers:

docker stop $(docker ps -a -q)

Remove all containers:

## Linux
docker rm $(docker ps -a -q)

Copying Files

Container must be stopped first:

docker stop [CONTAINER_ID]
docker cp [CONTAINER_ID]:/file/path/within/container /host/path/target

Copy from host to container:

docker cp /host/path/to/folder/ [CONTAINER_ID]:/folder/path/within/container/

Notes:

Pushing to Registry

Build with registry tag:

docker build . -t bustrokeracr.azurecr.io/bustroker-webui:v1 -t bustrokeracr.azurecr.io/bustroker-webui:latest

Login and push (enable Admin user in ACR Access keys):

docker login bustrokeracr.azurecr.io
## OR
docker login --username=$DOCKER_USER --password=$DOCKER_PASS bustrokeracr.azurecr.io

## THEN
docker push --all-tags bustrokeracr.azurecr.io/bustroker-webui

AWS ECR Login

aws sso login --profile PROFILE
aws ecr get-login-password --region REGION --profile PROFILE | docker login --username AWS --password-stdin ECR

## example
aws sso login --profile aws-infrastructure
aws ecr get-login-password --region eu-central-1 --profile aws-infrastructure | docker login --username AWS --password-stdin 258781458051.dkr.ecr.eu-central-1.amazonaws.com

Storage Configuration

Increase container disk space:

## Linux
docker run --storage-opt dm.basesize=40G hello-world

## Windows
docker run --storage-opt "size=40GB" mcr.microsoft.com/windows/servercore:ltsc2019 cmd

Or configure via C:\ProgramData\Docker\config\daemon.json:

{
    "storage-opts": [
        "size=40GB"
    ]
}

Restart Docker after config change. Verify with:

Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '3'"

See Docker daemon configuration for all options.

Troubleshooting

Docker for Windows HNS Error

Stop-Service docker
Stop-service hns
Start-service hns
Start-Service docker
docker network prune

Access Host Network from Container

Use host.docker.internal hostname:

docker exec -it [CONTAINER_NAME] /bin/bash
curl host.docker.internal:3000