Most commonly used docker commands with examples
Last Updated on :May 14, 2021
In this article we will go through the most commonly used docker commands with examples.
Docker Basic commands
Show the current version of docker
$ docker --version
Docker version 19.03.6, build 369ce74a3c
Show info like number of containers, number of images , server version etc
$ docker info
Client:
Debug Mode: false
Server:
Containers: 36
Running: 22
Paused: 0
Stopped: 14
Images: 119
Server Version: 19.03.6
Storage Driver: overlay2
--------------
--------------
--------------
Docker Container Commands
List running containers
docker container ls
OR
docker ps
List all containers (both running and not running containers)
$ docker container ls -a
Stop container , remove container and remove multiple container
# Stop a container
$ docker container stop CONTAINER_ID
# Remove a container that is not running
$ docker container rm CONTAINER_ID
# Remove a running container (with -f flag)
$ docker container rm -f CONTAINER_ID
# Remove multiple containers
$ docker container rm CONTAINER_ID_1 CONTAINER_ID_2 CONTAINER_ID_3
Check Container Logs
$ docker container logs CONTAINER_ID
OR
$ docker container logs CONTAINER_NAME
Create and run a docker container.
Below example of Starting a nginx Web Server
# Run a container in foreground
$ docker container run -it -p 80:80 nginx
# Run a container in background
$ docker container run -d -p 80:80 nginx
# Run a container specifying the name
$ docker container run -d -p 80:80 --name nginx-server nginx
# View details info of a container
$ docker container inspect CONTAINER_NAME
$ docker container inspect nginx-server
Go to container bash prompt to edit config files , debug etc
$ docker container exec -it nginx-server bash
Get docker container performance stats (cpu, mem, network, disk, etc)
$ docker container stats CONTAINER_NAME
# Get nginx container (created in above example) performance stats
$ docker container stats nginx-server
Docker Image Commands
List docker images that we have pulled
$ docker image ls
Pull the docker images
# Pull nginx docker image
$ docker pull nginx
# Pull nginx image with version 1.11.9
$ docker pull nginx:1.11.9
# Pull nginx image with version 1.11.9 with alpine tag
$ docker pull nginx:1.11.9-alpine
Remove an image
$ docker image rm IMAGE_NAME
Retag existing image
$ docker image tag nginx rkshpanigrahi/nginx
Upload to docker hub
$ docker image push rkshpanigrahi/nginx
Add tag to newly created image
$ docker image tag rkshpanigrahi/nginx rkshpanigrahi/nginx:test