Ashish Singh Baghel's Blog

Basic Docker Commands | Images | Containers

December 27, 2020

Docker Image

Docker Commands

Demo for these commands is available in the following YouTube video

  1. Check Docker Version
docker version
  1. Check the detailed information of Docker installed
docker info

Docker commands - Images

  1. Search an image in Docker hub
docker search ubuntu
docker search <<imagename>> | head -20
docker search ubuntu | head -20
#  will search ubuntu images in the dockerhub and limit the search result to only 20

Note: head command works on bash terminal, for PowerShell terminal in windows use select -first 10

docker search ubuntu | select -first 10
  1. Pull and Image from Docker hub
docker pull <<imagename>>
docker pull nginx
  1. Check image history (layers in an image)
docker history <<imagename>>
docker history nginx
  1. Pull a specific version of an image
docker pull <<imagename>>:<<versiontag>>
docker pull busybox:1.24
  1. Verify Downloaded docker images
docker images
  1. Remove Docker images
docker rmi <<imagename>>
docker rmi nginx
  1. Download and run an image in docker container using run command
docker run <<imagename>>
docker run --name cntnginx -d nginx
# --name to specify a name for the running container. In this example, it is cntnginx
# -d to run the container in background (detached mode)

Docker Commands - Containers

  1. List running containers
docker ps
  1. List all containers (This includes containers in all state)
docker ps -a
  1. Inspect container object
docker inspect <<containername/containerid>>
docker inspect cntnginx
  1. Print the stats for a running container
docker stats <<containername/containerid>>
docker stats cntnginx
  1. Pause a running container
docker pause <<containername/containerid>>
docker pause cntnginx
  1. Unpause a paused container
docker unpause <<containername/containerid>>
docker unpause cntnginx
  1. Kill a running container
docker kill <<containername/containerid>>
docker kill cntnginx
  1. Start a killed container
docker start <<containername/containerid>>
docker start cntnginx
  1. Stop a running container
docker stop <<containername/containerid>>
docker stop cntnginx
  1. Delete a container
docker rm <<containername/containerid>>
docker rm cntnginx
  1. To remove all stopped containers
docker container prune
  1. Export a container as an image (.tar file)
docker export <<containername/contianerid>> -o <<filename>>.tar
docker run --name newnginxcontainer -d nginx
docker export newnginxcontainer -o test.tar

# Note: You must have permissions on the folder where you are saving the test.tar
# use `chmod 777 ./` for permissions in case if you are facing an permission denied error.
  1. Import an exported container image (.tar file)
docker import <<remoteurl/imagename.tar>>
docker import test.tar

© 2021 Ashish Singh Baghel, Built with Gatsby