View basic Docker Desktop logs
In this step, you will learn how to view basic logs from Docker containers. Logs are essential for debugging and monitoring your applications running in containers.
First, let's run a simple container that generates some output. We will use the hello-world
image, which is a very small image that prints a message and exits.
docker run hello-world
You should see output similar to this, indicating that the Docker daemon pulled the image and ran the container successfully:
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:f52335ce493178fc15f729218f180e9988e31c374a6ce98da40cbb890f97f10e
Status: Downloaded newer image for hello-world:latest
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.
(Assuming it was not already locally available.)
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 learn more, try the following commands:
docker run -it ubuntu bash
docker images
docker ps
docker stop <containerid>
docker rm <containerid>
To get started with Docker Desktop, visit:
https://www.docker.com/products/docker-desktop
The output you see is the standard output (stdout) and standard error (stderr) streams from the container. Docker captures these streams and makes them available as logs.
Now, let's run a container that stays running and generates logs over time. We will use the alpine
image and run a simple command that prints a message every 5 seconds.
First, pull the alpine
image:
docker pull alpine
You should see output indicating the image is being pulled:
Using default tag: latest
latest: Pulling from library/alpine
... (output showing download progress)
Digest: sha256:...
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest
Now, run the alpine
container in detached mode (-d
) so it runs in the background, and give it a name (--name mylogger
) for easy reference. The command while true; do echo "Hello from mylogger at $(date)"; sleep 5; done
will print a message with the current date every 5 seconds.
docker run -d --name mylogger alpine sh -c 'while true; do echo "Hello from mylogger at $(date)"; sleep 5; done'
This command will output the container ID.
To view the logs of the running container, use the docker logs
command followed by the container name or ID.
docker logs mylogger
You will see the output generated by the container's command, with a new line appearing approximately every 5 seconds. You can press Ctrl+C
to stop viewing the logs.
To follow the logs in real-time, use the -f
(follow) option:
docker logs -f mylogger
This will continuously display new log entries as they are generated. Press Ctrl+C
to stop following the logs.
Finally, let's stop and remove the container we created:
docker stop mylogger
docker rm mylogger
This cleans up the container resources.