Explain the concept of Docker Overlay Networks and when you would use them.
Answer:
Docker Overlay Networks enable communication between Docker containers running on different Docker daemon hosts. They are crucial for multi-host container orchestration, like in a Docker Swarm or Kubernetes cluster, allowing services to communicate seamlessly across nodes without complex routing configurations.
What is the purpose of Docker Content Trust (DCT) and how does it work?
Answer:
Docker Content Trust (DCT) provides cryptographic verification of image publishers and integrity. It ensures that images pulled from a registry are signed by trusted publishers, preventing the use of tampered or unauthorized images. It works by using Notary to sign and verify image manifests.
How can you limit the resources (CPU, memory) a Docker container can consume?
Answer:
Resource limits can be set using docker run flags. For CPU, use --cpus (e.g., --cpus='1.5') or --cpu-shares. For memory, use --memory (e.g., --memory='2g') and --memory-swap. These settings prevent a single container from monopolizing host resources.
Describe the difference between COPY and ADD in a Dockerfile.
Answer:
COPY copies local files or directories from the build context into the image. ADD has similar functionality but can also extract tar archives from the source and download files from URLs. Generally, COPY is preferred for clarity and security unless ADD's extra features are specifically needed.
What is a multi-stage build in Docker and what are its benefits?
Answer:
A multi-stage build uses multiple FROM instructions in a single Dockerfile, where each FROM can discard artifacts from previous stages. This significantly reduces the final image size by only copying necessary build artifacts (e.g., compiled binaries) into the final, smaller runtime image, improving security and deployment speed.
How do you optimize Docker image size and build speed?
Answer:
Optimize image size by using multi-stage builds, choosing smaller base images (e.g., Alpine), leveraging .dockerignore, and consolidating RUN commands. Optimize build speed by ordering Dockerfile instructions to maximize layer caching, using a .dockerignore file, and ensuring build context is minimal.
Answer:
Docker uses storage drivers (e.g., OverlayFS, AUFS, Btrfs) to manage how layers are stored and combined. OverlayFS is generally recommended for its performance and simplicity, especially for read-heavy workloads. The choice of driver impacts container startup time, write performance, and overall disk I/O.
What is Docker Swarm Mode and how does it differ from Kubernetes?
Answer:
Docker Swarm Mode is Docker's native orchestration tool for managing a cluster of Docker engines. It's simpler to set up and use than Kubernetes, making it suitable for smaller deployments or those already heavily invested in the Docker ecosystem. Kubernetes is a more powerful, feature-rich, and complex orchestrator, widely adopted for large-scale, production-grade deployments.
How can you troubleshoot a Docker container that keeps restarting?
Answer:
First, check container logs using docker logs <container_id>. Then, inspect the container's state with docker inspect <container_id> to see exit codes and restart policies. You might also try running the container interactively (docker run -it ...) to observe its behavior directly or attach to it (docker attach).
Describe Docker's networking modes and their use cases.
Answer:
Docker offers several networking modes: bridge (default, isolated network for containers), host (container shares host's network stack), none (no network interface), and overlay (for multi-host communication). bridge is common for single-host apps, host for performance-critical apps needing direct port access, and overlay for distributed services.