Troubleshooting containers – Containerization with Docker

To see what’s going on within the container, you can use the docker logs command. But before using that, we need to know the container’s ID or name to see the container’s logs.

To get a list of containers running within the host, run the following command

$ docker ps      
CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
beb5dfd529c9nginx:1.18.0“/docker-2 minutes agoUp 2 minutes80/tcpfervent_
  entrypoint.…”   shockley

The preceding command lists the NGINX container that we just started. Unless you specify a particular name for your container, Docker allocates a random name to it. In this case, it has called it fervent_ shockley. It also assigns every container a unique container ID, such as beb5dfd529c9.

You can use the container ID or the container name to interact with the container to list the logs. Let’s use the container ID this time:

$ docker logs beb5dfd529c9

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform

configuration

/docker-entrypoint.sh: Configuration complete; ready for start up

As you can see, it prints a similar log output as it did when we ran it in the foreground.

Practically speaking, you will use docker logs 90% of the time unless you need to debug something with BusyBox. BusyBox is a lightweight shell container that can help you troubleshoot and debug issues with your container – mostly network issues.

Let’s make BusyBox echo Hello World! for us:

$ docker run busybox echo ‘Hello World!’

Unable to find image ‘busybox:latest’ locally

latest: Pulling from library/busybox

325d69979d33: Pull complete

Digest: sha256:560af6915bfc8d7630e50e212e08242d37b63bd5c1ccf9bd4acccf116e262d5b

Status: Downloaded newer image for busybox:latest

Hello World!

As we can see, Docker pulls the latest busybox image from Docker Hub and runs the echo ‘Hello World’ command.

You can also use BusyBox in interactive mode by using the -it flag, which will help you run a series of commands on the BusyBox shell. It is also a good idea to add an –rm flag to it to tell Docker to clean up the containers once we have exited from the shell, something like this:

$ docker run -it –rm busybox /bin/sh

  • # echo ‘Hello world!’ Hello world!
  • # wget http://example.com

Connecting to example.com (93.184.216.34:80)

saving to ‘index.html’

index.html                   100% |***********************************

****|  1256    0:00:00 ETA

‘index.html’ saved

/ # exit

Upon listing all the containers, we do not see the busybox container in there:

$ docker ps -a      
CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
beb5dfd529c9nginx:“/docker-17 minutesUp 1780/tcpfervent_
 1.18.0entrypoint.…”agominutes shockley

There are various other flags that you can use with your containers, each serving a specific purpose.

Let’s look at a few common ones.

Leave a Reply

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