Skip to main content

Top Docker Commands Every User Should Master

· 4 min read

Docker commands are instructions used within the Docker ecosystem to interact with the Docker Engine and manage containerized applications. They facilitate various tasks, such as building, running, stopping, inspecting, and removing containers and images. These commands allow users to control Docker's behavior, create and manage containers, handle images, networks, volumes, and orchestrate multi-container applications using tools like Docker Compose. Docker commands are executed via the command-line interface (CLI) and offer a comprehensive set of functionalities to efficiently work with Dockerized environments.

1. Docker Images

Mastering these commands enables effective management of Docker images, including building, pulling, tagging, deleting, inspecting, and handling image history, ultimately streamlining your Docker image workflow.

1.1. docker build

  1. Usage: docker build [OPTIONS] PATH | URL | -
  2. Explanation: Builds a Docker image from a Dockerfile in the specified path or URL.
  3. Example: docker build -t myimage:latest .

1.2. docker pull

  1. Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
  2. Explanation: Fetches an image from a registry, such as Docker Hub.
  3. Example: docker pull ubuntu:latest

1.3. docker images

  1. Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
  2. Explanation: Lists all local images available on the Docker host.
  3. Example: docker images

1.4. docker rmi

  1. Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
  2. Explanation: Deletes one or more specified images from the local machine.
  3. Example: docker rmi myimage:latest

1.5. docker tag

  1. Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
  2. Explanation: Assigns a new tag to an image.
  3. Example: docker tag myimage:latest myrepo/myimage:latest

1.6. docker save

  1. Usage: docker save [OPTIONS] IMAGE [IMAGE...]
  2. Explanation: Saves one or more images to a tar archive (streaming to STDOUT by default).
  3. Example: docker save -o image_backup.tar myimage:latest

1.7. docker load

  1. Usage: docker load [OPTIONS]
  2. Explanation: Loads an image from a tar archive with STDIN as the source.
  3. Example: docker load -i image_backup.tar

1.8. docker history

  1. Usage: docker history [OPTIONS] IMAGE
  2. Explanation: Displays the history of an image including the image layers.
  3. Example: docker history myimage:latest

1.9. docker inspect

  1. Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]
  2. Explanation: Returns low-level information on Docker objects like containers, networks, and images.
  3. Example: docker inspect myimage

1.10. docker prune

  1. Usage: docker image prune [OPTIONS]
  2. Explanation: Removes dangling images to free up disk space.
  3. Example: docker image prune

2. Docker Containers

These commands cover the essential aspects of creating, listing, starting, stopping, and managing Docker containers, offering control and insights into container activities and configurations.

2.1. docker run

  1. Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  2. Explanation: Creates and starts a new container from a specified image. Allows customization with various options.
  3. Example: docker run -d --name my_container nginx

2.2. docker ps

  1. Usage: docker ps [OPTIONS]
  2. Explanation: Lists active containers.
  3. Example: docker ps

2.3. docker stop

  1. Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
  2. Explanation: Stops one or more running containers gracefully by sending a SIGTERM.
  3. Example: docker stop container_name

2.4. docker start

  1. Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]
  2. Explanation: Starts one or more stopped containers.
  3. Example: docker start container_name

2.5. docker restart

  1. Usage: docker restart [OPTIONS] CONTAINER [CONTAINER...]
  2. Explanation: Stops and then starts a container.
  3. Example: docker restart container_name

2.6. docker exec

  1. Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
  2. Explanation: Executes a command within a running container.
  3. Example: docker exec -it container_name bash

2.7. docker rm

  1. Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
  2. Explanation: Removes one or more containers. Use -f to force removal.
  3. Example: docker rm container_name

2.8. docker logs

  1. Usage: docker logs [OPTIONS] CONTAINER
  2. Explanation: Fetches the logs of a container.
  3. Example: docker logs container_name

2.9. docker inspect

  1. Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]
  2. Explanation: Displays detailed information on one or more containers.
  3. Example: docker inspect container_name

2.10. docker top

  1. Usage: docker top CONTAINER [ps OPTIONS]
  2. Explanation: Displays the running processes of a container.
  3. Example: docker top container_name

3. Docker Compose

Docker Compose is a powerful tool for defining and running multi-container Docker applications. These commands allow easy management, orchestration, and monitoring of multi-container applications defined in a Docker Compose YAML file, streamlining development and deployment workflows.

3.1. docker-compose up

  1. Usage: docker-compose up [options] [SERVICE...]
  2. Explanation: Builds, (re)creates, starts, and attaches to containers for a service defined in the docker-compose.yml file.
  3. Example: docker-compose up -d starts containers in detached mode.

3.2. docker-compose down

  1. Usage: docker-compose down [options]
  2. Explanation: Stops and removes containers, networks, volumes, and images created by docker-compose up.
  3. Example: docker-compose down -v removes volumes along with containers.

3.3. docker-compose build

  1. Usage: docker-compose build [options] [SERVICE...]
  2. Explanation: Builds or rebuilds services defined in the docker-compose.yml file.
  3. Example: docker-compose build service_name builds a specific service.

3.4. docker-compose start

  1. Usage: docker-compose start [SERVICE...]
  2. Explanation: Starts existing services defined in the docker-compose.yml file.
  3. Example: docker-compose start service_name starts a specific service.

3.5. docker-compose stop

  1. Usage: docker-compose stop [SERVICE...]
  2. Explanation: Stops running services without removing them.
  3. Example: docker-compose stop service_name stops a specific service.

3.6. docker-compose restart

  1. Usage: docker-compose restart [SERVICE...]
  2. Explanation: Restarts services defined in the docker-compose.yml file.
  3. Example: docker-compose restart service_name restarts a specific service.

3.7. docker-compose logs

  1. Usage: docker-compose logs [options] [SERVICE...]
  2. Explanation: Displays logs from services.
  3. Example: docker-compose logs -f service_name shows logs and follows them in real-time.

3.8. docker-compose exec

  1. Usage: docker-compose exec [options] SERVICE COMMAND [ARGS...]
  2. Explanation: Executes a command in a running container of a service.
  3. Example: docker-compose exec service_name bash opens a bash shell in a specific service container.

3.9. docker-compose ps

  1. Usage: docker-compose ps [options] [SERVICE...]
  2. Explanation: Lists services with their status.
  3. Example: docker-compose ps shows the status of all services.

3.10. docker-compose config

  1. Usage: docker-compose config [options]
  2. Explanation: Validates and displays the configuration of the docker-compose.yml file.
  3. Example: docker-compose config validates and displays the YAML configuration.