Areeb Abdul Ghani
Areeb Abdul Ghani's blog

Follow

Areeb Abdul Ghani's blog

Follow
An extensive guide on using Docker commands related to Docker containers, images, networks, and volumes

An extensive guide on using Docker commands related to Docker containers, images, networks, and volumes

Areeb Abdul Ghani's photo
Areeb Abdul Ghani
·Sep 5, 2022·

5 min read

• List all the Running Containers

docker ps

• List all the Containers (irrespective of the state)

docker ps -a

docker ps -all

• List all the Running Containers with the File Size

docker ps -s

docker container ls -s

• List the IDs of the Running Containers

docker ps -q

docker container ls -q

• List the IDs of all the Containers (irrespective of the state)

docker ps -a -q

docker ps -aq

• Filter container list

docker ps -f name=un

docker ps -a -f name=ar

docker ps -a -f status=running

• Creating a new Container using Docker Image

docker create <image_name>

docker container create <image_name>

docker create nginx

• Creating a new Container using Docker Image with some fixed name

docker create --name <container_name> <image_name>

docker container create --name <container_name> <image_name>

docker create --name nginx-container nginx

• Start a Docker Container

docker start <container_id or container_name>

docker start nginx-contianer

• Stop a running Docker Container

docker stop <container_id or container_name>

docker stop nginx-container

• Restart a Docker container

docker restart <container_id or container_name>

• Pause a running Container

docker pause <container_id or container_name>

docker pause nginx-container

• Unpause a paused Container

docker unpause <container_id or container_name>

docker container unpause <container_id or container_name>

docker unpause nginx-container

• Docker Run command

docker run <image_name>

docker container run <image_name>

• Docker Run command in Foreground and Detached Modes

docker container run nginx

docker ps

• Delete the container on the exited state

docker run --rm nginx

• Run the container in daemon mode

docker run -d <image_name>

• Run Docker Container with a name using the run command

docker container run -d --name <container_name> <image_name>

docker container run -d --name nginx-container nginx

• Listing Processes running in a Docker Container

docker top <container_name or container_id>

docker container top <container_name or container_id>

• Map ports of a Docker Container

docker container run --name <container_name? -d -p <host_post>:<container_port> <image_name>

docker container run --name nginx-container -d -p 8080:80 nginx

• Rename a Docker Container

docker rename <old_name> <new_name>

docker rename nginx-container nginx-cont

• Run the Docker Container in an Interactive Mode

docker container run -it <image_name> /bin/bash

docker container run -it nginx /bin/bash

• Get Inside the Running Container

docker exec -it <contaner_id or container_name> /bin/bash

docker run -d --name nginx-container nginx

• Start a Docker Container and keep it running

docker run -dt <image_name>

• Copy a File from a Container to a Host

docker cp <container_id or container_name>:<source_file_path> <destination_path>

• Copy a File from the Host to the Docker Container

docker cp <location_of_file_on_host> <container_id or container_name>:<file_desinaion>

• Remove a specific Docker Container

docker stop <container_name or container_id>

• Remove a Docker Container after it exits

docker run --rm <image_name>

• Delete all the Stopped Containers

docker container prune

• Delete all the Docker Containers

docker rm -f $(docker ps -a -q)

docker container rm -f $(docker ps -a -q)

• Create a Docker image from a Docker Container

docker container commit <container_id or container_name> <new_image_name>

• Run command inside the Docker Container

docker exec -it <container_id or container_name> <command>

• Set Environment Variable in a Docker Container

docker run --env ENV_VAR1=value1 --env ENV_VAR1=value2 --name <container_name> <image_name>

• Set Environment Variable in a Docker Container using a File

docker run --env-file <path_to_the_file> --name <container_name> <image_name>

Docker Image Commands:

• List all the Docker Images

docker images

• List the Docker Image Ids

docker images -q

• List all the Docker Images (including dangling images)

docker images -a

• Build a Docker Image

docker build -t <image_name> <contenxt_dir>

• Build Docker Images with a different tag

docker build . -t <image_name>:<tag or version>

• Build a Docker Image using a custom named Dockerfile

docker build -f <custom_docker_file_name> -t <image_name> .

• Build a Docker Image from a Dockerfile that is not in the Current Directory

docker build -f </path/to/dockerfilename> -t <image_name> .

• Show History of a Docker Image

docker history <imagename or imageid>

• Rename an existing Docker Image

docker tag <imagename> <newname>:<version>

• Remove Docker images

docker rmi <image_name or image_id>

• Force delete a Docker Image

docker rmi -f <image_name or image_id>

Unused Docker Images:

• Dangling Docker Images

docker build . -t imagename

• List Dangling Docker Images

docker images -f dangling=true

• Remove all the Dangling Docker Images

docker image prune

• Remove all the Dangling and Unused Docker Images

docker image prune -a

• Login to Docker

docker login

• Push a Docker Image to the Docker Registry

docker push repository_name/imagename:tag

• Download a Docker Image from the registry

docker pull imagename:tag

Docker logs:

• Get Logs of the Docker container

docker container logs <container_id or container_name>

• Monitor the Docker Container Logs

docker container logs -f <container_id or container_name>

• Get the last 2 lines of the Container Logs

docker container logs --tail 2 <container_id or container_name>

Docker Network Commands:

• List all the Networks

docker network ls

• Create a Network

docker network create --driver <driver-name> <bridge-name>

• Connect a Docker Container to a Network:

docker network create --driver <driver-name> <bridge-name>

• Connect a Docker Container to a Network on Start

docker run -d --network=<network_name or id> <contaienr_name>

• Disconnect a Docker container from a Network:

docker network disconnect <network_name_or_id> <container_name_or_id>

• Remove a Network

docker network rm <network_id or network_name>

• Show Information about one or more Networks

docker network inspect <network_id or network_name>

• Get the IP Address of the running Docker Container

sudo docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name or container_id>

Docker Volumes:

• Create Docker Volume

docker volume create --name volume-name

• List Docker Volumes

docker volume ls

• Mounting Docker Volume using the -v Flag

docker run -it --name <container-name> -v <volume-name>:<path-in-container-where-volume-is-mounted> <image-name>

• Mounting Docker Volume using the --mount Flag

docker run -it --name <container-name> --mount soure=<volume-name>, destination=<path-in-container-where-volume-is-mounted> <image-name>

• Get Details about a Docker Volume

docker volume inspect <volume-name>

• Remove a Docker Volume

docker volume rm <volume-name>

• Volume Mount using bind-mount

docker run -it -v /path/on/host:/path/in/contianer/where/volume/has/to/be/mounted <image-name>

• Creating Bind Mount Volume using the --mount flag

docker run -it --name <container_name> --mount type=bind,source=/path/on/host/,target=/path/on/container first-image

Docker System-wide Commands:

• Docker Info

docker info

• Docker Stats of the running Container

docker stats

• Docker Stats of all the Containers

docker stats --all

• Show the Docker Version

docker version

• Get Detailed Info about an Object (Container, Image, Volume, etc)

docker inspect <name or id>

docker inspect nginx

• Get the Summary of Docker Usage

docker system df

• Clean your Docker system

docker system prune

That's it for today guys!

• Thanks for reading and i would love to connect with you guys on Socials

• Any feedbacks are welcome!!

Did you find this article valuable?

Support Areeb Abdul Ghani by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this