Introduction

In this article, we are going to explore frequently used docker commands with examples.

Docker Commands

1) To list out the available commands in the installed docker version:

docker --help

2) To check the information and options about the particular command:

Syntax : docker <command_name> --help

docker run --help

3) To get the currently installed version of docker:

docker –-version

4) To login to the local registry or docker hub repository:

Syntax : docker login --username=<user_name> --password=<password>

docker login --username=testuser --password=password

5) To list out the all locally stored docker images:

docker images ls

6) To pull an image from the docker repository (https://hub.docker.com/):

Syntax : docker pull <image_name:version>

docker pull python

7) To push an image to the docker repository:

Syntax : docker push <image_name:version>

docker push testimage:1.0

8) To build an image from the DockerFile in the current directory and tag the image:

Syntax : docker build <DockerFile_path> -t <image_name:version>

docker build . -t testimage:1.0

9) To delete an image from the local image store:

Syntax : docker image rmi <image_name:version>

docker image rmi testimage:1.0

10) To retag the local image with a new image name and tag:

Syntax : docker tag <image_name:version> <new_image_name:new_version>

docker tag testimage:1.0 myimage:2.0 

11) To display the history of images:

Syntax : docker history <image_name>

docker history testimage

12) To search the Docker Hub for images:

Syntax : docker search <search_term>

docker search dotnet

13) To create a container from an image:

Syntax : docker run -name <container_name> -it -d <image_name:version>

docker run testcontainer -it -d testimage:1.0

14) To list out the all running containers:

docker ps  (or) docker container ls

15) To list out the both all running and exited containers:

docker ps -a

16) To run the stopped container:

Syntax : docker start <container_name or container_id>

docker start testcontainer

17) To stop one or more running containers:

Syntax : docker stop <container_name or container_id>

docker stop testcontainer
docker stop testcontainer1 testcontainer2

18) To kill the container. If the container is in a running state then it will stop and kill the container:

Syntax : docker kill <container_name or container_id>

docker kill testcontainer

19) To remove one or more stopped containers:

Syntax : docker rm <container_name or container_id>

docker rm testcontainer
docker rm testcontainer1 testcontainer2

20) To delete all running and stopped containers (force stop):

docker container rm -f $(docker ps -aq)

21) To remove all stopped containers:

docker container prune

22) To restart one or more containers:

Syntax : docker restart <container_name or container_id>

docker restart testcontainer
docker restart testcontainer1 testcontainer2

23) To print the last 'N' lines of a container's log:

Syntax : docker container logs --tail <no_of_lines> <container_name or container_id>

docker container logs --tail 1000 testcontainer

24) To list port mappings of the container:

Syntax : docker port <container_name or container_id>

docker port testcontainer

25) To create a new image from edited container on the local system:

Syntax : docker commit <container_name or container_id> <image_name:version>

docker commit testcontainer testimage:2.0

26) To access the running container which means run a command in the running container:

Syntax : docker exec -it <container_name or container_id> bash

docker exec -it testcontainer bash

27) To create a new volume that containers can consume and store data in it:

Syntax : docker volume create <volume_name>

docker volume create testvolume

28) To remove one or more volumes:

Syntax : docker volume rm <volume_name>

docker volume rm testvolume

29) To copy files/folders between a container and the local file system:

Syntax : docker cp <src> <dest>

docker cp index.html testcontainer:/index.html
docker cp testcontainer:/index.html index.html

30) To get the low-level information on Docker objects:

Syntax : docker inspect <object_name or id>

docker inspect testcontainer

31) To display system-wide information:

docker info

32) To lists the details of all the networks in the cluster:

docker network ls

33) To logging out from dockerhub:

docker logout

Hope you like it and know about the list of docker commands which are frequently used. Thank you.