How to debug with `docker exec`?

QuestionsQuestions8 SkillsProCustom Docker ImagesNov, 03 2025
079

To debug a running Docker container using docker exec, you can follow these steps:

  1. Identify the Running Container: First, list the running containers to find the one you want to debug.

    docker ps
  2. Access the Container's Shell: Use docker exec to start an interactive shell session inside the container. This allows you to run commands directly within the container's environment.

    docker exec -it <container_name_or_id> /bin/bash

    or, if the container uses a different shell:

    docker exec -it <container_name_or_id> /bin/sh
  3. Inspect the Environment: Once inside the container, you can check the environment, installed packages, and configuration files. For example:

    # Check the current working directory
    pwd
    
    # List files in the directory
    ls -la
    
    # Check installed packages (for Debian/Ubuntu)
    dpkg -l
  4. Run Diagnostic Commands: You can run commands to diagnose issues, such as checking logs, testing network connectivity, or inspecting running processes:

    # Check application logs
    cat /var/log/app.log
    
    # Test network connectivity to another container
    curl http://other-container:port
  5. Modify Configuration: If necessary, you can modify configuration files or environment variables directly within the container to troubleshoot issues.

  6. Exit the Container: Once you have finished debugging, you can exit the shell session by typing:

    exit

Using docker exec in this way allows you to interactively debug issues within a running container, making it easier to identify and resolve problems.

0 Comments

no data
Be the first to share your comment!