Container Security Collect Kubernetes Logs on Docker for Mac

Identify Security Vulnerabilities in Your Web Apps and APIs
Scan now for free

You have just started using the built-in Kubernetes functionality on Docker for Mac? It is a promising alternative to docker-compose if you want to mirror your system infrastructure for local development. If you are using Kubernetes in production, you can easily use your existing pod definitions on your machine without the need to set up a Kubernetes cluster like minikube yourself. This short blog post will show you how to collect all logs for your local cluster.

You switch to your local cluster using kubectl config use-context docker-for-desktop and run all kubectl commands such as collecting logs for your pods and containers. Sometimes, you may want to store all your logs for later analysis. Typically, this job is for the elk / Elastic stack or tools such as Graylog or Google Cloud Operations Suite. But that seems a total overkill for a local development environment. Unfortunately, there is nothing like docker-compose logs to collect all logs simultaneously.

Using a little bash code, you can still collect all logs in one place. This script leverages the fact that all Kubernetes pods are still run using docker and pipes the log for each container into a single file:


# If there is no logs directory, just create it
if [[ ! -d logs ]]
    mkdir logs

# Inifinite loop to collect logs
while true

    # Get all docker containers running in kubernetes except the kubernetes system containers such as the kubernetes dashboard
    # via:
    for c in $(docker ps -a --format="" | egrep k8s | egrep -v "kubernetes")
        if [[ ! -f logs/$c.log ]]
            echo "Creating Log Pipe for: $c"
            docker logs -f $c > logs/$c.log 2>&1 &

    # Sleep ten seconds before the next attempt
    sleep 10


How do you like this workaround? Just leave me a comment with your thoughts!

Get a quick security report for your website for free now

We are analyzing
Scanning target
Scan status: In progress
Scan target:
Date: 30/11/2023
Crashtest Security Suite will be checking for:
Information disclosure Known vulnerabilities SSL misconfiguration Open ports
Complete your scan request
Please fill in your details receive the
quick security audit by email.
Security specialist is analyzing your scan report.
То verify your identity please provide your phone/mobile:
Thank you.
We have received your request.
As soon as your security audit is ready, we will notify you.