Troubleshooting & Comparisons

Troubleshooting Docker Container Issues with Logs and Commands

Learn how to diagnose and resolve issues with Docker containers using logs and commands.

4 min read

Docker containers are a critical part of modern development, but diagnosing issues can be tricky when a container refuses to start or behaves unexpectedly. This guide walks you through how to troubleshoot Docker container issues using logs and commands, including the crucial "docker logs" tool and other essential steps.

Common Issues and Error Behavior

When working with Docker containers, these challenges often arise:

  • Docker container not starting or exiting immediately without explanation.
  • Logs appear incomplete or are not accessible when diagnosing issues.
  • Misuse or misunderstanding of commands like docker stop and docker kill.

Each issue stems from different causes, but logs are the first stop for debugging.

prerequisites

  • Docker installed and running (verify with docker --version).
  • Basic familiarity with shell/command-line operations.
  • Access to the container ID for the affected container (use docker ps -a).

Steps to View Docker Container Logs

Logs are essential for understanding why a container might not behave as expected. Follow these steps to access logs for any Docker container:

steps

  1. Identify the container: Use the following command to list all containers, including stopped ones:

    bash
    docker ps -a
  2. Copy the container ID: Note down the CONTAINER ID of the container you wish to investigate.

  3. View the logs: Run the following command, replacing [containerID] with the actual ID:

    bash
    docker logs [containerID]
  4. Understand log contents: Look for error messages, warnings, or unexpected output. These provide clues for resolution.

Creating and Interpreting Simple Logs

If you're just getting started or need a controlled example, use BusyBox to simulate logs:

steps

  1. Create a simple container running the ls command:

    bash
    docker run --name test-container busybox ls
  2. Start the container in detached mode without attached output:

    bash
    docker container start test-container
  3. View the logs to confirm the output:

    bash
    docker logs test-container

Output Differences with and without the -a Flag

When starting containers, the -a flag determines whether execution outputs immediately to the terminal. Compare these commands to explore the difference:

bash
docker container start -a test-container  # Output visible in real-time
docker container start test-container    # Output logged for later review

Understanding Docker Container Stop vs Kill

Handling long-running or hanging containers requires choosing the right command:

For example, long-running processes (like a ping) may exceed the allowed stop time:

bash
docker run --name ping-container busybox ping google.com
docker container stop ping-container  # Graceful stop (takes 10 seconds for unresponsive processes)
docker container kill ping-container   # Instant termination

Steps to Use Docker Stop and Kill Commands Effectively

steps

  1. Stop a container gracefully:

    bash
    docker container stop [containerID]
  2. Force stop an unresponsive process:

    bash
    docker container kill [containerID]
  3. Verify shutdown status:

    bash
    docker ps -a

    Confirm the container is no longer running by checking its "STATUS" field.

Using Docker Compose Logs for Better Diagnostics

If you're using Docker Compose to manage containers, its logging tools offer consolidated insights:

steps

  1. Run docker-compose logs to view logs for all containers:

    bash
    docker-compose logs
  2. Add specific service names to narrow the scope:

    bash
    docker-compose logs [service-name]
  3. Use flags like --tail or --follow to refine the log output:

    bash
    docker-compose logs --tail 50 --follow

    This displays the last 50 lines of logs and continuously updates as new logs come in.

Mistakes and Verification Checklist

Avoid these pitfalls when troubleshooting Docker containers:

FAQ

Why is my Docker container not starting?

If a Docker container fails to start, the most common causes are application-level issues or configuration errors. Check the startup logs using docker logs [containerID] for more details.


How do I check multiple container logs at once?

If you're managing multiple containers via Docker Compose, use docker-compose logs to view logs from all containers in a single output. You can also specify a service name to narrow the scope.


What happens when docker stop hangs?

When a container ignores the stop signal, Docker waits up to 10 seconds for graceful shutdown. If the process remains unresponsive, Docker forcefully terminates it. Use docker kill to immediately stop such containers.


Official reference: Docker documentation.