Basic Docker Commands

Verifying Our Docker Installation

Now that we have installed Docker on our machine, next step would be to verify whether our installation has been successful. To do this we can pull a docker hello world image and run it as below.

C:\WINDOWS\system32>docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:5122f6204b6a3596e048758cabba3c46b1c937a46b5be6225b835d091b90e46c
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

If everything is alright, you should see a message stating everything is working correctly as above.

Docker Images vs Containers

Before we dive into the next part, it is good to learn some important terminology and understand the difference between a docker image and a container.

A Docker image is basically an immutable read-only template that allows you to deploy a container. It can be referred to as a snapshot or a blueprint of a container. An image contains all that is required to run a completely working environment of a particular application. This includes artefacts such as dependencies, libraries, source code and any deployables. Docker images are stored on registry such as Docker Hub or any private registry.

A Docker Container is an instance of a Docker image. From a single image, you can run as many containers as you want with different parameters and their runtime environments will be completely isolated from each other. To borrow from a programming analogy, if an image is a class, a container is an object of that class.

Basic Commands

Docker pull

Allows you to pull an image from a registry. Let’s pull a publicly available image from Docker Hub.

C:\WINDOWS\system32>docker pull redis
Using default tag: latest
latest: Pulling from library/redis
69692152171a: Pull complete
a4a46f2fd7e0: Pull complete
bcdf6fddc3bd: Pull complete
b7e9b50900cc: Pull complete
5f3030c50d85: Pull complete
63dae8e0776c: Pull complete
Digest: sha256:365eddf64356169aa0cbfbeaf928eb80762de3cc364402e7653532bcec912973
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest

This will pull the image to your machine. Notice that I have not specified any source to pull the image from. In this case it directly pulls the image from Docker Hub.

Docker images

Lists down all the images available in your machine. As you can see there are 2 at the moment on my machine.

C:\WINDOWS\system32>docker images
REPOSITORY          TAG        IMAGE ID       CREATED        SIZE
redis               latest     bc8d70f9ef6c   3 days ago     105MB
hello-world         latest     d1165f221234   2 months ago   13.3kB
Docker run

Allows you run a container based on an image. Here I am running a container using the Redis image I pulled.

C:\WINDOWS\system32>docker run redis
1:C 16 May 2021 12:58:29.183 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 16 May 2021 12:58:29.183 # Redis version=6.2.3, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 16 May 2021 12:58:29.183 # 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 16 May 2021 12:58:29.184 * monotonic clock: POSIX clock_gettime
1:M 16 May 2021 12:58:29.185 * Running mode=standalone, port=6379.
1:M 16 May 2021 12:58:29.186 # 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 16 May 2021 12:58:29.186 # Server initialized
1:M 16 May 2021 12:58:29.186 * Ready to accept connections

As you can see, the container has started but the process has attached to the command line. This is not very convenient as I cannot use this command line again without terminating the running the container. To exit you can press CTRL + C while on the command line.

To start a container in detached mode, we can use the -d flag with the run command.

C:\WINDOWS\system32>docker run -d redis
961cf1cc06ba7d2f2e950cb9212fbf9c81a8b453c0633ed23e8afa39c59e791d

Running the container with -d flag as above prints the id of the container I just started as above.

Docker ps

Lists down all the running containers.

C:\WINDOWS\system32>docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS      NAMES
961cf1cc06ba   redis     "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes   6379/tcp   nice_beaver
7758055ebb12   redis     "docker-entrypoint.s…"   4 minutes ago   Up 4 minutes   6379/tcp   quirky_pasteur

As you can see we have two containers running from the Redis image. Here you can see details such as container IDs, status, when the container was created, port, name that has been automatically assigned to the container.

If you need to see all the containers including the ones that exited, you need to specify the -a flag.

C:\WINDOWS\system32>docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS      NAMES
12ab81b24dab   hello-world       "/hello"                 3 hours ago      Exited (0) 3 hours ago                                          kind_gauss
961cf1cc06ba   redis     "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes   6379/tcp   nice_beaver
7758055ebb12   redis     "docker-entrypoint.s…"   4 minutes ago   Up 4 minutes   6379/tcp   quirky_pasteur

This command show the hello-world container that was run earlier. Notice the status shows as exited.

Docker stop

Stops a running container.

C:\WINDOWS\system32>docker stop nice_beaver
nice_beaver

Here you can specify either the container ID or the name. If you are giving the container ID, you do not have to give the whole ID. Even if you give only the first few characters, it would work as long as it can identify the container uniquely from the string.

Docker rm

Removes a container. Do keep in mind that you have to stop the container if it is running.

C:\WINDOWS\system32>docker rm nice_beaver
nice_beaver
Docker rmi

Allows you to remove an image from the machine. Do keep in mind that you have to stop and remove all the containers that are based on the image before you can remove it.

C:\WINDOWS\system32>docker rmi hello-world
Untagged: hello-world:latest
Deleted: sha256:71fa375b9d1997521c2b046eacfb1b3fdd721b7b1cd9ab4e104be80ea7be38ca

In the next article we will look into slightly more advanced commands!

Share this article if it was helpful!

Leave a Reply

Your email address will not be published. Required fields are marked *