Docker

Docker Cheat Sheet

## List Docker CLI commands
docker
docker container --help

## Display Docker version and info
docker --version
docker version
docker info

## Execute Docker image
docker run hello-world

## List Docker images
docker image ls

## List Docker containers (running, all, all in quiet mode)
docker container ls
docker container ls --all
docker container ls -aq

# -------------------------------------

# Create image using this directory's Dockerfile
docker build -t friendlyhello .

# Run "friendlyname" mapping port 4000 to 80
docker run -p 4000:80 friendlyhello

# Same thing, but in detached mode
docker run -d -p 4000:80 friendlyhello

# List all running containers
docker container ls

# List all containers, even those not running
docker container ls -a

# Gracefully stop the specified container
docker container stop <hash>

# Force shutdown of the specified container
docker container kill <hash>

# Remove specified container from this machine
docker container rm <hash>

# Remove all containers
docker container rm $(docker container ls -a -q)

# List all images on this machine
docker image ls -a

# Remove specified image from this machine
docker image rm <image id>

# Remove all images from this machine
docker image rm $(docker image ls -a -q)

# Log in this CLI session using your Docker credentials
docker login

# Tag <image> for upload to registry
docker tag <image> username/repository:tag

# Upload tagged image to registry
docker push username/repository:tag

# Run image from a registry
docker run username/repository:tag

# -------------------------------------

# List stacks or apps
docker stack ls

# Run the specified Compose file
docker stack deploy -c <composefile> <appname>

# List running services associated with an app
docker service List

# List tasks associated with an app
docker service ps <service>

# Inspect task or container
docker inspect <task or container>

# List container IDs
docker container ls -q

# Tear down an application
docker stack rm <appname>

# Take down a single node swarm from the manager
docker swarm leave --force

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

Docker Version

$ docker --version

  # 'Docker version 17.12.0-ce, build c97c6d6'
1
2
3

Docker Info

To view even more details about your Docker installation:

$ docker info

  # Containers: 0
  #   Running: 0
  #   Paused: 0
  #   Stopped: 0
  # Images: 0
  # Server Version: 17.12.0-ce
  # Storage Driver: overlay2
  # ...
1
2
3
4
5
6
7
8
9
10

Test Docker installation

To test that your installation works

$ docker run hello-world

  # Unable to find image 'hello-world:latest' locally
  # latest: Pulling from library/hello-world
  # ca4f61b1923c: Pull complete
  # Digest: sha256:ca0eeb6fb05351dfc8759c20733c91def84cb8007aa89a5bf606bc8b315b9fc7
  # Status: Downloaded newer image for hello-world:latest
  # 
  # Hello from Docker!
  # This message shows that your installation appears to be working correctly.
  # ...
1
2
3
4
5
6
7
8
9
10
11

Docker Images - List

List the downloaded images available on your machine:

$ docker image ls
1