Let's take a look at Docker Logging
Tasks:
Now that Docker is setup, it's time to get our hands dirty. In this section, you are going to run a reverse proxy called Traefik container (a high-performance webserver, load-balancer, and proxy) on your system and get hands-on with the docker logs
command.
-
To get started, create a new directory
traefik
and change to thetraefik
directorymkdir traefik cd traefik
-
Using your favorite editor create a new file named
traefik.toml
and add the below configuration to the newly created file.################################################################ # API and dashboard configuration ################################################################ 123[api] ################################################################ # Docker configuration backend ################################################################ [docker] domain = "docker.local" watch = true ################################################################ # Enable Access Logs ################################################################ [accessLog]
-
Start the
Traefik
proxy. Ensure thetraefik.toml
is in your current working directory.
docker run -d -p 8080:8080 -p 80:80 --name traefik \
-v $PWD/traefik.toml:/etc/traefik/traefik.toml \
-v /var/run/docker.sock:/var/run/docker.sock traefik
-
Ensure the
Traefik
container is running by running thels
command with-l
showing last containerdocker container ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0b2062765e41 traefik "/traefik" 8 minutes ago Exited (1) 8 minutes ago Traefik
Uh oh, what happend? We can actually use the
docker logs
command on stopped containers to troubleshoot. Great, let's do it. -
Check the logs of the
Traefik
container to see why the container didn't startdocker container logs traefik 2019/04/25 15:11:51 Error reading TOML config file /etc/traefik/traefik.toml : Near line 3 (last key parsed ''): bare keys cannot contain '['
-
OK, remove the
Traefik
containerdocker container rm -f traefik
This is the forceful way to remove it. With great power comes great responsability. You are warned!
-
Fix the
traefik.toml
configuration file line 4 removing123
in front of the[API]
block################################################################ # API and dashboard configuration ################################################################ [api] ################################################################ # Docker configuration backend ################################################################ [docker] domain = "docker.local" watch = true ################################################################ # Enable Access Logs ################################################################ [accessLog]
-
Start
Traefik
with the fixed configuration file. Ensure thetraefik.toml
is in your current working directory.docker run -d -p 8080:8080 -p 80:80 --name traefik \ -v $PWD/traefik.toml:/etc/traefik/traefik.toml \ -v /var/run/docker.sock:/var/run/docker.sock traefik
-
Ensure the
Traefik
container is runningdocker container ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e72a26a2b752 traefik "/traefik" 5 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp,0.0.0.0:8080->8080/tcp traefik
-
Test the
Traefik
container withcurl
or open a browser tab and navigate to:https://0.0.0.0
curl 0.0.0.0
Go ahead and send a few curl/refresh request to the
Traefik
container. -
Check the logs
docker container logs traefik
What do we see different? We should now see each curl/refresh we sent to the
Traefik
containerWe should see a 404 error about no backends configured.
Now, we will connect a whoami
container to the Traefik
proxy. This whoami
container will register itself automatically with the proxy. The Traefik
proxy routes traffic from 0.0.0.0
from the Traefik
proxy to our new whoami
application.
Traefik
watches the Docker daemon for new containers that join and start on thee server. When a new container starts it automatically registers it with Traefik
-
Start the
whoami
containerdocker run -d --name whoami emilevauge/whoami
-
curl
the whoami container using the Virtual Hostnametest.docker.local
configured inTraefik
curl --header 'Host: whoami.docker.local' 'http://localhost:80/'
Response
```
Hostname: 299f3c36eb18
IP: 127.0.0.1
IP: 172.17.0.5
GET / HTTP/1.1
Host: test.docker.local
User-Agent: curl/7.47.0
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 172.17.0.1
X-Forwarded-Host: test.docker.local
X-Forwarded-Port: 80
X-Forwarded-Proto: http
X-Forwarded-Server: 10744bcc8a7d
X-Real-Ip: 172.17.0.1
```
-
Finally, run the
docker container logs
command on the proxy to ensure everything is now working as expecteddocker container logs traefik
We now see the hostname which is queried and a
HTTP 200
success code172.17.0.1 - - [25/Apr/2019:15:37:33 +0000] "GET / HTTP/1.1" 200 326 "-" "curl/7.47.0" 5 "Host-whomai-docker-local-0" "http://172.17.0.5:80" 1ms 172.17.0.1 - - [25/Apr/2019:15:37:36 +0000] "GET / HTTP/1.1" 200 326 "-" "curl/7.47.0" 6 "Host-whoami-docker-local-0" "http://172.17.0.5:80" 1ms 172.17.0.1 - - [25/Apr/2019:15:37:37 +0000] "GET / HTTP/1.1" 200 326 "-" "curl/7.47.0" 7 "Host-whoami-docker-local-0" "http://172.17.0.5:80" 0ms
The docker container logs
command is a powerful command and is used for troubleshooting, analyzing, and general information gathering. The command is useful to Developers to debug new applications as well as Operations for information gathering.
-
Great! Let's now take a look at the
docker container logs
help to better understand how we can best use the commanddocker container logs --help Usage: docker logs [OPTIONS] CONTAINER Fetch the logs of a container Options: --details Show extra details provided to logs -f, --follow Follow log output --since string Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes) --tail string Number of lines to show from the end of the logs (default "all") -t, --timestamps Show timestamps --until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
-
Add timestamps to our logging output. This will help with narrowing down when events occured if no timestamp is created in the log message.
docker container logs -t traefik
-
Tail the last
n
number of lines in the log fileThis is extremely helpful when logs become very big. If you were to run a
docker logs
command on a large log it could over run your terminal winderdocker container logs --tail 5 traefik
-
Follow the log for real-time updates.
docker container logs -t -f traefik
Curl the
whoami
container a couple times to see the log update. Remember the IP address of the node you are on by looking at the command promptroot@ip_address
.Switch to a different worker node and run the below command.
curl --header 'Host: whoami.docker.local' 'http://<ip_address_host>:80/'
-
Switch back to the original Worker and Restart the
Traefik
containerdocker container restart traefik
-
Check the logs again. What do you notice?
docker container logs traefik
The logs still persist inside the container from our previous tests.
-
Stop and remove the
Traefik
containerdocker container rm -f traefik
-
Start
Traefik
againdocker run -d -p 8080:8080 -p 80:80 --name traefik \ -v $PWD/traefik.toml:/etc/traefik/traefik.toml \ -v /var/run/docker.sock:/var/run/docker.sock traefik
-
Check the logs. What do you notice?
docker container logs traefik
This time we removed the container and started a new container. It is important to notice now the logs didn't persist. This is why it is important we persist logs outside of the containers.
-
Cleanup running containers
docker container rm -f whoami traefik
We have now seen how logging works in a single container. Now, we want to see what logs look like when multiple containers are running in a compose file. In this example we will use the docker voting application. This stack contains 3 different containers running with one docker-compose file.
-
In the
Setup
section we cloned the Repo. If you haven't done so please do it nowgit clone https://github.com/vegasbrianc/docker-compose-demo.git
-
Start the Compose demo with docker-compose.
cd docker-compose-demo docker-compose up -d
-
Once the voting application stack has started. Check the logs of the voting app stack.
docker-compose logs
What we notice is that Docker color codes the log based on container names. Since we have 3 different containers this makes it easier when viewing from a terminal window.
-
Expanding the command to capture certain containers
docker-compose logs reverse-proxy
-
Combing everything we learned follow and timestamp the logs
docker-compose logs -f -t reverse-proxy
-
Time to stop and remove the running containers
docker-compose rm -fs
What did we learn in this section?
- Running
docker container logs
on stopped containers - Troubleshooting containers
- The
docker container logs
command is actually quite powerful and can be combined with other tools like the Linuxgrep
command or others - Logs don't persist in containers once the container is removed
docker-compose logs
works similarly todocker container logs
but displays all containers in the compose stack
For the next step in the workshop, head over to Docker Swarm & Logging