Infrastructure
Docker Commands

In this guide, we'll cover some of the most essential Docker commands, providing examples and commonly used flags to help you navigate Docker with ease. From running containers to managing networks, these commands will serve as your toolkit for effective Docker usage.

1. docker images

Purpose: Lists all Docker images on your local machine.

Example

docker images  

Widely Used Flags:

  • -a or --all: Show all images, including intermediate images.
  • -q or --quiet: Only display image IDs.
2. docker build

Purpose: Builds an image from a Dockerfile.

Example

docker build -t image_name:tag . 

In the place of image_name use the name of the image you build with and give the tag number and . dot represents the current directory If your Dockerfile is in a different directory, replace the . with the path to that directory.

For example:

docker build -t image_name:tag /path/to/your/dockerfile-directory

Widely Used Flags:

  • -t or --tag: Tag an image with a name.
  • --no-cache: Build the image without using cache.
3. docker pull

Purpose: Pulls an image from a Docker registry (like Docker Hub).By default, it pulls the latest image, but you can also mention the version of the image.

Example

$ docker pull  

Widely Used Flags:

  • --all-tags: Pull all tags of the image.
4. docker run

Purpose: The docker run command creates and starts a container from a specified image. If the image isn't available locally, it pulls the image first.

Example

$ docker run 
To give name of container
$ docker run --name  

Widely Used Flags:

  • -d or --detach: Run container in the background.
  • -p or --publish: Map container ports to host ports.
  • --name: Assign a name to the container.
  • -e or --env: Set environment variables in the container.
  • -v or --volume: Mount a host directory or file into the container.
5. docker ps

Purpose: Lists running containers.

Example

docker ps  

To list all containers, including stopped ones:

$ docker ps [options..]  

Widely Used Flags:

  • -a or --all: Show all containers, not just running ones.
  • -q or --quiet: Only display container IDs.
  • -l or --label: Shows only the Id of the containers.
6. docker network create

Purpose: Creates a new Docker network.

Example

docker network create   

Widely Used Flags:

  • --driver: Specify the network driver (e.g., bridge, overlay).
  • --subnet: Define a custom subnet for the network.
7. docker volume create

Purpose: Creates a new Docker volume.

Example

docker volume create   

Widely Used Flags:

  • --driver: Specify the volume driver.
  • --label: Add metadata to the volume.
8. docker start

Purpose: Starts one or more stopped containers.

Example:

$ docker start 

Widely Used Flags:

  • -a or --attach: Attach to the container’s output.
9. docker restart

Purpose: Stops and then starts one or more running containers.

Example

docker restart 

This command restarts the container named my_container. Widely Used Flags:

  • -tor --time: Specify the number of seconds to wait for the container to stop before killing it.
10. docker stop

Purpose: Stops a running container.

Example

$ docker stop   

Widely Used Flags:

  • -t or --time: Seconds to wait for stop before killing the container.
11. docker rm

Purpose: To delete a container, you can use either its ID or its automatically assigned name (e.g., confident_boyd, heuristic_villani).

Example

 docker rm {options} 

Widely Used Flags:

  • -f or --force: Force the removal of a running container.
  • -l or --link: Remove the specific link mentioned.
12. docker rmi

Purpose: Removes one or more Docker images.

Example

docker rmi 

To remove all unused images:

docker rmi $(docker images -q)  

Widely Used Flags:

  • --no-prune: Don’t delete untagged parent images.
13. docker stats

Purpose: Displays a live stream of container(s) resource usage statistics.

Example

docker stats  

This command shows real-time metrics for all running containers.

Widely Used Flags:

  • --no-stream: Display the statistics only once instead of continuously streaming.
  • --format: Format the output using a Go template.
14. docker exec

Purpose: This command opens an interactive terminal session inside a running container named container_name, allowing you to execute commands directly within the container.

Example

docker exec -it container_name /bin/bash 

Widely Used Flags:

  • -i: It will keep STDIN open even when not attached.
  • -d: For running the commands in the background.
  • -e: Sets the environment variables.
15. docker logs

Purpose: Fetches the logs of a container.

Example

docker logs   

Widely Used Flags:

  • -f or --follow: Follow log output.
  • --tail: Show only the most recent log lines.
Have a doubt?
Post it here, our mentors will help you out.