Day 21 Task: Important Docker Interview Questions

Faizan ShaikhFaizan Shaikh
4 min read

Essential Docker Interview Questions for DevOps Engineers

1. What is the difference between an Image, Container, and Engine?

  • Image: A lightweight, stand-alone package containing everything needed to run a piece of software (code, runtime, libraries, etc.).

  • Container: A runnable instance of an image. Containers encapsulate the application and its environment, providing isolation.

  • Engine: The core component of Docker, responsible for managing images, containers, networks, and storage.

2. Difference between COPY vs ADD in Docker?

  • COPY: Simply copies files or directories from the source to the container.

  • ADD: Does the same but also supports extracting tar files and downloading from URLs.

3. Difference between CMD vs RUN?

  • RUN: Executes commands during image build time, creating a new image layer.

  • CMD: Specifies the default command to run when the container starts but can be overridden.

4. How to reduce the size of a Docker image?

  • Use a minimal base image, multi-stage builds, reduce the number of layers, clean up unnecessary files, and avoid adding extra tools not needed at runtime.

5. Why and when should you use Docker?

  • Docker is beneficial for consistent environments, easy deployment, scalability, CI/CD, and microservices. Use it when you need isolation, rapid deployment, or simplified dependency management.

6. Explain Docker components and how they interact.

  • Key components include Docker Daemon, Client, Images, Containers, Networks, and Volumes. The Docker Daemon manages objects, while the client interacts via commands, images provide a basis for containers, and networks/volumes enhance connectivity and data persistence.

7. Explain the terminology: Docker Compose, Dockerfile, Docker Image, Docker Container.

  • Docker Compose: Tool for defining and managing multi-container Docker applications.

  • Dockerfile: Script with instructions to build Docker images.

  • Docker Image: An application blueprint; read-only.

  • Docker Container: The runtime instance of an image.

8. Real-life scenarios where you have used Docker?

  • Examples could be developing microservices, deploying applications in staging/production environments, or using Docker for testing and CI/CD.

9. Docker vs Hypervisor?

  • Docker uses OS-level virtualization, making it lightweight and fast, whereas hypervisors virtualize the entire OS, providing better isolation but are heavier.

10. Advantages and disadvantages of Docker?

  • Advantages: Portability, rapid deployment, efficient resource usage.

  • Disadvantages: Limited to Linux-based environments (though Windows is partially supported), security challenges, and potential complexity in networking.

11. What is a Docker namespace?

  • A namespace is a Linux kernel feature used by Docker to isolate containers’ processes, networking, and filesystem, enhancing security.

12. What is a Docker registry?

  • A storage and distribution system for Docker images. Docker Hub is a public registry, but private registries can also be set up for secure storage.

13. What is an entry point?

  • ENTRYPOINT defines a container’s default executable, which cannot be overridden by CMD. Useful for setting up an environment where all CMD instructions are appended.

14. Implementing CI/CD in Docker?

  • CI/CD is implemented using Docker images for consistent build environments, Docker Compose for managing services, and tools like Jenkins and GitLab CI for orchestrating workflows.

15. Will data on the container be lost when it exits?

  • Yes, container data is ephemeral. Persistent data is managed using Docker volumes or bind mounts.

16. What is Docker Swarm?

  • A Docker-native orchestration tool for managing clusters and scaling applications across multiple hosts.

17. Docker commands for common tasks:

  • Viewing running containers: docker ps

  • Running a container with a specific name: docker run --name container_name image_name

  • Exporting a Docker image: docker save -o image.tar image_name

  • Importing an image: docker load -i image.tar

  • Deleting a container: docker rm container_name

  • Removing stopped containers, networks, caches: docker system prune

18. Best practices to reduce Docker image size?

  • Multi-stage builds, clean-up steps within Dockerfile, minimizing RUN layers, using .dockerignore files to exclude unnecessary files.

19. Troubleshooting a Docker container that won’t start?

  • Use logs (docker logs container_name), inspect commands, check port bindings, dependencies, and examine Dockerfile configurations.

20. Explain the Docker networking model.

  • Docker provides various networking options: bridge (default), host, none, overlay (for Swarm), and macvlan for more complex setups.

21. Managing persistent storage in Docker?

  • Use volumes or bind mounts. Volumes are managed by Docker, while bind mounts are tied to the host’s filesystem.

22. How to secure a Docker container?

  • Use the principle of least privilege, limit container privileges, restrict network access, scan images, and avoid running containers as root.

23. What is Docker overlay networking?

  • Overlay networks enable communication between containers on different hosts in a Docker Swarm setup.

24. Managing environment variables in Docker?

  • Use the ENV instruction in Dockerfile, pass variables with docker run --env, or use .env files in Docker Compose.
0
Subscribe to my newsletter

Read articles from Faizan Shaikh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Faizan Shaikh
Faizan Shaikh