Docker is an evolving system with developers proactively working to improve usage and performance. So the commands are always changing. Docker commands often get deprecated and replaced with new or more efficient ones. You can use the help option to check the latest available commands on your docker installation:

$ docker --help

To check options for a particular command, you can use the help option for that command. For example, in order to check the ‘docker run’ command options, you can use the following:

$ docker run --help

As a docker expert, there are certain tasks you have to perform regularly. So, we’ll organize the information into smaller parts. It will give you better context for running the various commands.
At the moment, there are 13 management commands and 41 general commands. Here are the top docker commands we are going to use for our lessons:

docker attach – Attaches your local input/output/error stream to a running container.
docker commit – Creates a new image from the current changed state of the container.
docker exec – Runs a command in a container that is active or running.
docker history - Displays the history of an image.
docker info - Shows system-wide information.
docker inspect - Finds system-level information about docker containers and images.
docker login - Logins to local registry or Docker Hub.
docker pull – Pulls an image or a repository from your local registry or Docker Hub.
docker ps - Lists various properties of containers.
docker restart - Stops and starts a container.
docker rm - Remove containers.
docker rmi - Remove images
docker run – Runs a command in an isolated container.
docker search – Searches the Docker Hub for images.
docker start – Starts already stopped containers.
docker stop – Stops running containers.
docker version - Provides docker version information.

You can find more detailed descriptions here.
Let’s dive into the various actions that you can perform with these docker commands.
Note: For partial information dumps, we have used three dots (…).

Finding Docker Version and System Information

Whether you are working on your own machine or working on the cloud, you’ll often need to check the docker version and the docker system information. You can find out your docker version using the command:

$ docker version
Client:
Version: 18.03.0-ce
API version: 1.37
Go version: go1.9.4
Git commit: 0520e24
Built: Wed Mar 21 23:06:22 2022
OS/Arch: darwin/amd64
Experimental: false
Orchestrator: swarm
Server:
Engine:
Version: 18.03.0-ce
API version: 1.37 (minimum version 1.12)
Go version: go1.9.4
Git commit: 0520e24
Built: Wed Mar 21 23:14:32 2022
OS/Arch: linux/amd64
Experimental: true

Another important command is ‘docker info’:

$ docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 18.03.0-ce
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog

It will show you various important information like Server Version, Storage Driver, Kernel Version, Operating System, Total Memory and more. The information can be useful when you are trying to spin up new resources for your current docker installation or trying to figure out a system-level resource allocation problem. It is also a quick way to check the number of running, paused and stopped containers and the number of images downloaded to your system.

Searching and Downloading Docker Images

You can search for already available images on Docker Hub with the 'docker search' command.

$ docker search ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 7861 [OK]
dorowu/ubuntu-desktop-lxde-vnc Ubuntu with openssh-server and NoVNC 190 [OK]
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 156 [OK]
ansible/ubuntu22.04-ansible Ubuntu 22.04 LTS with ansible 93 [OK]
ubuntu-upstart Upstart is an event-based replacement for th… 87 [OK]
neurodebian NeuroDebian provides neuroscience research s… 50 [OK]

The above search for 'ubuntu' is showing the available images and their descriptions, stars, official statuses, and automated statuses. The stars, official and automated statuses are useful ways to figure out the reputation of the image.
Let’s download the most reputable ‘ubuntu’ image. You can use the ‘docker pull’ command:

$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
6b98dfc16071: Pull complete
4001a1209541: Pull complete
6319fc68c576: Pull complete
b24603670dc3: Pull complete
97f170c87c6f: Pull complete
Digest: sha256:5f4bdc3467537cbbe563e80db2c3ec95d548a9145d64453b06939c4592d67b6d
Status: Downloaded newer image for ubuntu:latest
$

Playing Around With Docker Images

You can use the ‘docker info’ command to find out the number of images you have:

$ docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 1

But the ‘docker images’ command will list in detail the images you have:

$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 113a43faa138 2 weeks ago 81.2MB

Suppose you decide to download an NGINX image. You can just run another ‘docker pull’ command:

$ docker pull nginx

Now if you check your docker images, you’ll see something like this:

$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 113a43faa138 2 weeks ago 81.2MB
nginx latest cd5239a0906a 2 weeks ago 109MB

You can find out more about these images here:

〉 Docker Hub Official Repositories
〉 Ubuntu
〉 Nginx

You can use these pages to find out a particular version of an image. On the Ubuntu page, you’ll notice that the latest version of Ubuntu is 22.10. If you are looking for 18.04 version of Ubuntu, you can use the 18.04 tag to download that particular version:

$ docker pull ubuntu:18.04

Then you’ll have two Ubuntu image versions on your machine:

$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 18.04 5e8b97a2a082 2 weeks ago 114MB
ubuntu latest 113a43faa138 2 weeks ago 81.2MB
nginx latest cd5239a0906a 2 weeks ago 109MB

Note: You don't need to register with Docker Hub to pull images. But if you want to push images to Docker Hub, you'll need to register and then login using the 'docker login' command:

$ docker login --username=yourhubusername [email protected]

Running Docker Container for Images

Let’s suppose you want to run an NGINX server on docker. Run the following command:

$ docker run -p 8080:80 nginx

You have used the run command to create an NGINX container from the Nginx image that you previously pulled from Docker Hub. The ‘-p 8080:80’ is telling Docker to map your localhost port 8080 to docker container's port 80. You should be able to access your NGINX server from http://localhost:8080.
The NGINX container is attached to your command line. So if you exit the command line, the container will die. You can start the NGINX container with the detach (‘-d’) option, so it can keep running even if you exit the command line.

$ docker run -p 8080:80 -d nginx

The above command will start the container in the detached mode and return back to the command line.

List Docker Containers with ‘docker ps’ command

The ‘docker ps’ command allows you to look up all the containers you are running.

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6162daed25da nginx "nginx -g 'daemon of…" 7 minutes ago Up 7 minutes 0.0.0.0:8080->80/tcp hardcore_torvalds

It shows the various properties of the containers. You can see that it has been created from the nginx image and the port forwarding information is also shown. The CONTAINER ID and NAMES property require special mention. You can use these properties to uniquely identify the containers. Both of these properties are auto-generated. But you can also name the container during the container creation process. Let’s create an NGINX container with the name “my_nginx”:

$ docker run --name my_nginx -p 8888:80 -d nginx

Let’s list docker containers again:

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7b19b6ad778 nginx "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:8888->80/tcp my_nginx
6162daed25da nginx "nginx -g 'daemon of…" 15 minutes ago Up 16 minutes 0.0.0.0:8080->80/tcp hardcore_torvalds

Notice the new container has the name “my_nginx”. When you are dealing with a lot of containers, you can use a naming convention. This will help you better organize the containers.

The ‘docker ps’ command only shows running containers. If you use the ‘docker info’ command for the above case:

$ docker info
Containers: 2
Running: 2
Paused: 0
Stopped: 0
Images: 3

You can see that there are 2 containers running. If you have a paused or stopped container, you will not see those containers with the 'docker ps' command only. You’ll have to use the all (’-a’) option:

$ docker ps -a

Starting, Stopping, Restarting and Killing Containers

You can start and stop containers with the use of the next docker commands.
Suppose, you want to stop the ‘my_nginx’ container. You can either use the CONTAINER ID or the NAME. In this case, let’s use the name:

$ docker stop my_nginx
my_nginx

Let’s list docker containers:

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6162daed25da nginx "nginx -g 'daemon of…" 27 minutes ago Up 27 minutes 0.0.0.0:8080->80/tcp hardcore_torvalds
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7b19b6ad778 nginx "nginx -g 'daemon of…" 12 minutes ago Exited (0) About a minute ago my_nginx
6162daed25da nginx "nginx -g 'daemon of…" 27 minutes ago Up 27 minutes 0.0.0.0:8080->80/tcp hardcore_torvalds

If you run ‘docker ps’ without the ‘-a’ option, it only shows the running container. In the second case, you can see that the ‘my_nginx’ container is in exited status.
Let’s docker start container:

$ docker start my_nginx
my_nginx

If you check the list of docker containers:

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7b19b6ad778 nginx "nginx -g 'daemon of…" 16 minutes ago Up 29 seconds 0.0.0.0:8888->80/tcp my_nginx
6162daed25da nginx "nginx -g 'daemon of…" 30 minutes ago Up 30 minutes 0.0.0.0:8080->80/tcp hardcore_torvalds

You can see that STATUS is showing that the container ‘my_nginx’ is up again.
If you want to stop and start a container due to some issue, you can use the restart command. It's faster than stopping and starting the containers individually:

$ docker restart my_nginx

You can kill a docker container like a process. Let’s kill the ‘my_nginx’ container:

$ docker kill my_nginx

List docker containers again:

$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7b19b6ad778 nginx "nginx -g 'daemon of…" 22 minutes ago Exited (137) 7 seconds ago my_nginx
6162daed25da nginx "nginx -g 'daemon of…" 36 minutes ago Up 36 minutes 0.0.0.0:8080->80/tcp hardcore_torvalds

The container my_nginx is not operating. Also, you can see in the info that you have a running container and a stopped container:

$ docker info
Containers: 2
Running: 1
Paused: 0
Stopped: 1
Images: 3

Use the start or restart docker commands to start the killed container.

Docker Exec Bash and Docker SSH into Container

You’ll often need to interact with a container’s shell to create a service or solve problems. You can use the ‘docker exec’ command to create an interactive shell. Let’s start a container from the ubuntu image with a bash shell:

$ docker run --name my_ubuntu -it ubuntu:latest bash
root@a19c770b8621:/#

The root@hash# means that you are in the bash shell of the docker container. You can run shell commands:

root@a19c770b8621:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@a19c770b8621:/# pwd
/

From another command prompt of your local machine, you can list docker containers:

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a19c770b8621 ubuntu:latest "bash" About a minute ago Up About a minute my_ubuntu
6162daed25da nginx "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:8080->80/tcp hardcore_torvalds

You can see ‘my_ubuntu’ is running. Suppose you want to docker ssh into container ‘my_ubuntu’. You can use the docker exec bash method:

$ docker exec -it my_ubuntu bash
root@a19c770b8621:/#

Notice the CONTAINER ID and hash of the command prompt matches. So your effort to docker SSH into container ‘my_ubuntu’ was successful.

Use ‘docker exec’ to issue commands into your container. For example, you can run the ‘ls’ command on your ‘my_ubuntu’ docker container directly from the command prompt:

$ docker exec -it my_ubuntu ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr

Launching Containers in Detached Mode and Using ‘docker attach’

In the above example, you started the ubuntu container in attached mode. Instead, you can start it in a detached mode:

$ docker run -it -d --name my_ubuntu_2 ubuntu:latest bash
75b28b7208359137b3e1dc2843387918e28b4c6c4860ef0cdeac79c205f5cbc4

Verify the container is running:

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
75b28b720835 ubuntu:latest "bash" 3 minutes ago Up 3 minutes my_ubuntu_2
a19c770b8621 ubuntu:latest "bash" 15 minutes ago Up 15 minutes my_ubuntu
6162daed25da nginx "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:8080->80/tcp hardcore_torvalds

Use the ‘docker attach’ command to get the docker exec bash like effect:

$ docker attach my_ubuntu_2
root@75b28b720835:/#

Checking History of Docker Image

The docker community builds the docker images. These images are created in layers. You can use the ‘docker history’ command to see how the images were created. Let’s first find out what images you have:

$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 22.04 5e8b97a2a082 2 weeks ago 114MB
ubuntu latest 113a43faa138 2 weeks ago 81.2MB
nginx latest cd5239a0906a 2 weeks ago 109MB

Let’s check the history of 'nginx' image:

$ docker history nginx
IMAGE CREATED CREATED BY SIZE COMMENT
cd5239a0906a 2 weeks ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B
&LTmissing> 2 weeks ago /bin/sh -c #(nop) STOPSIGNAL [SIGTERM] 0B
&LTmissing> 2 weeks ago /bin/sh -c #(nop) EXPOSE 80/tcp 0B
&LTmissing> 2 weeks ago /bin/sh -c ln -sf /dev/stdout /var/log/nginx… 22B
&LTmissing> 2 weeks ago /bin/sh -c set -x && apt-get update && apt… 53.7MB
&LTmissing> 2 weeks ago /bin/sh -c #(nop) ENV NJS_VERSION=1.15.0.0.… 0B
&LTmissing> 2 weeks ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.15.0-… 0B
&LTmissing> 7 weeks ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B
&LTmissing> 8 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B
&LTmissing> 8 weeks ago /bin/sh -c #(nop) ADD file:ec5be7eec56a74975… 55.3MB

You can use the history command of the image to find out what changes happened recently. If you notice a problem after spinning up a container from a new version of an image you were already using, this command can help you find the cause. Alternatively, you can use the following version of the command too:

$ docker image history nginx

Docker Inspect Containers

You can use the ‘docker inspect’ command to find out low-level information about your system. Run the ‘docker ps’ command to list docker containers:

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6162daed25da nginx "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:8080->80/tcp hardcore_torvalds

Let’s use the CONTAINER ID to inspect the container (You can use the container name also):

$ docker inspect 6162daed25da
[
{
"Id": "6162daed25da50b98afca5f7ed8caca2289bf309b2547d87ae8674b899d604a4",
"Created": "2022-06-25T05:46:37.902211302Z",
"Path": "nginx",
"Args": [



"DriverOpts": null
}
}
}
}
]

The command will provide a lot of information in a JSON format. Here is a trick to find the IP address of a container:

$ docker inspect 6162daed25da | grep "IPAddress"
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",

Using ‘docker cp’ to Copy Files from Your Local Machine to a Container

Let’s list docker containers:

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6162daed25da nginx "nginx -g 'daemon of…" 3 hours ago Up 3 hours 0.0.0.0:8080->80/tcp hardcore_torvalds

The NGINX container is running on port 8080. So, if you go to http://localhost:8080, you will see the following:

Welcome to Nginx!

If you see this page, the Nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using Nginx.

Let’s create this index.html in your local directory:

&LThtml>
&LTHeader>&LTtitle>My Website&LT/title>&LT/header>
&LTbody>
Hello world
&LT/body>
&LT/html>

Let’s check the folder that has the index.html file in the NGINX container using 'docker exec' command with 'ls':

$ docker exec -it hardcore_torvalds ls /usr/share/nginx/html
50x.html index.html

Overwrite the container's index.html file with the local file you created:

$ docker cp index.html hardcore_torvalds:usr/share/nginx/html/

Now if you check http://localhost:8080 again, you should see the greeting “Hello world”.
You can use the ‘docker cp’ command to move files between your local machine and the containers you create. This method can be used to overwrite configuration files or other assets.

Creating Your Own Docker Images

Suppose you want to create future containers from the “Hello World” container you created. But you can only spin containers from images. In order to make more "Hello World" containers, you have to save the current "Hello World" container as an image.
First, stop the container:

$ docker stop hardcore_torvalds

Now list all docker containers:

$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
75b28b720835 ubuntu:latest "bash" About an hour ago Exited (0) About an hour ago my_ubuntu_2
a19c770b8621 ubuntu:latest "bash" 2 hours ago Exited (0) About an hour ago my_ubuntu
6162daed25da nginx "nginx -g 'daemon of…" 3 hours ago Exited (0) 27 seconds ago hardcore_torvalds

From the STATUS, you can see that the NGINX 'hardcore_torvalds' container is stopped. Use the ‘docker commit’ command to create a new image:

$ docker commit 6162daed25da nginx_hello_world_template
sha256:117d060587a316035ed5a776e613d9cfbeee9fbfe202c6edc9203820c7da987b

Now if you check the images, you’ll see the new image:

$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx_hello_world_template latest 117d060587a3 40 seconds ago 109MB
ubuntu 22.04 5e8b97a2a082 2 weeks ago 114MB
ubuntu latest 113a43faa138 2 weeks ago 81.2MB
nginx latest cd5239a0906a 2 weeks ago 109MB

You can use this image just like other images and spin up new docker containers. Instead of the “Welcome NGINX” page, the newly created containers will have the “Hello world” page. Example use:

$ docker run -it -d -p 8886:80 nginx_hello_world_template
4e042d76c39125471951626ba42cd609a65c73f041943298f74f4fc43dc5596a
$

Removing Docker Containers and Images

Docker containers and images take up space on your hard disk. So it’s a good idea to clean them periodically. Let’s first stop all the docker containers and then list all the containers, we will use next docker commands to perform this:

$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e042d76c391 nginx_hello_world_template "nginx -g 'daemon of…" 2 minutes ago Exited (0) 8 seconds ago boring_keldysh
75b28b720835 ubuntu:latest "bash" 2 hours ago Exited (0) About an hour ago my_ubuntu_2
a19c770b8621 ubuntu:latest "bash" 2 hours ago Exited (0) About an hour ago my_ubuntu
6162daed25da nginx "nginx -g 'daemon of…" 3 hours ago Exited (0) 11 minutes ago hardcore_torvalds

There are 4 containers in stopped state. You can use the ‘docker rm’ command to remove containers:

$ docker rm 4e042d76c391 75b28b720835 a19c770b8621 6162daed25da
4e042d76c391
75b28b720835
a19c770b8621
6162daed25da

Instead of the CONTAINER ID, you can also use the NAMES. Your container list should be clean now:

$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

Let’s list docker images:

$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx_hello_world_template latest 117d060587a3 11 minutes ago 109MB
ubuntu 22.04 5e8b97a2a082 2 weeks ago 114MB
ubuntu latest 113a43faa138 2 weeks ago 81.2MB
nginx latest cd5239a0906a 2 weeks ago 109MB

You can remove the docker images using the ‘docker rmi’ command and IMAGE IDs:

$ docker rmi 117d060587a3 5e8b97a2a082 113a43faa138 cd5239a0906a

Now your docker images list should be clean:

$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE

Congratulations!

Hopefully, this mini-guide will help you gain confidence in using all the top docker commands. Use this article as a how-to guide to practice regularly and the commands will become second nature to you.

Request more information today

Discover how our services can benefit your business. Leave your contact information and our team will reach out to provide you with detailed information tailored to your specific needs. Take the next step towards achieving your business goals.

Contact form:
Our latest news