Fatskills
Practice. Master. Repeat.
Study Guide: Docker Docker Images and Containers Containers docker run docker ps docker stop docker rm docker logs docker exec docker attach
Source: https://www.fatskills.com/kubernetes/chapter/docker-docker-images-and-containers-containers-docker-run-docker-ps-docker-stop-docker-rm-docker-logs-docker-exec-docker-attach

Docker Docker Images and Containers Containers docker run docker ps docker stop docker rm docker logs docker exec docker attach

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. Core Command / Concept

Run a Docker container from an image. docker run

2. Step-by-Step

  1. Check if Docker is installed and running: docker --version
  2. Pull a Docker image from Docker Hub: docker pull ubuntu
  3. Run a new container from the pulled image: docker run -it ubuntu
  4. Check the running containers: docker ps
  5. Stop the container: docker stop <container_id>
  6. Remove the stopped container: docker rm <container_id>
  7. Check the container logs: docker logs <container_id>
  8. Clean up all stopped containers: docker system prune -a ⚠️ This deletes all stopped containers.

3. Real Commands with Examples


Example 1: Run a container with a specific command

  • Purpose: Run a container and execute a command inside it.
  • Command: docker run -it ubuntu /bin/bash
  • Brief explanation of flags: -it flag allows interactive mode and allocates a pseudo-TTY.

Example 2: Run a container with a volume mount

  • Purpose: Mount a local directory inside the container.
  • Command: docker run -it -v /local/dir:/container/dir ubuntu /bin/bash
  • Brief explanation of flags: -v flag mounts a volume, and the format is host_dir:container_dir.

Example 3: Run a container with a port mapping

  • Purpose: Map a host port to a container port.
  • Command: docker run -it -p 8080:80 ubuntu /bin/bash
  • Brief explanation of flags: -p flag maps a port, and the format is host_port:container_port.

4. Common Errors


Error 1: Unable to pull the image

  • Error message or symptom: Error: unable to pull image
  • Why it happens: The image does not exist or is not accessible.
  • One-line fix: docker pull <image_name> or check the image name.

Error 2: Container not found

  • Error message or symptom: Error: No such container
  • Why it happens: The container does not exist or has been removed.
  • One-line fix: Check the container ID or recreate the container.

Error 3: Port already in use

  • Error message or symptom: Error: port is already allocated
  • Why it happens: Another process is using the same port.
  • One-line fix: Use a different port or stop the other process.

5. Quick Checks


Scenario 1: Check if a container is running

  • Situation: You want to check if a container is running.
  • Command to run: docker ps
  • What a correct response looks like: A list of running containers.

Scenario 2: Check the container logs

  • Situation: You want to check the logs of a container.
  • Command to run: docker logs <container_id>
  • What a correct response looks like: The container logs.

Scenario 3: Check the Docker version

  • Situation: You want to check the Docker version.
  • Command to run: docker --version
  • What a correct response looks like: The Docker version number.

6. Last-Minute Reference

docker run flags: -it, -v, -p, -d, -rm • Default Docker port: 2375 • Docker Hub default repository: docker.io • Docker image format: docker pull <image_name>:<tag> • Container ID format: <container_id>[:<name>] • Docker system prune flags: -a, -f • ⚠️ Docker system prune deletes all stopped containers.
• ⚠️ Docker system prune -f forces the prune operation without prompting.
• ⚠️ Docker system prune -a removes all unused data.
• Docker logs format: docker logs -f <container_id> • Docker logs follow flag: -f




ADVERTISEMENT