Docker cheatsheet

Version: 19.03

build image and set tag -t

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

multiple tags are allowed as well

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

build image and see commands output

For debugging issues. E.g., run RUN ls and see out put

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

tag image

docker tag [localImage] [registryServer]/[imageName]:[version]
# ej 
docker tag bustroker.notes.webui:v1

add tag to existing tagged image

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

list images

docker images

remove all images

optionally add -f at the end to force

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

run container

docker run --rm -d `
--network host `
-v /host/directory:/container/directory `

–rm => remove the container on exit -d => run and detach the console (run as daemon) -p => port map, i.e., make a port in the container visible/accessible from the host –network host => from network perspective, it’s like the app is running directly in the host, so just curl http://localhost in the port the actual app is listening. This is not supported when running docker on Windows -e => set environment variables in the container -v => map folders from the host to the container. (Also to create volumes)

get container logs

docker logs --follow <CONTAINER>

run container and bash inside

docker run -it <IMAGE-NAME> bash

run and map host folder to container folder

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

run bash inside a running container

docker exec -it <containerId> bash

list containers

docker ps

stop all containers

docker stop $(docker ps -a -q)

remove all containers

docker rm $(docker ps -a -q)

push image to container registry

docker build . -t -t
docker login  
# OR
docker login --username=$DOCKER_USER --password=$DOCKER_PASS

docker push --all-tags

copy file from docker container to host and viceversa

The container cannot be running, so stop it first

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

To copy from host to container, the same

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

They both copy the folder to target folder. Add / at the end to signify it’s a folder. Target folder needs to exist already. Do not prepend c: to path in windows, as it fails. Also, use double quotes rather than one, as it fails as well (in windows at least)

increase container hdd space

docker run --storage-opt dm.basesize=40G  hello-world
docker run --storage-opt "size=40GB" cmd

or change config file (see config file in docker for windows below)

    "storage-opts": [

Make sure to restart docker (restart didn’t work for me. Had to stop and then start.)

To test it, run the container, open a powershell session and go:

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

config file in docker for windows

The configuration file is located in C:\ProgramData\Docker\config\daemon.json. It can also be edited using Docker for Desktop.

The rest of available params are described here.

docker for windows fails with hns error

Run powershell as admin

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

connect to host’s port

From inside docker, host’s network can be accessed through host.docker.internal host name. Accesing an API on port 3000 running in the host, from a docker container would be:

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