Basic Docker CLI Commands


docker run

Run a new container from docker image

Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Options: -a,-c,-d,-e,-h,-i,-l,-m,-p,-P,-t,-u,-v,-w

But Some of the commonly used options are:

-d or --detach  - Run the container in the background
-p or --publish - Publish a container's port(s) to the host
-e or --env     - Set environment variables
-v or --volume  - Bind mount a volume
--name          - Assign a name to the container

Example: 
$ docker run hello-world

docker ps

Usage:  docker ps [OPTIONS]

List containers

Options:
-a, --all             Show all containers (default shows just running)

-f, --filter filter   Filter output based on conditions provided
    --format string   Pretty-print containers using a Go template

-n, --last int        Show n last created containers (includes all  
                      states(default -1)

-l, --latest          Show the latest created container (includes all 
                      states)
    --no-trunc        Don't truncate output

-q, --quiet           Only display container IDs

-s, --size            Display total file sizes


Example: 

$ docker ps -l
CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                     PORTS     NAMES
5cbc96681f4e   hello-world   "/hello"   7 minutes ago   Exited (0) 7 minutes ago             wizardly_ritchie

docker images

Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]

List images

Options:
-a, --all             Show all images (default hides intermediate 
                      images)
    --digests         Show digests

-f, --filter filter   Filter output based on conditions provided
    --format string   Pretty-print images using a Go template
    --no-trunc        Don't truncate output
  
-q, --quiet           Only show image IDs


Example: 

$ docker images
REPOSITORY                                    TAG             IMAGE ID       CREATED         SIZE
hello-world                                   latest          feb5d9fea6a5   16 months ago   13.3kB

docker image

Usage:  docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile

  history     Show the history of an image

  import      Import the contents from a tarball to create a 
              filesystem image

  inspect     Display detailed information on one or more images

  load        Load an image from a tar archive or STDIN

  ls          List images

  prune       Remove unused images

  pull        Pull an image or a repository from a registry

  push        Push an image or a repository to a registry

  rm          Remove one or more images

  save        Save one or more images to a tar archive (streamed to 
              STDOUT by default)

  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE


Example: 

$ docker image history feb5d9fea6a5
IMAGE          CREATED         CREATED BY                                      SIZE      COMMENT
feb5d9fea6a5   16 months ago   /bin/sh -c #(nop)  CMD ["/hello"]               0B
<missing>      16 months ago   /bin/sh -c #(nop) COPY file:50563a97010fd7ce…   13.3kB

docker container

Usage:  docker container COMMAND

Manage containers

Commands:
  attach      Attach local standard input, output, and error streams 
              to a running container

  commit      Create a new image from a container's changes

  cp          Copy files/folders between a container and the local 
              filesystem

  create      Create a new container

  diff        Inspect changes to files or directories on a container's 
              filesystem

  exec        Run a command in a running container

  export      Export a container's filesystem as a tar archive

  inspect     Display detailed information on one or more containers

  kill        Kill one or more running containers

  logs        Fetch the logs of a container

  ls          List containers

  pause       Pause all processes within one or more containers

  port        List port mappings or a specific mapping for the 
              container

  prune       Remove all stopped containers

  rename      Rename a container

  restart     Restart one or more containers

  rm          Remove one or more containers

  run         Run a command in a new container

  start       Start one or more stopped containers

  stats       Display a live stream of container(s) resource usage 
              statistics

  stop        Stop one or more running containers

  top         Display the running processes of a container

  unpause     Unpause all processes within one or more containers

  update      Update configuration of one or more containers

  wait        Block until one or more containers stop, then print  
              their exit codes

Example: 

$ docker container logs feb5d9fea6a5

docker system

Usage:  docker system COMMAND

Manage Docker

Commands:
  df          Show docker disk usage
  events      Get real time events from the server
  info        Display system-wide information
  prune       Remove unused data

Example 1: 

$ docker system prune

WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all dangling images
        - all build cache
Are you sure you want to continue? [y/N] y

Deleted Containers:
f44f9b81948b3919590d5f79a680d8378f1139b41952e219830a33027c80c867
792776e68ac9d75bce4092bc1b5cc17b779bc926ab04f4185aec9bf1c0d4641f

Deleted Networks:
network1
network2

Deleted Images:
untagged: hello-world@sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f
deleted: sha256:1815c82652c03bfd8644afda26fb184f2ed891d921b20a0703b46768f9755c57
deleted: sha256:45761469c965421a92a69cc50e92c01e0cfa94fe026cdd1233445ea00e96289a

Total reclaimed space: 1.84kB

Example 2:
Clean up unused and dangling images
$ docker image prune

Example 3:
Clean up dangling images only
$ docker image prune -a

Example 4:
Clean up stopped containers
$ docker container prune

Example 5:
Clean up unused volumes
$ docker volume prune

docker compose

Usage:  docker compose [OPTIONS] COMMAND

Docker Compose

Options:
-f, --file FILE             Specify an alternate compose file
                            (default: docker-compose.yml)

-p, --project-name NAME     Specify an alternate project name
                            (default: directory name)

Commands:
  build      Build or rebuild services
  bundle     Generate a Docker bundle from the Compose file
  config     Validate and view the Compose file
  create     Create services
  down       Stop and remove containers, networks, images, and volumes
  events     Receive real time events from containers
  exec       Execute a command in a running container
  help       Get help on a command
  images     List images
  kill       Kill containers
  logs       View output from containers
  pause      Pause services
  port       Print the public port for a port binding
  ps         List containers
  pull       Pull service images
  push       Push service images
  restart    Restart services
  rm         Remove stopped containers
  run        Run a one-off command
  scale      Set number of containers for a service
  start      Start services
  stop       Stop services
  top        Display the running processes
  unpause    Unpause services
  up         Create and start containers
  version    Show the Docker Compose version information


Example 1: 
Run default docker-compose.yml file from the current directory
$ docker compose up

Example 2:
Run default docker-compose.yml file from the current directory in detached mode. Basically it will run containers in the background.
$ docker compose up -d

Example 3:
Run specific docker-compose-kafka.yml file from the current directory
$ docker compose -f docker-compose-kafka.yml up

Example 4:
Run specific docker-compose-kafka.yml file from the current directory in detached mode. Basically it will run kafka containers in the background.
$ docker compose -f docker-compose-kafka.yml up

Reference

https://docs.docker.com/reference/