Skip to content

Latest commit

 

History

History
204 lines (172 loc) · 11.1 KB

File metadata and controls

204 lines (172 loc) · 11.1 KB

1. What is Docker, and how does it work?

  • Answer:
    Docker is a platform for developing, shipping, and running applications in lightweight, portable containers. Containers package up the code, libraries, and dependencies needed to run an application, ensuring that it works consistently across different environments. Docker provides a consistent environment by running applications in isolated containers, which improves portability, scalability, and ease of deployment.

2. What is the difference between a Docker container and a virtual machine (VM)?

  • Answer:
    The key difference is in the architecture:
    • Virtual machines run on hypervisors and contain a full operating system along with the application, leading to higher overhead.
    • Docker containers share the host OS kernel, are more lightweight, and contain only the application and its dependencies, making them faster to start and more resource-efficient.

3. What are Docker images, and how do they differ from containers?

  • Answer:
    • Docker Images are the blueprints for containers, containing all necessary code, libraries, and dependencies for running an application. Images are read-only and can be versioned and reused.
    • Docker Containers are running instances of Docker images. They are dynamic and can be started, stopped, and deleted as needed.

4. What is Docker Compose and when would you use it?

  • Answer:
    Docker Compose is a tool for defining and running multi-container Docker applications. You define a multi-container environment using a docker-compose.yml file, and Compose handles starting, stopping, and rebuilding the containers as needed. It is ideal for applications that require multiple services, such as a web server, database, and caching service.

5. Explain how Docker Networking works.

  • Answer:
    Docker uses several types of networks to allow communication between containers:
    • Bridge Network: Default network for containers, isolated from the host but allows containers to communicate with each other.
    • Host Network: Directly connects containers to the host network.
    • Overlay Network: Used for multi-host communication, often used in Docker Swarm and Kubernetes.
    • None Network: The container has no network interface. Each container gets its own IP address, and they can communicate through ports.

Docker Troubleshooting Questions:

1. What steps would you take if a Docker container fails to start?

  • Answer:
    To troubleshoot a container that fails to start:
    1. Check the container logs using docker logs <container-id> to identify error messages.
    2. Inspect the exit code using docker inspect <container-id> to understand why the container stopped.
    3. Ensure that the container has the correct configuration (e.g., environment variables, ports, and volumes).
    4. Confirm that the necessary images and dependencies are present and correctly tagged.
    5. If the image relies on external services (e.g., databases), ensure those services are running and accessible.

2. How would you troubleshoot Docker container crashes?

  • Answer:
    If a container is repeatedly crashing, I would:
    1. Check logs (docker logs <container-id>) for errors or misconfigurations.
    2. Review resource usage like CPU or memory limits (docker stats <container-id>) to ensure the container isn’t exceeding its allocated resources.
    3. Check for issues in the Dockerfile, such as incorrect dependencies or missing files.
    4. Ensure that network configurations and volume mounts are correctly set up.
    5. If using a Docker Compose setup, confirm that all services and their dependencies are properly linked.

3. Why might a Docker image build fail?

  • Answer:
    A Docker image build might fail due to several reasons:
    1. Incorrect base image or an unsupported version in the Dockerfile.
    2. Missing dependencies or incorrect paths in the application being copied into the image.
    3. Build context issues, such as missing files or incorrect paths.
    4. Problems with networking during the RUN commands, such as failing to download dependencies from remote repositories.
    5. Errors in Dockerfile syntax or commands (e.g., incorrect commands or bad formatting).

4. How would you troubleshoot "Cannot Connect to Docker Daemon" errors?

  • Answer:
    To troubleshoot a "Cannot Connect to Docker Daemon" error:
    1. Ensure the Docker daemon is running by using systemctl status docker (Linux) or checking the Docker service in the system tray (Windows/macOS).
    2. Check if the user has the necessary permissions to interact with Docker. On Linux, add the user to the docker group: sudo usermod -aG docker <username>.
    3. Confirm that the Docker socket file (/var/run/docker.sock) exists and has correct permissions.
    4. Restart Docker service using sudo systemctl restart docker or the equivalent command for the platform.

5. What would you do if you receive "ImagePullBackOff" error for a container in Kubernetes?

  • Answer:
    The "ImagePullBackOff" error typically occurs when Kubernetes cannot pull the required image. To resolve this:
    1. Check the pod logs using kubectl describe pod <pod-name> for more details on the error.
    2. Verify that the image name and tag in the pod specification are correct.
    3. Ensure that the container registry credentials (stored as secrets or in a config map) are correctly configured.
    4. Check if there are network issues or access restrictions preventing the image from being pulled.
    5. If the image is private, ensure that the imagePullSecrets are correctly configured in the deployment YAML.

6. How would you debug a Docker container that's failing to connect to a database service?

  • Answer:
    If a Docker container is failing to connect to a database service, I would:
    1. Verify that the database container/service is running and accessible from within the Docker network (docker ps).
    2. Confirm that the correct network configuration is being used, and the container can reach the database host.
    3. Check the environment variables in the application (e.g., database hostname, port, username, and password).
    4. Inspect the logs of both the container and database to identify any specific errors or access issues.
    5. If applicable, test the database connection using a network tool like telnet or nc from inside the container.

7. What would you do if your Docker container cannot access the internet?

  • Answer:
    To troubleshoot internet connectivity issues in a Docker container:
    1. Check the network mode the container is running in (e.g., bridge, host). Ensure the correct network mode is used to allow external access.
    2. Test the DNS resolution inside the container by pinging a known external domain (e.g., ping google.com).
    3. Inspect the Docker daemon’s network settings and ensure the container’s network interface is properly configured.
    4. Check the firewall and iptables rules on the host machine, as they may block outbound traffic from containers.
    5. Test the host machine’s internet connectivity to ensure there’s no network issue preventing Docker from accessing the internet.

8. How would you troubleshoot "Cannot Allocate Memory" errors in Docker containers?

  • Answer:
    If a Docker container is running into memory allocation issues, I would:
    1. Check the container logs for memory-related errors (e.g., docker logs <container-id>).
    2. Review the memory limits set in the container configuration. If no limit is set, Docker may not allocate enough resources.
    3. Inspect the system’s available memory (free -m or top) to see if the host is running low on resources.
    4. Verify that the application inside the container is optimized and not consuming excessive memory.
    5. Consider increasing the memory limit for the container or adjusting the workload to reduce memory consumption.

9. What are some common performance issues in Docker containers, and how would you address them?

  • Answer:
    Common performance issues in Docker containers include:
    1. High CPU or Memory Usage: Monitor the container’s resource consumption using docker stats. Optimize the application to use fewer resources, or increase the container's limits.
    2. Slow File I/O: Use volume mounts wisely, as performance can degrade if the volume is mounted from a slow storage system.
    3. Networking Latency: Review the container’s network configuration. Switching to host networking may help in some cases.
    4. Excessive Disk Usage: Clean up unused images, containers, and volumes (docker system prune).
    5. Container Restarts: Investigate the container's logs and resource usage to identify the cause of crashes.

  1. Docker Version:

    docker --version
    • Purpose: Check the installed Docker version.
  2. List Docker Images:

    docker images
    • Purpose: View a list of all available Docker images.
  3. List Running Containers:

    docker ps
    • Purpose: View a list of all running containers.
  4. List All Containers (Including Stopped):

    docker ps -a
    • Purpose: List both running and stopped containers.
  5. Pull Docker Image:

    docker pull <image_name>
    • Purpose: Download an image from Docker Hub or a private repository.
  6. Build Docker Image:

    docker build -t <image_name> .
    • Purpose: Build an image from a Dockerfile in the current directory.
  7. Run Docker Container:

    docker run -d --name <container_name> <image_name>
    • Purpose: Start a container from an image in detached mode.
  8. Inspect a Container:

    docker inspect <container_name_or_id>
    • Purpose: Get detailed information about a container (network settings, volume mounts, etc.).
  9. Stop a Running Container:

    docker stop <container_name_or_id>
    • Purpose: Stop a running container.
  10. Remove a Stopped Container:

    docker rm <container_name_or_id>
    • Purpose: Remove a stopped container.
  11. Remove a Docker Image:

    docker rmi <image_name_or_id>
    • Purpose: Remove an image from the local repository.
  12. View Container Logs:

    docker logs <container_name_or_id>
    • Purpose: View logs of a running or stopped container.
  13. Access Running Container (Interactive Shell):

    docker exec -it <container_name_or_id> bash
    • Purpose: Access a running container’s shell for troubleshooting or configuration.
  14. View Docker System Information:

    docker info
    • Purpose: Get a summary of Docker system information, including the number of containers and images.
  15. Docker System Prune (Clean Up):

    docker system prune
    • Purpose: Clean up unused containers, images, volumes, and networks.