1
Docker Commands
Docker Commands
Dictionary
Contents
Installation & Orchestration ............................................................................................................. 2
Monitoring state of container........................................................................................................... 2
Docker Networking ........................................................................................................................... 3
Docker Images .................................................................................................................................. 3
Docker Build using Dockerfile ........................................................................................................... 4
Docker commit ................................................................................................................................. 4
Docker Push to Docker Hub .............................................................................................................. 4
Docker Storage volumes ................................................................................................................... 4
Docker Tmpfs volume ....................................................................................................................... 5
Docker Compose ............................................................................................................................... 5
Docker Swarm................................................................................................................................... 5
Docker services ................................................................................................................................. 6
Docker Secrets .................................................................................................................................. 7
Docker Overlay networks ................................................................................................................. 7
Docker Stacks.................................................................................................................................... 7
Docker Universal Control Plane (UCP) .............................................................................................. 7
DTR ................................................................................................................................................... 8
2
Docker Commands
Installation & Orchestration
Install docker – sudo apt install docker.io
Start docker – systemctl start docker
Enable docker - systemctl enable docker
Pull image from registry – docker pull <image name e.g. nginx>
List images on docker host - docker images ls
Create container with an image – docker run -d –name <container name> <image name>
List containers – docker ps
Creating image – docker build <dockerfile>
Updating image – docker commit <>
Check docker status & all information – docker info
Current user name - whoami
Running a container – docker run -it <interactive> <image name>
Running container in detached mode – docker run -d <image name>
Create container – docker create <image name>
Start container – docker start <container id>
Restart container – docker restart <container id>
Stop container – docker stop <container id>
Monitoring state of container
Consumption – docker top <container id> OR <container name>
Statistics – docker stats <container id>
Complete information – docker inspect <container id>
Delete container – docker rm <container id>
3
Docker Commands
Docker Networking
Show IP address and all network information – ip add show (ifconfig, now deprecated in
some OS)
Check docker bridge – docker network inspect bridge
Check docker host – docker network inspect host
Add container on the host – docker run -itd –name nginx1 –net=host nginx
Inspect container – docker inspect <container id> or <container name>
Add new custom network – docker network create -driver=bridge new_bridge1
Remove network from container – docker network disconnect <network
type…bridge,host,none> <container id> or <container name>
Update network for container – docker network connect <new network type…bridge,
none> <container id> or <container name>
Remove network – docker network rm <custom network name>
Remove all custom network which are not in use – docker network prune
Docker Images
List Docker images – docker image ls
List docker images with full details – docker image ls --no-trunc
List images from specific repository – docker image ls <repo name, e.g. nginx>
List images created before specific image – docker image ls –filter ‘before=<image id>’
List images created after specific image – docker image ls –filter ‘after=<image id>’
List images in specific formatted output - docker image ls --format "{{.ID}} - {{.Repository}}"
Delete image – docker image rm <image id>
Delete image using tag – docker image rm <repository:tag> (ubuntu:18.10)
Delete multiple images – docker image rm <image1 id> <image2 id> <reportsitory:tag>
4
Docker Commands
Docker Build using Dockerfile
Building image using Dockerfile – docker build -t <new image name> <dockerfile path>
Running a container using new image – docker run -d --name <container name> <image
name>
Docker commit
Download an image (docker pull), create a container with that image, do ‘docker exec…’ to
connect to the container BASH, update the container (download some software, run apt-get
update)
Commit the changes done to the container to a new image – docker container commit
<container name> <new image name>
Docker commit with parameters – docker container commit –message=”test” –
author=”test” <container name> <new image name>
Docker Push to Docker Hub
Login with Docker hub credentials – docker login -u <username> and then … <password>
Tag the image with username/repo name – docker tag <image name> <user
name>/<repository name>
Push the image to Docker hub – docker push <user name>/<repository name>
Docker Storage volumes
Check size of each running container – docker ps -s
Check disk usage by all objects (images, containers, local volumes, build cache) – docker
system df
Check disk usage – verbose – docker system df -v
5
Docker Commands
Create new volume – docker volume create <volume name>
Create new container with volume parameter – docker run -itd --volume <volume
name>:<default data path> <image name>
List volumes - docker volume ls
Inspect volume – docker volume inspect <volume name>
Remove docker volume – docker volume rm <volume name>
Creating container with new volume – docker run -itd –volume <volume name>:<test
path> <image name>
Creating container with multiple mounts/storage volumes - docker run -itd --mount
source=test_vol2,target=/app2 --mount source=test_vol3,target=/app3 nginx
Remove docker volume – docker volume rm <volume name>
Remove all volumes – docker volume prune
Docker Tmpfs volume
Create container with tmpfs volume - docker run -itd --tmpfs <temporary volume path>(/app)
<image name>
Create container with tmpfs volume using ‘mount’ flag - docker run -itd --mount
type=tmpfs,target=<target volume path>(/app) <image name>
Docker Compose
Docker compose using docker-compose.yml – docker-compose up
Shutting down services – docker-compose down
Docker Swarm
Convert docker host to swarm manager – docker swarm init
Generate token to join the swarm as manager - docker swarm join-token manager
6
Docker Commands
Generate worker token to join the swarm – docker swarm join-token worker
View swarm information (managers, workers) – docker info
To change the token for manager nodes – docker swarm join-token –rotate manager
To change the token for worker nodes – docker swarm join-token –rotate worker
To view the list of managers & worker nodes (from Manager node only) – docker node ls
Add labels to managers / workers – docker node update –label-add <label> <manager
name>/<worker name>
Promote a worker to manager – docker swarm promote <worker name>
Demote a manager to worker – docker swarm demote <manager name>
To make a worker leave the swarm – docker swarm leave
Lock swarm manager from restarting – docker swarm update –autolock=true
Unlock manager – docker swarm unlock
Docker services
Create a new service – docker service create –replicas <no of replicas> --name <name of
service> <image name>
Scale service – docker service scale <service name>=<number of nodes> <service name>
Create service with volume mount – docker service create –replicas<no of replicas> --
mount type=volume,source=<source folder>,target=<target folder path> --name <name of
service> <image name>
Remove mount volume from all nodes in swarm – docker service update --mount-rm
/<folder name> <service name>
Create service with specific image version – docker service create –name <service name>
<image name>:<version tag>
Update the image version of existing service on the swarm – docker service update – image
< image name>:<updated version tag> <service name>
Stopping a worker node from Manager – docker node update --availability drain <worker
name>
Starting a worker node from Manager - docker node update --availability active <worker
name>
Starting a service and publishing it on a particular port(TCP) - docker service create --
replicas 2 --publish published=8000,target=80 --name portsrv nginx
7
Docker Commands
Creating service and assigning specific hostname to nodes - docker service create --name
hostsrv --replicas 2 --hostname="{{.Node.ID}}-{{.Service.Name}}" nginx
Docker Secrets
Create a new secret using file – docker secret create <secret name> <secret file>
Associating secrets with Services - docker service create --name secng --replicas 3 --secret
secret1 nginx
Docker Overlay networks
Create an overlay network – docker network create --driver=overlay --subnet=172.0.0.1/16
myovnw
Associating custom overlay network with a service - docker service create --replicas 3 --
network myovnw --name overlaysrv nginx
Docker Stacks
Create a docker-compose.yml as follows:
version: "3"services: web: image: nginx deploy: replicas: 5 ports: - "80:80" mongo: image: mongo
deploy: placement: constraints: [node.role == manager]
Create a docker stack using the above docker-compose.yml file - docker stack deploy -c docker-
compose.yml mystack
Above deployment will create two service, Mongo DB running on Manager node only and nginx web
service running on all nodes.
Docker Universal Control Plane (UCP)
UCP Installation steps (ubuntu 19.04 disco):
1) apt-get update
2) Download containerd package - curl -O
https://download.docker.com/linux/ubuntu/dists/disco/pool/stable/amd64/containerd.i
o_1.2.6-3_amd64.deb
8
Docker Commands
3) Download Docker CE CLI - curl -O
https://download.docker.com/linux/ubuntu/dists/disco/pool/stable/amd64/docker-ce-
cli_19.03.3~3-0~ubuntu-disco_amd64.deb
4) Download Docker CE - curl -O
https://download.docker.com/linux/ubuntu/dists/disco/pool/stable/amd64/docker-
ce_19.03.3~3-0~ubuntu-disco_amd64.deb
5) Install the 3 packages using “dpkg -I” command as:
a. dpkg -I containerd.io_1.2.6-3_amd64.deb
b. dpkg -I docker-ce-cli_19.03.3~3-0~ubuntu-disco_amd64.deb
c. dpkg -I docker-ce_19.03.3~3-0~ubuntu-disco_amd64.deb
6) Install UCP - docker container run --rm -it --name ucp -v
/var/run/docker.sock:/var/run/docker.sock docker/ucp:3.1.6 install --interactive --force-
minimums
7) Access UCP on the external IP of the swarm manager/worker using Admin credentials (e.g.
root)
For running UCP, license key is required. Download licence key from Docker Hub using docker
hub credentials.
DTR
Installing Docker Trusted Registry on a worker node - docker run -it --rm docker/dtr install --dtr-
external-url https://<Worker node IP/FQDN> --ucp-node worker1 --ucp-username <admin user
name> --ucp-url https://<Manager node IP/FQDN> --ucp-insecure-tls