DEV Community

Cover image for Most Commonly Used Docker Commands
Ashutosh Sharma
Ashutosh Sharma

Posted on • Edited on

Most Commonly Used Docker Commands

Most used Docker Commands

Check the version of the installed docker version.

$ **docker --version**
Docker version 18.09.7, build 2d0083d

$ **docker -v**
Docker version 18.09.7, build 2d0083d
Enter fullscreen mode Exit fullscreen mode

Check the running containers:

$ **docker ps**
CONTAINER ID        IMAGE               COMMAND             CREATED STATUS              PORTS               NAMES
Enter fullscreen mode Exit fullscreen mode

Check the available images:

$ **docker images**
REPOSITORY        TAG     IMAGE ID     CREATED             SIZE
redis             latest  857c4ab5f029 10 months ago       98.2MB
weaveworks/scope  1.11.4  a082d48f0b39 10 months ago       78.5MB
ubuntu            latest  3556258649b2 11 months ago       64.2MB
alpine            latest  b7b28af77ffe 11 months ago       5.58MB
Enter fullscreen mode Exit fullscreen mode

Run a container using the redis image

$ **docker run redis**
1:C 21 Jun 2020 18:12:08.070 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 21 Jun 2020 18:12:08.070 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 21 Jun 2020 18:12:08.070 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 21 Jun 2020 18:12:08.072 * Running mode=standalone, port=6379.
1:M 21 Jun 2020 18:12:08.072 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 21 Jun 2020 18:12:08.072 # Server initialized
1:M 21 Jun 2020 18:12:08.072 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 21 Jun 2020 18:12:08.072 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted afterTHP is disabled.
1:M 21 Jun 2020 18:12:08.072 * Ready to accept connections
Enter fullscreen mode Exit fullscreen mode

Stop the container using Ctrl+C

Run a container using the redis image in background named redis-app

$ **docker run -d --name redis-app redis**
3749e501b0d591889b1867b2e8a17e17ccf2797a493f475092088f10a83ba957
Enter fullscreen mode Exit fullscreen mode

Stop the container using the name

$ **docker ps**
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
3749e501b0d5        redis               "docker-entrypoint.s…"   31 seconds ago      Up 30 seconds       6379/tcp            redis-app
$ docker stop redis-app
redis-app
Enter fullscreen mode Exit fullscreen mode

You can also stop the container by its ID also.

docker stop ID

ID doesn’t need to be required to be complete. The first few unique characters are also enough as ID.

$ **docker ps**
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
67cef909310c        redis               "docker-entrypoint.s…"   8 seconds ago       Up 6 seconds        6379/tcp            dreamy_lalande
$ **docker stop 6**
6
Enter fullscreen mode Exit fullscreen mode

List all the containers running and stopped.

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                      PORTS               NAMES
67cef909310c        redis               "docker-entrypoint.s…"   About a minute ago   Exited (0) 53 seconds ago                       dreamy_lalande
3749e501b0d5        redis               "docker-entrypoint.s…"   6 minutes ago        Exited (0) 5 minutes ago                        redis-app
Enter fullscreen mode Exit fullscreen mode

Remove stopped container by NAME or ID

$ docker rm redis-app
redis-app
$ docker rm 6
6
Enter fullscreen mode Exit fullscreen mode

List all the images available in local repository

$ docker images
REPOSITORY       TAG     IMAGE ID     CREATED             SIZE
redis            latest  857c4ab5f029 10 months ago       98.2MB
weaveworks/scope 1.11.4  a082d48f0b39 10 months ago       78.5MB
ubuntu           latest  3556258649b2 11 months ago       64.2MB
alpine           latest  b7b28af77ffe 11 months ago       5.58MB
Enter fullscreen mode Exit fullscreen mode

Pull an image busybox from docker hub

$ docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
76df9210b28c: Pull complete
Digest: sha256:95cf004f559831017cdf4628aaf1bb30133677be8702a8c5f2994629f637a209
Status: Downloaded newer image for busybox:latest
Enter fullscreen mode Exit fullscreen mode

Delete the image busybox

$ docker rmi busybox
Untagged: busybox:latest
Untagged: busybox@sha256:95cf004f559831017cdf4628aaf1bb30133677be8702a8c5f2994629f637a209
Deleted: sha256:1c35c441208254cb7c3844ba95a96485388cef9ccc0646d562c7fc026e04c807
Deleted: sha256:1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569
Enter fullscreen mode Exit fullscreen mode

Try all these scenario based tutorial on interactive UI:

Docker Basic Commands Scenario | ashusharma | Katacoda

Katacode Screenshot

To add new scenarios, please raise a pull request on repository:
ashusharmatech/katacoda-scenarios

Top comments (0)