Docker 102 — Basic commands to operate containers using CLI

Nitin Manju
7 min readSep 6, 2022

In Chapter 1, we understood what Docker is and how it works in a nutshell. We have also installed Docker and are ready to move further. In this chapter, we will understand the basics of the Docker CLI.

The Docker CLI

The Docker CLI is a tool with which you control various Docker components. It is the primary way to interact with Docker. The CLI sends out the command to the daemon which in turn carries out the task.

Let's get started with some basic commands to create, run and operate the containers.

docker run

The very first command we will understand is the run. This command will create a container from an image and start it. It will execute the default operation (startup command) that has been configured for the image. However, this can be overridden to run a custom command.

Refer to the below syntax:

docker run <image name> [Command]

Alright, let’s get started. As a real programmer would do, the first thing we do is say hello to the world. Assuming the installation went well and without errors, let’s run our first docker command. Open a terminal/command window and try:

docker run hello-world

This should download and run the ‘hello-world’ image as a container.

The first time you run it, the image is not found in the local repository, hence Docker will pull it from the DockerHub repository and then create the container from the image. Notice the first few messages after the command is executed. Refer to the below image:

If we run the same command a second time, notice what happens:

This time it didn't pull the image since it was available locally.

Let us try to run some overridden commands. If you notice above, the hello-world image when executed will output a series of text. This is because it executes a startup command that does it for us. However, sometimes, we may have to execute additional commands.

To demonstrate this, we will use a different image. We will use an image called ‘busybox’ which is a lightweight Linux image from the Docker community. Try:

docker run busybox echo hello world

we see that the image name is followed by the custom command echo followed by “hello world”

echo is a Linux tool to echo the text back to the standard output. In our case, we want to echo “hello world”. The output should look something like this:

You can use some other commands which are supported by Busybox like ls.

You must have noticed that running the commands will immediately exit the container after the startup command is done executing and the prompt is returned to us.

However, it is possible that a container will continue to run and doesn't exit until we explicitly do so. For example, the ‘Redis’ image will run as a container and continue to do so until explicitly stopped.

You can see that the prompt has not been returned to us.

docker ps

This brings us to the next topic, the ps command. Similar to its use in other contexts, the ps command will return a list of running container processes. Try:

docker ps

you will notice that it has listed the redis container process running, which we started in the previous exercise. Let us create another container:

docker run busybox ping www.google.com

If we now run the pscommand, we should see the below result:

One thing to notice here is the ‘CONTAINER ID’ and the ‘NAMES’ column. These are useful to identify the containers for use elsewhere. Specifically, the ID is used to reference a running container. To get a history of all the containers created on your machine, use the --all flag.

docker ps --all

Notice the ‘CREATED’ and the ‘STATUS’ columns which give some information about the status of the container.

docker create

If you are wondering about the CONTAINER ID which was created and is displayed when the ps command is executed, we will talk about it next. The run command is a combination of two instructions.

Run = Create + Start

The create command will create a container instance from an image, which is when a container ID is also created.

docker create <image name>

Let us try to create a container from the Redis image:

This command returns a long string, which is the ID. If you are wondering the ps command hadn’t returned a long string like this, then you are right we can identify a container using the first few characters from this ID. We can now start this container using the start command discussed next.

docker start

The start command is used to start a created container.

Copy a few characters from the container ID which was generated and use it in the start command. You can use the full ID as well.

docker start <container ID>

To verify if the container has started, use the ps command:

using the -a flag along with the start command will also print the output from the container. Use it as shown below:

docker start -a <container ID>

docker logs

When containers are started, they may print certain output logs. The logs command can be used to get this log from the container.

docker logs <container ID>

Let us create and start a container and then execute logs. You will notice that, when we start the container, we see no output. However, we can get it back using this command.

It should be noted that when the logs command is executed, it will not restart the container.

docker exec

We can execute commands inside a running container. This is handy when you would like to execute additional commands after the default command configured for the image is done executing.

To do so, we can use the exec command. This along with the -i and -t flags will open a channel to execute commands inside the container. Refer to the command below:

docker exec [-it] <container ID> <command>

You can even open a shell prompt from inside the running container using the sh as the command for exec. This will allow you to execute more than one command. Refer below:

you could now type supported shell commands like ls in the prompt.

If you don't attach the -it flag, the command will be executed, however, you won't be able to interact with the standard input to the container.

The -it flag can be used along with the run command while running containers. However, doing that the default command won't be executed.

docker stop

To stop a running container use the stop command. Pass the ID of a running container and it should be stopped.

docker stop <container ID>

verify by running the ps command to make sure the container has stopped.

Restarting a container is possible by executing the start command with the same container ID. Using the --all flag should give a list of stopped containers.

The stop command will gracefully shut down the container process. It gives enough time to the container to perform any action that should be part of the shutdown process. If the stop command doesn’t respond on time, docker will fall back to a different command called kill which is explained next.

docker kill

Using the kill command will kill the container immediately and won't give any time for the shutdown process.

docker kill <container ID>

docker system prune

Creating containers will take up system resources, if not handled correctly, it can take up a lot of storage space. To clean up stopped containers and all the associated resources, use the system prune command. Try:

docker system prune

This will reclaim some of the system resources and delete unused containers. However, it should be noted that the local images downloaded from the Hub will also be deleted.

In the next chapter, we will learn how to build our first image using a Dockerfile. Hope you have enjoyed this post. Stay tuned for more Docker 101!

--

--