Table of Contents
In this article, we will look into 40 Ultimate Docker command with examples. Docker is an open source software that enables developers to package and run their applications in containerized environment. It utilizes OS-level virtualization to deliver application containerization. Using docker, developers and programmers can built light weight containers which further simplifies the application deployment and testing. We will see some of the ultimate docker real world examples in below section. More on docker official docs.
Synopsis
docker [OPTIONS] COMMAND [ARG...]
docker daemon [--help|...]
docker [--help|-v|--version]
Ultimate Docker Commands with Examples
Also Read: 48 Useful ifconfig command examples for Linux Network Administration
Example 1: How to Check docker command version
If you want to check installed docker version then you need to run docker --version
command as shown below.
[root@localhost ~]# docker --version
Docker version 1.13.1, build 7d71120/1.13.1
Example 2: How to Run a Container
If you want to download a docker image and run container over it then you need to use docker run <image_name>
syntax as shown below. In this example we are downloading debian
image and starting container over it by using docker run debian
command as shown below. This command will pull the latest image from repository if not already available in the system and then it will create and start the container using that image.
[root@localhost ~]# docker run debian
Unable to find image 'debian:latest' locally
Trying to pull repository docker.io/library/debian ...
latest: Pulling from docker.io/library/debian
627b765e08d1: Pull complete
Digest: sha256:cc58a29c333ee594f7624d968123429b26916face46169304f07580644dde6b2
Status: Downloaded newer image for docker.io/debian:latest
Example 3: How to Check all running Containers
You can check all running containers by using docker ps
command as shown below. This command will give important information like container Id, Image name, uptime etc.
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4197e54fa6d8 debian "bash" 4 seconds ago Up 3 seconds romantic_khorana
Example 4: How to Create a Container
If you want to create a container over a docker image and allocate /bin/bash
terminal to it then you need to use docker create -it <image_name> /bin/bash
command as shown below.
[root@localhost ~]# docker create -it debian /bin/bash
a4f0a9ac79e489507bb4603dfc4354127955f555d4d13d4a79a858422d0e7501
Example 5: How to Start and attach STDIN to a Container
If you are looking to start and attach STDIN to a container then you need to use docker start -ai <container_id>
command. In this example we are attaching STDIN to container a4f0a9ac79e4
using docker start -ai a4f0a9ac79e4
command as shown below.
[root@localhost ~]# docker start -ai a4f0a9ac7
root@a4f0a9ac79e4:/#
Example 6: How to List all the docker images
If you want to list all the docker images then you need to use docker images
command as shown below. It is also equivalent to using docker images ls
command.
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest 08b152afcfae 27 hours ago 133 MB
docker.io/debian latest 0980b84bde89 36 hours ago 114 MB
Example 7: How to Check the status of all Containers
If you want to check the status of all the containers then you need to use docker ps -a
command as shown below.
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a4f0a9ac79e4 debian "/bin/bash" 7 minutes ago Exited (0) 4 minutes ago nifty_easley
7fc9795b44de debian "bash" About an hour ago Exited (0) About an hour ago cocky_shockley
d3d36503939d nginx "/docker-entrypoin..." About an hour ago Exited (0) About an hour ago adoring_brahmagupta
6f7b950f4f48 nginx "/docker-entrypoin..." About an hour ago Exited (0) About an hour ago relaxed_curran
Example 8: How to Remove all unused Images
If you have few unused docker images which you want to remove from your system then you can use docker image prune
command as shown below. This will delete all the unused images and will reclaim the free space.
[root@localhost ~]# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] Y
Total reclaimed space: 0 B
Example 9: How to Run Container in Detach Mode
If you want to run a container in detach mode then you need to use docker run -it -d <container_name>
syntax. In this example we are running container using debian image in detach mode by using docker run -it -d debian
command as shown below.
[root@localhost ~]# docker run -it -d debian
4197e54fa6d82f57ddb10f8838c1c524328e20dbf33c791f09258581cdbcc94c
Example 10: How to Stop a Container
If you want to stop a container then you need to use docker stop <container_Id>
command. In this example we are stopping docker container with Id 4197e54fa6d8 using docker stop 4197e54fa6d8
command as shown below.
[root@localhost ~]# docker stop 4197e54fa6d8
4197e54fa6d8
Example 11: How to Remove a Container
If you want to remove a container then you need to use docker rm <container_id>
syntax. In this example we are removing container with ID of 4197e54fa6d8
using docker rm 4197e54fa6d8
command as shown below.
[root@localhost ~]# docker rm 4197e54fa6d8
4197e54fa6d8
Example 12: How to Access a Container
If you want to access the container bash login shell then you need to use docker exec -ti <container_id> bash
command. In this example we are accessing the bash shell of container Id 0ea2b7740fa0
using docker exec -ti 0ea2b7740fa0 bash
command as shown below.
[root@localhost ~]# docker exec -ti 0ea2b7740fa0 bash root@0ea2b7740fa0:/# pwd /
Example 13: How to Start a docker Container
To start a docker container you need to use docker start <container_ID>
syntax. In this example we are starting our container using docker start 0ea2b7740fa0
command as shown below.
[root@localhost ~]# docker start 0ea2b7740fa0
0ea2b7740fa0
Example 14: How to Restart a docker Container
To restart a docker container you can use docker restart <Image_ID>
syntax. In this example we are restarting container with Image ID 0ea2b7740fa0 using docker restart 0ea2b7740fa0
command as shown below.
[root@localhost ~]# docker restart 0ea2b7740fa0
0ea2b7740fa0
Example 15: How to List all the docker network Details
If you want to check the network details of all the containers then you need to use docker network ls
command as shown below.
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
25bb1b9cb758 bridge bridge local
0f2a83ace0cb host host local
9ecf78fda3a4 none null local
Example 16: How to Check Complete Docker Information
If you are looking to check the detailed information about docker then you need to use docker info
command as shown below. This will give you in-depth information about docker.
[root@localhost ~]# docker info
Containers: 5
Running: 0
Paused: 0
Stopped: 5
Images: 2
Server Version: 1.13.1
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: journald
Cgroup Driver: systemd
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Example 17: How to Search a Docker Image
Sometimes you might need to search a docker Image from Docker hub for its availability. This can be done by using docker search <image_name>
syntax. In this example, we are searching nginx Image from Docker hub using docker search nginx
command as shown below.
[root@localhost ~]# docker search nginx
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/nginx Official build of Nginx. 15199 [OK]
docker.io docker.io/jwilder/nginx-proxy Automated Nginx reverse proxy for docker c... 2050 [OK]
docker.io docker.io/richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 816 [OK]
docker.io docker.io/jc21/nginx-proxy-manager Docker container for managing Nginx proxy ... 222
docker.io docker.io/linuxserver/nginx An Nginx container, brought to you by Linu... 149
docker.io docker.io/tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rt... 136 [OK]
docker.io docker.io/jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 124 [OK]
docker.io docker.io/alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from s... 102 [OK]
docker.io docker.io/nginxdemos/hello NGINX webserver that serves a simple page ... 70 [OK]
docker.io docker.io/privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & ... 56 [OK]
Example 18: How to Create a Docker Volume
If you want to create a docker volume then you need to use docker volume create
command as shown below.
[root@localhost ~]# docker volume create
8547fc6de7d059490da10b74e8172f6c8851d3a3ed384c3350b9d409af827032
Example 19: How to List all the Docker Volume
You can use docker volume ls
command to list all the volumes known to docker as shown below.
[root@localhost ~]# docker volume ls
DRIVER VOLUME NAME
local 8547fc6de7d059490da10b74e8172f6c8851d3a3ed384c3350b9d409af827032
Example 20: How to Kill a Container
If you want to immediately remove the container and its processes then you need kill it by using docker kill <container_Id>
syntax. This command is mostly used when you are unable to stop and remove the container in a normal way. In this example we are killing container ID 12576eaaab85
using docker kill 12576eaaab85
command as shown below.
[root@localhost ~]# docker kill 12576eaaab85
12576eaaab85
Example 21: How to Download a docker Image
If you want to only download a docker image then you need to use docker pull <image_name>
command. In this example we are downloading latest ubuntu image by using docker pull ubuntu
command as shown below.
[root@localhost ~]# docker pull ubuntu
Using default tag: latest
Trying to pull repository docker.io/library/ubuntu ...
latest: Pulling from docker.io/library/ubuntu
a31c7b29f4ad: Pull complete
Digest: sha256:b3e2e47d016c08b3396b5ebe06ab0b711c34e7f37b98c9d37abe794b71cea0a2
Status: Downloaded newer image for docker.io/ubuntu:latest
Example 22: How to Save a Image to a Tar Archive
If you want to save an image to a tar archive then you need to use docker save <image_name> > <some_name>.tar
syntax. In this example we are saving debian
image to debian.tar
archive using docker save debian > debian.tar
command as shown below.
[root@localhost ~]# docker save debian > debian.tar [root@localhost ~]# du -sh debian.tar 114M debian.tar
Example 23: How to Check Docker Stats
If you want to check all the container stats then you need to use docker stats
command as shown below. This command will provide you the CPU and memory utilization of all the containers along with the other important information as you can see below.
[root@localhost ~]# docker stats
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
a8119bfd0966 0.00% 452 KiB / 1.795 GiB 0.02% 5.79 kB / 656 B 0 B / 0 B 1
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
a8119bfd0966 0.00% 452 KiB / 1.795 GiB 0.02% 5.79 kB / 656 B 0 B / 0 B 1
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
a8119bfd0966 0.00% 452 KiB / 1.795 GiB 0.02% 5.79 kB / 656 B 0 B / 0 B 1
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
a8119bfd0966 0.00% 452 KiB / 1.795 GiB 0.02% 5.79 kB / 656 B 0 B / 0 B 1
Example 24: How to forcefully delete a docker Image
If you are unable to delete a docker Image normally then you can try to delete it forcefully by using -f
option. In this example we are forcefully deleting docker Image of ID 0980b84bde89
using docker rmi -f 0980b84bde89
command as shown below.
[root@localhost ~]# docker rmi -f 0980b84bde89
Untagged: docker.io/debian:latest
Untagged: docker.io/debian@sha256:cc58a29c333ee594f7624d968123429b26916face46169304f07580644dde6b2
Deleted: sha256:0980b84bde890bbdd5db43522a34b4f7c3c96f4d026527f4a7266f7ee408780d
Example 25: How to Load Image from a Tar File
If you want to load the docker image which is currently available in tar archive format then you need to use docker load --input <tar_file>
command. In this example we are loading debian.tar
archive using docker load --input debian.tar
command as shown below.
[root@localhost ~]# docker load --input debian.tar Loaded image: docker.io/debian:latest [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/nginx latest 08b152afcfae 33 hours ago 133 MB docker.io/debian latest 0980b84bde89 42 hours ago 114 MB docker.io/ubuntu latest c29284518f49 9 days ago 72.8 MB
Example 26: How to Check docker disk Usage
If you want to check the amount of disk space used by docker then you need to use docker system df
command as shown below.
[root@localhost ~]# docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 3 2 320.1 MB 72.78 MB (22%)
Containers 11 0 2.218 kB 2.218 kB (100%)
Local Volumes 1 0 0 B 0 B
Example 27: How to Login to Docker Hub
If you want to login to Docker Hub using command line shell then you need to use docker login
command. Here you need to provide your valid docker id and password to login.
[root@localhost ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: cyberithub
Password:
Login Succeeded
Example 28: How to Log out from Docker Registry
To log out from docker registry you need to use docker logout
command as shown below.
[root@localhost ~]# docker logout
Removing login credentials for https://index.docker.io/v1/
Example 29: How to Create a new Image from a Container's Changes
If you want to create a new image after doing certain changes in container then you need to use docker container commit <container_id> <name>
syntax.
[root@localhost ~]# docker container commit c9ed8d8e9e1c debian:latest
sha256:06b06e038e17d38f3d7b1f702c390cb32bab62800b895feda63007d21e8b289d
Example 30: How to Inspect the Changes of Container Files and Directories
If you want to know the changes done on files and directories within the container filesystem then you need to use docker diff <container_id>
syntax. In this example we are checking all the changes done within Container ID c9ed8d8e9e1c
using docker diff c9ed8d8e9e1c
command as shown below.
[root@localhost ~]# docker diff c9ed8d8e9e1c
C /run
D /run/secrets
Example 31: How to Pause all the processes within a Container
If you want to pause all the processes within a container then you need to use docker pause <container_ID>
command. In this example, we are pausing all the processes within Container ID c9ed8d8e9e1c
using docker pause c9ed8d8e9e1c
command as shown below.
[root@localhost ~]# docker pause c9ed8d8e9e1c
c9ed8d8e9e1c
Example 32: How to Update a Container's CPU-Shares
If you want to update the CPU shares of a container then you need to use --cpu-shares
option and pass the value using docker update
command. In this example, we are updating CPU shares of container ID c9ed8d8e9e1c to 100 by using docker update --cpu-shares 100 c9ed8d8e9e1c
command as shown below.
[root@localhost ~]# docker update --cpu-shares 100 c9ed8d8e9e1c
c9ed8d8e9e1c
Example 33: How to Unpause all the processes within a Container
If you want to unpause all the processes within a container which was paused earlier then you need to use docker unpause <container_id>
syntax. In this example, we are unpausing all the processes within Container ID c9ed8d8e9e1c
using docker unpause c9ed8d8e9e1c
command as shown below.
[root@localhost ~]# docker unpause c9ed8d8e9e1c
c9ed8d8e9e1c
Example 34: How to Tag an Image By ID
If you want to tag any local image in a repository with some version then you need to use docker tag <image_id> <repository>:<version>
syntax. In this example we are tagging Image ID 06b06e038e17
using docker tag 06b06e038e17 docker.io/debian:latest-release
command as shown below.
[root@localhost ~]# docker tag 06b06e038e17 docker.io/debian:latest-release [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/debian latest-release 06b06e038e17 About an hour ago 114 MB
Example 35: How to Check the Logs of a Container
If you are facing any issues with container then you can check its logs by using docker logs <container_id>
syntax. In this example we are checking logs of container ID c9ed8d8e9e1c
using docker logs c9ed8d8e9e1c
command as shown below.
[root@localhost ~]# docker logs c9ed8d8e9e1c
Example 36: How to Push an Image to Dockerhub
If you want to push an Image to docker hub registry then you need to use docker push <image_name>
syntax. In this example we are pushing cyberithub/debian
image to docker hub registry using docker push cyberithub/debian
command as shown below.
[root@localhost ~]# docker push cyberithub/debian
The push refers to a repository [docker.io/cyberithub/debian]
b7d836cab8ae: Pushed
afa3e488a0ee: Mounted from library/debian
latest: digest: sha256:60fc02849f608abb8fb892318a6803ab87c86ef61b49927d6713f206e7bee3c9 size: 736
Example 37: How to Rename a Container
If you want to rename a container then you need to use docker rename <current_name> <new_name>
syntax. In this example we are renaming container quizzical_heisenberg
to debian_latest
using docker rename quizzical_heisenberg debian_latest
command as shown below.
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c9ed8d8e9e1c 0980b84bde89 "bash" About an hour ago Up About an hour quizzical_heisenberg [root@localhost ~]# docker rename quizzical_heisenberg debian_latest [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c9ed8d8e9e1c 0980b84bde89 "bash" About an hour ago Up About an hour debian_latest
Example 38: How to Check Container Information in JSON format
If you want to check the container information in JSON format then you need to use docker inspect <container_id>
syntax. In this example we are checking all the information related to Container ID c9ed8d8e9e1c
using docker inspect c9ed8d8e9e1c
command as shown below.
[root@localhost ~]# docker inspect c9ed8d8e9e1c
[
{
"Id": "c9ed8d8e9e1c64799a9c71dbf51804602a5da955f796f075b64906c80a440505",
"Created": "2021-07-24T03:27:40.698139586Z",
"Path": "bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 2017,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-07-24T03:27:42.009939511Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
Example 39: How to Check all the options available with docker command
If you want to check all the options available with docker command then you need to use docker --help
command as shown below.
[root@localhost ~]# docker --help
Usage: docker COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-D, --debug Enable debug mode
--help Print usage
-H, --host list Daemon socket(s) to connect to (default [])
-l, --log-level string Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Example 40: How to Check Man Page of docker command
If you want to check the man page of docker command then you need to use man docker
command as shown below.
[root@localhost ~]# man docker
DOCKER(1) APRIL 2014 DOCKER(1)
NAME
docker - Docker image and container command line interface
SYNOPSIS
docker [OPTIONS] COMMAND [ARG...]
docker daemon [--help|...]
docker [--help|-v|--version]
DESCRIPTION
is a client for interacting with the daemon (see dockerd(8)) through the CLI.
The Docker CLI has over 30 commands. The commands are listed below and each has its own man page which explain usage and arguments.
To see the man page for a command run man docker <command>.