

Or, perhaps you’re even planning on removing syslog soon, but you need to process Docker logs in the meantime. Additionally, for performance reasons at the networking level, you could decide to have a syslog server locally as a buffer to network traffic.
Docker container logs how to#
It’s likely you already have a logging architecture using syslog and the missing piece was how to collect logs from Docker.įor instance, you could use a central syslog server as an aggregation location and then process all logs for removal of sensitive or private information, aggregating data, or forwarding logs to other places. However, it’s a cheap and straightforward strategy to help you get started. Using a syslog server might not be the first option you think of when you think about collecting container logs. You’ll notice, though, there’s going to be a missing piece once logs are there-I’ll come back to this later. There are many ways you can process and retain Docker container logs. We need a better way to collect container logs, so we don’t lose them when the container stops-containers come and go all the time. This command only works while the container is running. However, if the container goes away, you can’t read its logs anymore using the logs command. That’s initially good because you can read the logs from a container. See below for an example of the commands you’d usually run: $ docker psĬONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESģ40461436e07 busybox "/bin/sh -c 'i=0 wh…" 7 seconds ago Up 7 seconds fervent_shtern Then, you can read the logs with the docker logs CONTAINER, and you can add the -f flag to see logs live. If you want to read the logs from that container, you need either the name or the ID of the container.

docker run -d busybox /bin/sh -c 'i=0 while true do echo "$i: $(date)" i=$((i+1)) sleep 1 done' The following command runs a container that simply prints a text indefinitely. Reading Logs From a Containerĭocker comes with a native command, docker logs, to read logs from a container. Then, I’ll show you how you can integrate syslog and Docker to improve your troubleshooting strategy through logs. In this post, I’ll provide a quick overview of why having a syslog server might be a good option. Also, if the container has gone away, we can read the logs collected while it was running. We need to have a centralized location for our logs to have a better context of the problem. If you’re using Docker Compose, the process can be automated: version: "3.However, we live in a world where we won’t have only one container running, and our systems have multiple dependencies. mount source=nginx-logs,target=/var/log/nginx First, create a new volume: docker volume create nginx-logsĪnd run the container with the -mount: docker run -d You can bind a directory like /var/log/nginx to a volume that’s visible from the host. You can even run /bin/bash if you want to hop in and poke around.Ī more permanant solution that plays nicer with host services is to use a Docker volume mount. Using this, you can tail a log file inside a Docker container: docker exec -it e4bd48ef3103 tail -f log.txtīecause this lets you run any command, you can use journalctl or any other debugging strategies you want, as long as you preface it with docker exec -it. Docker provides the exec -it command to allow you to run any command inside any running Docker process. & ln -sf /dev/stderr /var/log/nginx/error.logīut, if you want to check out specific files inside a container, you can do so. RUN ln -sf /dev/stdout /var/log/nginx/access.log It does this with a symlink from /dev/stdout to the log file, and you can set up something similar for your containers. For example, the default NGINX container is set up to send its Docker logs to STDOUT to make log checking easier. Viewing Logs From Apps Inside Containersĭepending on the container, this might not be necessary. Accessing logs like this is still possible from the host side through Docker. Many apps have their own dedicated logging systems, which often log to files like /var/log/nginx/access.log. However, one thing you’ll notice is that this is STDOUT and STDERR, which is useful for lots of things, but only shows you the console output of the entrypoint specified by “CMD” in the Docker file. If you’re using Docker Compose, you can use the log command from that to view all logs easily: docker-compose logs

You can also -tail the file, or use -timestamps to display the log time, or use -until and -since to filter based on time. Here, the -f flag will keep the prompt open and “follow” any new entries in the file.
