Docker Basic Commands

The following are the most common commands that are needed to work with Docker. Open the terminal and run the commands.

Note

If you have installed docker on local computer then make sure docker service is running before executing any command.

Docker Version

To find the version of installed docker client and docker server:

docker version

Download image

To download/pull an image from the default registry (docherhub).

docker pull <image name>
# e.g.
docker pull hello-world
# or
docker pull busybox

Runing Container

The run command is the combination of two commands run and pull. If the image is available on the local computer then it will run that image. If the image is not available locally then the command will search the docker hub for the image and if it is found it will be pulled/downloaded and run as container.

docker run <image name>
# e.g.
docker run -d hello-world
# or
docker run -d busybox echo "Hello World"

-d option is used to run the container in the background and free terminal to run more commands.

List Images

To list all the images either downloaded from the internet or created locally:

docker images

List Running Containers

To list all the containers currently under execution

docker ps

List all Containers

To list all the containers that are under execution and those which finished execution.

docker ps -a

Stop Container

To stop a running container

# First step is to run the container which you will stop below.
# The following command will keep busybox container running for 10 seconds
docker run -d busybox sleep 10
# Second step is to find the ID of the container to stop
docker ps
# To stop the container
docker stop <container_ID>

Start Container

To start a stopped container

# To find the ID of the container to start
docker ps -a
# To start the container
docker start <container_ID>

Get Container Details

To get the container details like IP address, image, creation time and much more

# To find the ID of the container to stop
docker ps -a
# To get the details of a container
docker inspect <container_ID>

Get all information

To get the complete details about running containers, stopped containers, images, server, network, storage etc.

docker info

Delete Container

To remove/delete a container

# To find the ID of the container to delete
docker ps -a
# To delete the container
docker rm -f <container_ID>

Delete all Containers

To delete all containers (running and stopped both)

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

Delete Image

To delete an image

# To find the ID of the image to delete
docker images
# To delete the image
docker rmi -f <image_ID or image_name>
# e.g.
docker rmi -f hello-world

Delete all Images

To remove/delete all images

docker rmi -f $(docker images -a -q)