More Docker Commands

Now that we are familiar with basic Docker commands, we are going to jump into some more slightly advanced commands.

Running a Busybox Container

In order to go into docker logs, we I am using Busybox image. BusyBox combines tiny versions of many common UNIX utilities into a single small executable and the docker image is available in Docker Hub.

To run a container using the Busybox image I pulled, I am using the docker run command.

C:\WINDOWS\system32>docker run busybox

C:\WINDOWS\system32>

You might have noticed that when you run a container, usually it outputs the container ID. But nothing happened this time. If I run the ps command with -a flag, I can see that the container has exited.

C:\WINDOWS\system32>docker ps -a
 CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS                        PORTS                               NAMES
 de1d9b1f0256   busybox           "sh"                     3 minutes ago    Exited (0) 3 minutes ago                                          awesome_williams

The reason for this is because a Docker container is always attached to a process. As long as the process is running, the container will be running as well. If the process dies, then the container exits. In this case, since there is no process attached to our container, it exits immediately.

How do we overcome this? Let’s just start a shell inside our container and attach it to an interactive terminal.

C:\WINDOWS\system32>docker run --name bbox -it busybox sh
 / #

You might have noticed that Docker assigns random names to our containers by default. This is not very convenient as they are not meaningful and hard to remember. Therefore, here I have used --name flag to specify the name of my container as “bbox”. -it flags open an interactive terminal(SH shell) to allow me to do what I want inside the BusyBox system.

Once the above command is run, our container will attach to our command line and you would see a flickering cursor. Now if you exit this terminal, it will kill the container as well. Therefore I will open another CMD and run a docker ps command to see if I can see my container this time.

C:\WINDOWS\system32>docker ps
 CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS      NAMES
 fe96e4dfa5f8   busybox   "sh"                     11 minutes ago   Up 11 minutes              bbox

This time the container has not exited since it is attached to an interactive terminal. And you can see the name is bbox as I have specified.

Docker logs

Docker logs command allows us to see the standard output of our container. To do this I will first type in some commands in bbox terminal so that there will be something in the standard output. Once done, when I run the docker logs command on another CMD, I can see the output there.

C:\WINDOWS\system32>docker run --name bbox -it busybox sh
 / # echo hello
 hello
 / #
C:\WINDOWS\system32>docker run --name bbox -it busybox sh
 / # echo hello
 hello
 / # echo world
 world
 / #

C:\WINDOWS\system32>docker logs bbox
 / # echo hello
 hello
C:\WINDOWS\system32>
C:\WINDOWS\system32>docker logs bbox
 / # echo hello
 hello
 / # echo world
 world
C:\WINDOWS\system32>

If our container has accumulated a large amount of logs, then we can use several options to format our output.

C:\WINDOWS\system32>docker logs bbox --tail 2
 / # echo world
 world

Here --tail 2 only fetches the last two lines.

C:\WINDOWS\system32>docker logs bbox --follow
 / # echo hello
 hello
 / # echo world
 world
 / # echo 2021
 2021

--follow will follow the output. which means if anything new is logged, you command line will update it in real time without you having to type in any new commands.

Docker Inspect

Docker inspect provides detailed information on constructs controlled by Docker. Running the inspect command will give you results as a JSON array.

C:\WINDOWS\system32>docker inspect bbox
[
     {
         "Id": "fe96e4dfa5f8d4bbfb9ab8f4d66ef523218f73c5e1841fd03d4d28cc69644fcf",
         "Created": "2021-05-17T08:49:00.9339046Z",
         "Path": "sh",
         "Args": [],
         "State": {
             "Status": "running",
             "Running": true,
             "Paused": false,
             "Restarting": false,
             "OOMKilled": false,
             "Dead": false,
             "Pid": 8402,
             "ExitCode": 0,
             "Error": "",
             "StartedAt": "2021-05-17T08:49:01.4210071Z",
             "FinishedAt": "0001-01-01T00:00:00Z"
         },

You can run the command as above. I have only included a subsection of the result as the full resulting JSON array is too big. But you can format the result and fetch only a subsection in JSON format using the --format flag.

C:\WINDOWS\system32>docker inspect --format="{{json .Config.Image}}" bbox
 "busybox"

This gives me the image the container is based on

C:\WINDOWS\system32>docker inspect --format="{{json .NetworkSettings.Networks.bridge.IPAddress}}" bbox
 "172.17.0.3"

This gives me the IP address of the container. We’ll look into more on Docker networks later.

Run a Command Inside a Running Docker Container

To run a command inside a running container, we can use the docker exec command.

C:\WINDOWS\system32>docker exec -it 77 sh
 echo $(date)
 Wed May 19 12:46:21 UTC 2021
 #

Here I have opened an interactive terminal connected to a running Redis container(notice that I have used only the first 2 characters “77” of the container ID here) and printed the current date/time. You can run any command that you would usually run on a Linux terminal here.

I am just going to create a new text file inside the container, install vim and add some text into the file and save it.

# touch newFile.txt
# ls
 newFile.txt
# vim newFile.txt
 sh: 7: vim: not found
# apt update
 Get:1 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB]
...
...
# apt install vim
 Reading package lists… Done
 Building dependency tree
...
...

# vim newFile.txt
 # cat newFile.txt
 This is just a text file I created inside my Redis container.
 #

It works just like any Linux Terminal.

In the next article Docker Environment Variables, we will look at how we can use environment variables in Docker.

Share this article if it was helpful!

Leave a Reply

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