Fatskills
Practice. Master. Repeat.
Study Guide: Docker Docker Images and Containers Running vs Exited Containers Autoremoval --rm Interactive Mode -it Detached Mode -d
Source: https://www.fatskills.com/kubernetes/chapter/docker-docker-images-and-containers-running-vs-exited-containers-autoremoval-rm-interactive-mode-it-detached-mode-d

Docker Docker Images and Containers Running vs Exited Containers Autoremoval --rm Interactive Mode -it Detached Mode -d

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~3 min read

1. Running vs Exited Containers, Auto-removal (--rm), Interactive Mode (-it), Detached Mode (-d)

Understand the lifecycle of containers and how to manage them with flags.

docker run

2. Step-by-Step

  1. Start a new container from an image: docker run -it ubuntu /bin/bash
  2. Check the container is running: docker ps
  3. Output: Container ID and image name
  4. ⚠️ Stop the container: docker stop <container_id>
  5. Check the container is stopped: docker ps -a
  6. Output: Container ID, image name, and status (exited)
  7. ⚠️ Remove the stopped container: docker rm <container_id>
  8. Check the container is removed: docker ps -a
  9. Output: Container ID is no longer listed
  10. Run a container with auto-removal: docker run --rm -it ubuntu /bin/bash
  11. Check the container is removed after stopping: docker ps -a
  12. Output: Container ID is no longer listed
  13. Run a container in detached mode: docker run -d ubuntu /bin/bash
  14. Check the container is running in detached mode: docker ps
  15. Output: Container ID and image name
  16. Run a container in interactive mode: docker run -it ubuntu /bin/bash
  17. Check the container is running in interactive mode: docker ps
  18. Output: Container ID and image name

3. Real Commands with Examples

  1. Purpose: Run a container with auto-removal and interactive mode.
    Command: docker run --rm -it ubuntu /bin/bash
    Flags: --rm removes the container after stopping, -it enables interactive mode.
  2. Purpose: Run a container in detached mode.
    Command: docker run -d ubuntu /bin/bash
    Flags: -d runs the container in detached mode.
  3. Purpose: Run a container with a specific command.
    Command: docker run -it ubuntu ls -l
    Flags: -it enables interactive mode, ls -l runs the ls command with long format.

4. Common Errors

  1. Error: docker: Error response from daemon: Container <container_id> is not running.
    Why: The container is stopped or removed.
    Fix: docker start <container_id> or docker run <image_name>.
  2. Error: docker: Error response from daemon: Container <container_id> is not found.
    Why: The container is removed.
    Fix: docker run --rm <image_name>.
  3. Error: docker: Error response from daemon: Container <container_id> is already running.
    Why: The container is already running.
    Fix: docker stop <container_id> and then docker rm <container_id>.

5. Quick Checks

  1. Situation: Container is running and interactive.
    Command: docker ps
    Correct response: Container ID and image name.
  2. Situation: Container is stopped and removed.
    Command: docker ps -a
    Correct response: Container ID is no longer listed.
  3. Situation: Container is running in detached mode.
    Command: docker ps
    Correct response: Container ID and image name.

6. Last-Minute Reference

  • ⚠️ --rm removes the container after stopping.
  • docker run starts a new container.
  • -it enables interactive mode.
  • -d runs the container in detached mode.
  • docker ps lists running containers.
  • docker ps -a lists all containers.
  • docker stop stops a container.
  • docker rm removes a stopped container.
  • docker start starts a stopped container.
  • ? Verify with 'docker version' for changes in Docker versions.


ADVERTISEMENT