Skip to content

Table of Core Docker Commands

Command Description
docker ps List only running containers.
docker ps -a List all containers (both running and stopped).
docker stop Stop one or more running containers (use container ID or name).
docker images List all Docker images stored locally.
docker rm Remove one or more containers (use container ID or name).
docker info Display system-wide information about Docker, including configuration and usage.

Docker Quick Reference Guide

Table of Contents

  1. Stop and Remove a Docker Container and Image
  2. Start an Existing Docker Container
  3. Delete an Image
  4. Check if Docker is Running
  5. Build a Docker Container from a Compose File
  6. Difference Between a Container and an Image

1. Stop and Remove a Docker Container and Image

  • List Running Containers:

    1
    docker ps
    
    Sample Output:
    1
    2
    3
    4
    5
    PS C:\Users\sf230> docker ps -a
    CONTAINER ID   IMAGE                      COMMAND                  CREATED      STATUS                    PORTS     NAMES
    0c3108fe5f41   coastseg_docker-coastseg   "/bin/bash"              3 days ago   Exited (137) 3 days ago             coastseg_docker-coastseg-1
    faf1957579a9   coastseg_docker-notebook   "pixi run --environm…"   6 days ago   Exited (137) 6 days ago             coastseg_docker-notebook-1
    PS C:\Users\sf230>
    

  • Stop a Container:

    1
    docker stop [container_name_or_id]
    
    Sample Output:
    1
    2
    3
    4
    5
    PS C:\Users\sf230> docker ps -a
    CONTAINER ID   IMAGE                      COMMAND                  CREATED      STATUS                    PORTS     NAMES
    0c3108fe5f41   coastseg_docker-coastseg   "/bin/bash"              3 days ago   Exited (137) 3 days ago             coastseg_docker-coastseg-1
    faf1957579a9   coastseg_docker-notebook   "pixi run --environm…"   6 days ago   Exited (137) 6 days ago             coastseg_docker-notebook-1
    PS C:\Users\sf230>
    

  • Remove a Container:

    1
    docker rm [container_name_or_id]
    
    Sample Output:
    1
    [container_name_or_id]
    

  • Remove an Image:

    1
    docker rmi [image_name_or_id]
    
    Sample Output:
    1
    2
    Untagged: [image_name_or_id]:latest
    Deleted: sha256:[image_id]
    
    Note: Ensure the container is stopped before attempting to remove it. If a container exists that was created from the image, remove the container first before deleting the image.

2. Start an Existing Docker Container

  • Start a Container:
    1
    docker start [container_name_or_id]
    
    Sample Output:
    1
    [container_name_or_id]
    
    This command resumes a previously created (and stopped) container.

3. Delete an Image

  • Delete an Image:
    1
    docker rmi [image_name_or_id]
    
    Sample Output:
    1
    2
    Untagged: [image_name_or_id]:latest
    Deleted: sha256:[image_id]
    

4. Check if Docker is Running

  • Check Docker Status:
    1
    docker info
    
    Sample Output:
    1
    2
    3
    4
    5
    6
      Client:
      Version:    27.5.1-rd
      Context:    desktop-linux
      Debug Mode: false
      Plugins:
    ...
    
    Important: If you're using Docker Desktop, ensure that Docker Desktop is running as it manages the Docker engine on your machine.

5. Build a Docker Container from a Compose File

  • Build and Start Containers in Detached Mode:
    1
    docker-compose up --build -d
    
    Sample Output:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
      PS C:\development\doodleverse\coastseg\Coastseg_docker> docker compose up -d
      Compose now can delegate build to bake for better performances
      Just set COMPOSE_BAKE=true
      [+] Building 158.4s (7/8)                                                                          docker:desktop-linux
      => [coastseg internal] load build definition from dockerfile                                                      0.0s
      => => transferring dockerfile: 891B                                                                               0.0s
      => [coastseg internal] load metadata for ghcr.io/prefix-dev/pixi:latest                                           1.9s
      => [coastseg internal] load .dockerignore                                                                         0.0s
      => => transferring context: 661B                                                                                  0.0s
      => [coastseg 1/4] FROM ghcr.io/prefix-dev/pixi:latest@sha256:100cab99fc052041039e92761a4adcec8fe0b2fadeb4cc292b9  2.3s
      => => resolve ghcr.io/prefix-dev/pixi:latest@sha256:100cab99fc052041039e92761a4adcec8fe0b2fadeb4cc292b9db525007d  0.0s
      => => sha256:f3bd00dddb19b7013a3301c622ec9c2d9f82028e37713a33b2d020c94a1e30f3 2.15kB / 2.15kB                     0.0s
      => => sha256:04cb8c9dec2ddb1fa01a179ca4c6c11e38836666a7195d16e6be9fd1c558275f 23.27MB / 23.27MB                   1.9s
      => => sha256:100cab99fc052041039e92761a4adcec8fe0b2fadeb4cc292b9db525007df2d2 683B / 683B                         0.0s
      => => sha256:acae18dbecdd31cf9449c6021590872f876721f4e86bed47beb4eec91b8074b4 703B / 703B                         0.0s
      => => extracting sha256:04cb8c9dec2ddb1fa01a179ca4c6c11e38836666a7195d16e6be9fd1c558275f                          0.3s
      => [coastseg internal] load build context                                                                        97.2s
      => => transferring context: 3.57GB                                                                               97.2s
      => [coastseg 2/4] WORKDIR /coastseg                                                                               0.1s
      => [coastseg 3/4] COPY ./ .                                                                                       4.3s
      => [coastseg 4/4] RUN pixi install --locked                                                                      54.9s
    

This command builds the images defined in the docker-compose.yml file and starts the containers in detached mode. Detached mode runs the containers in the background and returns the command prompt to the user.

  • Stop and Remove Containers:
    1
    docker-compose down
    
    Sample Output:
    1
    2
    3
    Stopping myapp_web_1 ... done
    Removing myapp_web_1 ... done
    Removing network myapp_default
    

This command stops and removes the containers defined in the docker-compose.yml file.

  • Connect to a Running Container:
    1
    docker exec -it [container_id_or_name] /bin/bash
    
    Sample Output:
    1
    root@123abc456def:/#
    

This command opens an interactive terminal session inside the running container.

Difference Between a Container and an Image

  • Docker Image: A read-only template that includes the application code, libraries, and dependencies. It serves as the blueprint for creating containers.

  • Docker Container: A running instance of an image that includes a writable layer. It represents the actual, isolated environment where the application runs.