Docker logs command

In this tutorial we will learn how to view logs of the container. In previous tutorial we learn about docker start and create commands. If you do not know about this command I would suggest to read following article before you read this article.

Docker create or start command

View logs for a container or service

To learn about docker logs command let's first try to create a new container from the container image. Then we will run this container and at last we will check the output of the container.

For our example, we will take hello-world image from the docker hub and then create and start a container from this docker image. Open your terminal window and let's run following command:

# get the image from docker hub
docker pull hello-world

# sample output of above command
Using default tag: latest
latest: Pulling from library/hello-world
Digest: sha256:4fe721ccc2e8dc7362278a29dc660d833570ec2682f4e4194f4ee23e415e1064
Status: Image is up to date for hello-world:latest

When you run above command following things happen:

  • docker client takes this command and instruct docker server to contact docker hub
  • docker hub finds the public docker image hello-world and downloads it

Now, we have the image next we will create a container out of this downloaded docker hello-world image. To create a new container from this docker image run following command on your terminal window.

# create a container from hello-world image
docker create hello-world

# above command will out put with container id
# container id would look like something below
274d7022498e0caf7dd77536966af5092c641126a934136dda677e22c2c7a38a

Now, we created a container and have the id of the container but we have not started this container yet. To start the container you run following command.

# start the container with container ID
docker start 274d7022498e0caf7dd77536966af5092c641126a934136dda677e22c2c7a38a

# above command will output the same container id
274d7022498e0caf7dd77536966af5092c641126a934136dda677e22c2c7a38a

When you run this command you will notice that you do not know what is going on inside a container this is because it does not output anything on console accept the container id.

If you want to debug:

  • an error occured
  • or to see if container is still running or exited
  • to view step by step process output

You need to use docker logs command. Following is a syntax of this command:

Let's now see the logs of the container to see the process output and to check weather or not our container is still running. Open your terminal window again and run following command:

# check the logs of our container
docker logs 274d7022498e0caf7dd77536966af5092c641126a934136dda677e22c2c7a38a

Above command will output something like this on your terminal window:

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

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Alright, now we know what is going on within our container and we learned how to view process logs of our container.

In this tutorial we ran the series of following commands in order:

  • docker pull <image-name>
  • docker create <image-name>
  • docker start <container-id>
  • docker logs <container-id>

You could achieve all of these command in one single docker command however I wanted you to know what is going on here before I introduce you this command.

Now, run following command to achieve the similar output by avoiding to run all 4 commands:

# pull the docker image
# create a docker container from the image
# start a container with container id
# viewing the container process log
docker run hello-world

Above command will also output all of the container process logs along with pulling, creating and starting a container. To learn more in depth about this command checkout my tutorial on:

Docker run command