Docker Scenario Based

Q1: A container fails to start due to a port conflict. How would you troubleshoot and resolve this issue?

Ans: Check which process or container is already bound to the port using `docker ps` or `netstat`. Update the container’s port mapping in the Docker run command or compose file to use a free port.

Q2: A container is continuously restarting in production. How would you troubleshoot this issue?

Ans: Inspect logs with `docker logs ` and check restart policies. Common causes include application crashes, misconfigured health checks, or missing environment variables.

Q3: A Docker container is running but the application inside is not accessible. What steps would you take to debug the issue?

Ans: Verify container port exposure (`docker ps`), check firewall rules, confirm application is listening on the correct interface (0.0.0.0 vs localhost), and test connectivity with `curl` or `telnet`.

Q4: Logs are not visible for a running container. How would you debug logging issues in Docker?

Ans: Use `docker logs ` to check stdout/stderr. If logs are missing, verify the application’s logging configuration, check log drivers, and ensure log rotation is not truncating output.

Q5: A container health check keeps failing even though the app is running. What could be the issue?

Ans: Health check commands may not match the app’s actual readiness. Validate the health check endpoint, increase timeout/interval, and ensure the app responds correctly before marking healthy.

Q6: A container is consuming very high CPU usage. How would you identify the root cause and resolve it?

Ans: Use `docker stats` or `top` to identify the process. Profile the application, check for infinite loops or heavy queries, and apply CPU limits in Docker run/compose.

Q7: A Docker container is running out of memory frequently. How would you troubleshoot and fix this issue?

Ans: Monitor with `docker stats`. Check for memory leaks, optimize application code, and configure memory limits (`--memory`). Consider using swap or scaling horizontally.

Q8: Disk space on the Docker host is suddenly full. How would you identify what is consuming space and fix it?

Ans: Run `docker system df` and `docker system prune`. Large logs, unused images, and dangling volumes are common culprits. Clean them up and set log rotation policies.

Q9: Container logs are growing very large and consuming disk space. How would you manage log rotation?

Ans: Configure Docker’s logging driver with size limits and rotation (`--log-opt max-size`, `--log-opt max-file`). Alternatively, ship logs to external systems like ELK or Fluentd.

Q10: A container needs persistent storage but data is lost after restart. How would you fix this issue?

Ans: Mount a Docker volume or bind mount host directories. Volumes persist beyond container lifecycle, ensuring data durability.

Q11: Multiple containers need to share the same data volume. How would you configure this?

Ans: Create a named volume and mount it into multiple containers. Docker manages concurrent access safely.

Q12: Containers are not able to communicate with each other across hosts. What could be the possible reasons?

Ans: Check overlay network configuration, ensure Swarm/Kubernetes networking is set up, and verify firewall rules between hosts.

Q13: A Docker container cannot connect to an external database. How would you debug the network connectivity issue?

Ans: Test connectivity with `ping` or `telnet`. Verify DNS resolution, firewall rules, and environment variables. Ensure the DB allows connections from the container’s IP.

Q14: Docker network is not working after host restart. How would you troubleshoot it?

Ans: Restart the Docker daemon, check for missing bridge networks, and recreate custom networks if needed. Verify iptables rules.

Q15: Docker image builds are taking too long in CI/CD pipelines. How would you optimize the build time?

Ans: Use multi-stage builds, cache dependencies, minimize layers, and avoid unnecessary files with `.dockerignore`.

Q16: Docker image size becomes too large and slows down deployments. How would you optimize it?

Ans: Choose slim base images, remove build tools in final stage, clean package caches, and use multi-stage builds.

Q17: After deploying a new container version, the application stops working. How would you roll back to the previous version?

Ans: Use Docker tags to redeploy the previous image, or rollback in orchestrators like Kubernetes/Swarm. Keep versioned images for safety.

Q18: A container runs fine locally but fails in production. What could be the possible reasons and troubleshooting steps?

Ans: Differences in environment variables, networking, or resource limits. Compare configs, logs, and host OS versions.

Q19: Docker daemon fails to start on the host. How would you troubleshoot this issue?

Ans: Check system logs (`journalctl -u docker`), verify storage drivers, and ensure no corrupted files in `/var/lib/docker`. Restart the service.

Q20: After system reboot, Docker containers are not starting automatically. How would you resolve this issue?

Ans: Set restart policies (`--restart always`) or configure orchestrators/systemd to auto-start containers.

Q21: Docker registry authentication is failing during image pull. How would you debug it?

Ans: Check credentials in `~/.docker/config.json`, verify registry URL, and refresh login with `docker login`.

Q22: Sensitive credentials are exposed inside a Docker image. How would you prevent this issue?

Ans: Use environment variables or secrets management. Avoid hardcoding credentials in Dockerfiles. Use `.dockerignore` to exclude sensitive files.

Q23: Docker containers are running as root in production. What risks does this create and how would you fix it?

Ans: Running as root increases attack surface. Use non-root users in Dockerfiles (`USER` directive) and apply least privilege.

Q24: Security vulnerabilities are detected in Docker images. How would you fix and prevent this?

Ans: Regularly scan images with tools like Trivy or Clair. Update base images, apply patches, and rebuild images frequently.

Q25: A containerized application becomes slow over time. What possible causes would you investigate?

Ans: Check for memory leaks, resource exhaustion, database connection pooling issues, and monitor with `docker stats` or APM tools.

Q26: A container fails due to missing dependencies even though Dockerfile seems correct. How would you debug this?

Ans: Verify build logs, check package installation steps, and confirm dependencies exist in final image stage. Use `docker exec` to inspect runtime environment.