Fatskills
Practice. Master. Repeat.
Study Guide: Docker Docker Images and Containers Container Lifecycle Created Running Paused Stopped Deleted
Source: https://www.fatskills.com/kubernetes/chapter/docker-docker-images-and-containers-container-lifecycle-created-running-paused-stopped-deleted

Docker Docker Images and Containers Container Lifecycle Created Running Paused Stopped Deleted

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

⏱️ ~2 min read

1. Core Command / Concept

Create, manage, and delete containers. docker run.

2. Step-by-Step

  1. Check if Docker is running: docker ps -a
    Output: List of all containers, including stopped ones.
  2. Create a new container from an image: docker run -it --name mycontainer ubuntu
    Output: Container ID and a new shell prompt.
  3. List all running containers: docker ps
    Output: List of running containers.
  4. Pause a running container: docker pause mycontainer
    Output: Container is paused.
  5. ⚠️ Stop a running container: docker stop mycontainer
    Output: Container is stopped.
  6. Delete a stopped container: docker rm mycontainer
    Output: Container is deleted.
  7. Clean up all stopped containers: docker container prune
    Output: List of deleted containers.

3. Real Commands with Examples

  1. Purpose: Run a new container with a web server.
    Command: docker run -d --name webserver -p 8080:80 nginx
    Flags: -d runs container in detached mode, -p maps port 8080 on host to port 80 in container, --name sets container name.
  2. Purpose: Run a new container with a shell and attach to it.
    Command: docker run -it --name myshell ubuntu
    Flags: -it allocates a pseudo-TTY and keeps the container running even after you detach.
  3. Purpose: Run a new container from a specific image and set environment variables.
    Command: docker run -e MY_VAR=hello -it --name mycontainer ubuntu
    Flags: -e sets environment variable MY_VAR to hello.

4. Common Errors

  1. Error: docker: Error response from daemon: Container cannot be paused: container is not running.
    Why: Container is not running.
    Fix: docker start mycontainer then docker pause mycontainer.
  2. Error: docker: Error response from daemon: Container cannot be stopped: container is not running.
    Why: Container is not running.
    Fix: docker start mycontainer then docker stop mycontainer.
  3. Error: docker: Error response from daemon: Container mycontainer is not running.
    Why: Container does not exist.
    Fix: docker container ls -a to check if container exists.

5. Quick Checks

  1. Situation: Container is running but not responding.
    Command: docker logs -f mycontainer
    Correct response: Container logs are displayed.
  2. Situation: Container is paused but not stopped.
    Command: docker ps -a
    Correct response: Container is listed as paused.
  3. Situation: Container is deleted but still running.
    Command: docker container ls -a
    Correct response: Container is not listed.

6. Last-Minute Reference

  • docker run command: docker run [options] image [command] [args...]
  • Default port mapping: docker run -p 8080:80
  • Environment variables: docker run -e MY_VAR=hello
  • Container names: docker run --name mycontainer
  • Container IDs: docker ps -q
  • Container logs: docker logs -f mycontainer
  • ⚠️ docker container prune deletes all stopped containers.
  • ⚠️ docker system prune deletes all stopped containers, networks, and images.
  • ⚠️ docker volume prune deletes all unused volumes.
  • docker version command: docker version
  • docker info command: docker info


ADVERTISEMENT