Docker Scenario-Based Interview Questions and Detailed Answers (2025)

Author

Kritim Yantra

Apr 12, 2025

Docker Scenario-Based Interview Questions and Detailed Answers (2025)

Preparing for a Docker interview in 2025? Scenario-based questions test your practical problem-solving skills and real-world experience with Docker. This blog covers 10 complex scenarios with detailed solutions, from debugging to production deployments.


1. Scenario: Debugging a Crashing Container

Problem:
A container running a Python Flask app crashes randomly with exit code 137.

Solution Steps:

  1. Check Logs:

    docker logs <container_id>
    
    • Look for Out of Memory (OOM) errors.
  2. Inspect Resource Limits:

    docker inspect <container_id> | grep -i "memory"
    
    • If no limits are set, the host might kill the container under memory pressure.
  3. Fix:

    • Limit Memory:
      docker run -d --memory="512m" --name flask_app flask:latest
      
    • Optimize App: Reduce memory leaks (e.g., unbounded caches).

2. Scenario: Docker Build is Too Slow

Problem:
Building a Docker image takes 15 minutes due to large dependencies.

Solution Steps:

  1. Optimize Dockerfile:

    • Use multi-stage builds to discard build tools:
      FROM python:3.9 as builder
      COPY requirements.txt .
      RUN pip install --user -r requirements.txt
      
      FROM python:3.9-slim
      COPY --from=builder /root/.local /root/.local
      COPY . .
      
  2. Leverage Build Cache:

    • Order commands from least to most frequently changed:
      COPY requirements.txt .  # Rarely changes → cached
      RUN pip install -r requirements.txt
      COPY . .                 # Frequently changes → last
      
  3. Use .dockerignore:

    node_modules/
    .git/
    

3. Scenario: Container Can’t Connect to Database

Problem:
A containerized Node.js app fails to connect to a PostgreSQL DB with ECONNREFUSED.

Solution Steps:

  1. Verify Network:

    docker network ls
    docker inspect <network_id> | grep -A 5 "Containers"
    
    • Ensure both containers are on the same network.
  2. Test Connectivity:

    docker exec -it node_app ping db
    
    • If ping fails, check DNS resolution.
  3. Fix:

    • Create a shared network:
      docker network create app_net
      docker run -d --net=app_net --name db postgres
      docker run -d --net=app_net --name node_app node:latest
      

4. Scenario: Persistent Data Loss After Container Restart

Problem:
A MySQL container loses data when restarted.

Solution Steps:

  1. Check Volume Usage:

    docker inspect mysql_container | grep -A 10 "Mounts"
    
    • If no volume is mounted, data is stored in the container’s writable layer (ephemeral).
  2. Fix:

    • Use a Named Volume:
      docker volume create mysql_data
      docker run -d -v mysql_data:/var/lib/mysql --name mysql mysql:latest
      
    • Backup Volume:
      docker run --rm -v mysql_data:/data -v $(pwd):/backup busybox tar cvf /backup/mysql_backup.tar /data
      

5. Scenario: Docker Host Running Out of Disk Space

Problem:
The Docker host throws No space left on device errors.

Solution Steps:

  1. Identify Large Files:

    docker system df  # Show disk usage
    docker images --filter "dangling=true" -q | xargs docker rmi  # Remove dangling images
    
  2. Prune Unused Objects:

    docker system prune -a --volumes
    
  3. Limit Log Size:

    {
      "log-driver": "json-file",
      "log-opts": {
        "max-size": "10m",
        "max-file": "3"
      }
    }
    

    (Configure in /etc/docker/daemon.json)


6. Scenario: Securing a Container Running as Root

Problem:
A container runs as root, posing security risks.

Solution Steps:

  1. Run as Non-Root:

    FROM alpine
    RUN adduser -D appuser
    USER appuser
    
  2. Drop Capabilities:

    docker run --cap-drop=ALL --cap-add=NET_BIND_SERVICE nginx
    
  3. Read-Only Filesystem:

    docker run --read-only nginx
    

7. Scenario: Deploying a Multi-Service App with Docker Compose

Problem:
Need to deploy a web app (Nginx), API (Node.js), and DB (PostgreSQL) with dependencies.

Solution Steps:

  1. Write docker-compose.yml:

    version: "3.8"
    services:
      web:
        image: nginx
        ports: ["80:80"]
      api:
        build: ./api
        depends_on: ["db"]
      db:
        image: postgres
        volumes: ["pg_data:/var/lib/postgresql/data"]
    volumes:
      pg_data:
    
  2. Deploy:

    docker-compose up -d
    

8. Scenario: Zero-Downtime Deployment with Docker Swarm

Problem:
Update a service without downtime.

Solution Steps:

  1. Rolling Update:

    docker service update \
      --image myapp:v2 \
      --update-parallelism 2 \
      --update-delay 10s \
      myapp
    
  2. Rollback on Failure:

    docker service update --rollback myapp
    

9. Scenario: Debugging Slow Container Startup

Problem:
A container takes 2 minutes to start.

Solution Steps:

  1. Profile Startup:

    time docker run myapp
    
  2. Check Entrypoint:

    docker inspect myapp | grep -A 5 "Entrypoint"
    
    • Optimize slow init scripts.
  3. Use --init for Zombie Processes:

    docker run --init myapp
    

10. Scenario: Sharing Data Between Containers

Problem:
Two containers need access to the same configuration file.

Solution Steps:

  1. Use a Shared Volume:

    docker volume create config_vol
    docker run -d -v config_vol:/config --name container1 alpine
    docker run -d -v config_vol:/config --name container2 alpine
    
  2. Alternative: Bind Mount:

    docker run -d -v /host/path:/config --name container1 alpine
    

Final Tips for Scenario-Based Interviews

  1. Ask Clarifying Questions:
    • "Is the container running in Swarm/Kubernetes?"
    • "What’s the error message?"
  2. Explain Trade-offs:
    • "Bind mounts are easier for dev, but volumes are better for prod."
  3. Show Debugging Steps:
    • "First, I’d check docker logs, then docker inspect..."

Need more scenarios? Drop a comment! 🚀

Practice Challenge:

  • Simulate a memory leak in a container and debug it using docker stats and exec.
  • Deploy a 3-tier app (frontend, backend, DB) using Compose.

Good luck with your interview! 🐳

Tags

Docker

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Continue with Google

Related Posts