Docker Basics

Q1: What is Docker and why is it used?

Ans: Docker is a containerization platform that allows developers to package applications with all dependencies into lightweight, portable containers. It ensures consistency across environments and simplifies deployment.

Q2: Explain the core components of Docker architecture.

Ans: The components are as follow: 1. Docker Engine – the runtime that builds, runs, and manages containers.
2. Docker Daemon – background process that manages images, containers, networks, and volumes.
3. Docker CLI – command-line interface for interacting with the Docker daemon.
4. Docker Images – read-only templates used to create containers.
5. Docker Containers – lightweight, portable runtime instances of images.
6. Docker Registry – centralized service (like Docker Hub) for storing and distributing images.
7. Docker Volumes – persistent storage mechanism for containers.

Q3: What is the difference between a container and a virtual machine?

Ans: Containers share the host OS kernel and are lightweight, while virtual machines run a full OS with its own kernel, making them heavier. Containers start faster and use fewer resources compared to VMs.

Q4: Explain Docker architecture.

Ans: Docker uses a client-server architecture. The Docker client communicates with the Docker daemon, which manages images, containers, networks, and volumes. Registries store Docker images, and containers run from those images.

Q5: What is a Docker image?

Ans: A Docker image is a read-only template containing application code, libraries, and dependencies. It is used to create containers. Images can be built using Dockerfiles or pulled from registries like Docker Hub.

Q6: What is a Docker container?

Ans: A Docker container is a running instance of a Docker image. It is isolated, lightweight, and portable, providing a consistent runtime environment across different systems.

Q7: What is a Dockerfile?

Ans: A Dockerfile is a text file containing instructions to build a Docker image. It defines the base image, environment variables, dependencies, and commands to run inside the container.

Q8: Write a simple Dockerfile for a Node.js application.

dockerfile
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]

Q9: What is Docker Compose?

Ans: Docker Compose is a tool to define and run multi-container applications using a YAML file. It simplifies orchestration by allowing services, networks, and volumes to be configured in one place.

Q10: Write a sample docker-compose.yml file.

yaml
version: '3'
services:
  web:
    image: nginx
    ports:
      - "80:80"
  app:
    build: .
    ports:
      - "3000:3000"
    depends_on:
      - web

Q11: How do you persist data in Docker?

Ans: Data persistence in Docker is achieved using volumes or bind mounts. Volumes are managed by Docker and stored in Docker's storage area, while bind mounts map host directories into containers.

Q12: Explain the difference between Volume Mounts and File (Bind) Mounts in Docker.

Ans: The concepts are as follow: 1. Volume Mount – storage managed by Docker itself, located under Docker’s internal directory (e.g., /var/lib/docker/volumes). It is portable, isolated from the host filesystem, and ideal for production workloads like databases.
2. File (Bind) Mount – directly maps a file or directory from the host machine into the container using its absolute path. It reflects changes instantly, useful for development, but less portable and more tightly coupled to the host system.

Q13: What is the difference between COPY and ADD instructions in a Dockerfile?

Ans: The differences are as follow: 1. COPY – used to copy files and directories from the host machine into the Docker image. It is simple, predictable, and only supports local file system paths.
2. ADD – more versatile than COPY. In addition to copying local files, it can also fetch remote URLs and automatically extract compressed archives (like .tar files) into the image. Because of this extra functionality, ADD should be used carefully, while COPY is preferred for most file transfer operations.

Q14: What is the difference between RUN and CMD instructions in a Dockerfile?

Ans: The differences are as follow: 1. RUN – executes commands at build time while creating the Docker image. It is mainly used to install software packages, configure the environment, or perform setup tasks. Each RUN instruction creates a new image layer.
2. CMD – specifies the default command that runs when a container starts from the image. It does not execute during build time but only at runtime. A Dockerfile can have multiple RUN instructions but only one CMD, and CMD can be overridden when starting the container.

Q15: How do you troubleshoot a Docker container that is not starting?

Ans: Troubleshooting steps... 1. Run `docker logs ` to check logs. 2. Use `docker inspect ` for configuration details. 3. Verify image validity. 4. Check resource limits. 5. Ensure dependencies and environment variables are correctly set.

Q16: What is the difference between Docker Swarm and Kubernetes?

Ans: Docker Swarm is Docker's native orchestration tool, simpler to set up but less feature-rich. Kubernetes is a more powerful orchestration platform with advanced scaling, self-healing, and ecosystem support.

Q17: How do you secure Docker containers?

Ans: Security measures include using minimal base images, scanning images for vulnerabilities, applying least privilege with user namespaces, enabling resource limits, and keeping Docker and host OS updated.

Q18: Explain the concept of Docker networking.

Ans: Docker provides different networking modes: bridge (default, for container-to-container communication), host (shares host network), overlay (multi-host communication), and none (no networking). Networks allow containers to communicate securely.

Q19: In a production environment, if one Docker service is receiving significantly higher traffic compared to others, how would you handle resource allocation?

Ans: The approach is as follow: 1. Monitor and Identify – use observability tools to confirm which service is saturated and which are underutilized.
2. Scale Down Idle Services – reduce replicas or resource limits for services with low demand to free up CPU and memory.
3. Scale Up Critical Service – increase replicas or adjust resource limits for the high-traffic service to handle load effectively.
4. Autoscaling Policies – implement Horizontal Pod Autoscaler (Kubernetes) or monitoring-driven scaling (Docker Swarm) to automate this adjustment.
5. Resource Prioritization – configure quotas and limits so critical services always get priority access to cluster resources.
6. Continuous Review – regularly analyze traffic patterns to ensure long-term capacity planning and avoid manual firefighting.

Q20: Explain the Docker container lifecycle.

Ans: The lifecycle is as follow: 1. Created – the container is created from an image but not yet running.
2. Running – the container is actively executing its processes.
3. Paused – the container’s processes are temporarily suspended without stopping it.
4. Stopped/Exited – the container has finished execution or was manually stopped.
5. Restarted – the container is restarted after being stopped or due to a policy.
6. Removed – the container is deleted from the system, freeing resources.

Q21: What would you do if a Docker container keeps restarting in a production environment?

Ans: The approach is as follow: 1. Check Logs – use 'docker logs' to identify the root cause of crashes.
2. Inspect Health Checks – verify if failing health checks are triggering restarts.
3. Resource Limits – ensure the container has sufficient CPU and memory.
4. Configuration Errors – validate environment variables and entrypoint scripts.
5. Mitigation – fix the underlying issue or adjust restart policies.

Q22: If one Docker service is consuming all resources while others are idle, how would you handle it?

Ans: The solution is as follow: 1. Scale Down Idle Services – reduce replicas of underutilized services.
2. Scale Up Critical Service – increase replicas or allocate more resources to the busy service.
3. Autoscaling – configure monitoring-driven scaling policies.
4. Resource Prioritization – adjust CPU/memory limits so critical services get priority.

Q23: What happens if you delete a Docker volume that is still attached to a running container?

Ans: The impact is as follow: 1. Data Loss Risk – the container may lose access to persistent data.
2. Runtime Errors – applications depending on the volume may fail.
3. Best Practice – always stop containers before removing volumes and ensure backups exist.

Q24: How would you debug a situation where a container cannot connect to another container in the same network?

Ans: The approach is as follow: 1. Check Network – verify both containers are attached to the same Docker network.
2. DNS Resolution – test connectivity using container names instead of IPs.
3. Firewall Rules – ensure no host-level firewall is blocking traffic.
4. Application Ports – confirm the target container is listening on the expected port.

Q25: What would you do if a Docker image size becomes too large and slows down deployments?

Ans: The solution is as follow: 1. Optimize Layers – combine commands in RUN to reduce layers.
2. Use Slim Base Images – switch to lightweight images like 'alpine'.
3. Clean Up – remove unnecessary packages and temporary files during build.
4. Multi-stage Builds – use multi-stage Dockerfiles to keep only required artifacts.