important docker commnds

In this post, we will learn about the ten most important commands used in the docker world. we will look into some most frequently used commands.

Prerequisite

  • You should install Docker on your machine

List All Running Containers

docker ps
selftuts@master:~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
11c861c90887 registry:2 "/entrypoint.sh /etc…" 2 hours ago Up 2 hours 0.0.0.0:5000->5000/tcp docker-registry
9fa39cabf464 mysql:5.7 "docker-entrypoint.s…" 2 hours ago Up 2 hours 33060/tcp, 0.0.0.0:3307->3306/tcp mysql-5.7

This command lists down important information about all the running docker containers

  • CONTAINER ID: Unique ID of Container.
  • IMAGE: Image Name of the Container
  • COMMAND: Command used for creating the container
  • CREATED: Creation Time of the Container
  • STATUS: Uptime of the container
  • PORTS: Container Ports Mapped to host Machine Port
  • NAME: Name given to Container

List all Running and Stopped Container

docker ps -a

This above command lists down all the running and stopped containers.

selftuts@master:~ $ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
11c861c90887 registry:2 "/entrypoint.sh /etc…" 2 hours ago Up 2 hours 0.0.0.0:5000->5000/tcp docker-registry
9fa39cabf464 mysql:5.7 "docker-entrypoint.s…" 2 hours ago Exited (0) 7 seconds ago mysql-5.7
  • You can see the container with the name mysql-5.7
    • The status of this container is Exited (0) 7 seconds ago, which means that the command also lists the stopped container
  • As the status shows the exited container, we can say that the above command shows both running and stopped containers.

Stopping a Single Container

First, you need to list down all the running containers using the below command

docker ps
selftuts@master:~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
11c861c90887 registry:2 "/entrypoint.sh /etc…" 2 hours ago Up 2 hours 0.0.0.0:5000->5000/tcp docker-registry
9fa39cabf464 mysql:5.7 "docker-entrypoint.s…" 2 hours ago Up 2 hours 33060/tcp, 0.0.0.0:3307->3306/tcp mysql-5.7
  • You need to copy the CONTAINER ID value of the container which you are going to stop
    • For Example, you want to stop the container with the ID: 9fa39cabf464
  • Finally, we will use the below command to stop the container
docker stop 9fa39cabf464
  • You can again check the lists of running containers, and you will find that the container with the ID 9fa39cabf464 will be removed from the list.
  • Note: Stopping a container doesn’t remove the container fully

Starting a stopped container

  • Once a container has been stopped then you can again start the container using the below command
  • Note: we will be restarting the container which we have stopped previously (9fa39cabf464)
docker start 9fa39cabf464

After applying the above command you can again list all the running containers and find that the container with id 9fa39cabf464 is up and running.

Removing a Container Permanently

If you stop a container then it is not removed permanently. To delete a container permanently you need to use the below command.

docker rm 9fa39cabf464

Note: You can’t remove a running container. First, you have to stop the container then you can remove it.

Stopping all the running docker container

Sometimes you want to stop all the running containers at once. For that, you can use the below command.

docker stop $(docker ps -aq)

Removing all the containers at once

If you want to remove all the containers at once then you can use the following commands

docker rm $(docker ps -aq)

List all the docker images present in your machine

docker images

This will give you output similar to this

selftuts@master:~ $ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 718a6da099d8 6 days ago 448MB
registry 2 2d4f4b5309b1 7 weeks ago 26.2MB
hello-world latest bf756fb1ae65 7 months ago 13.3kB
konradkleine/docker-registry-frontend v2 60d4b91e68fa 2 years ago 266MB

This output will give the following information about the images

  • REPOSITORY: The repository name of the docker image in the docker hub
  • TAG: The tag of the images which is present on your system. An image can have multiple tags.
  • IMAGE ID: The unique image ID of the docker image.
  • CREATED: Denotes when the image was created.
  • SIZE: The size of the image on disk.

Downloading a docker image

  • Docker images are downloaded from the remote registry. The global public docker repository is called docker-hub.
  • You can download docker images from docker-hub using
docker pull

You need to replace the image name with the image that you want to download. Let’s say we want to download the hello-world image.

docker pull hello-world
selftuts@master:~ $ docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:49a1c8800c94df04e9658809b006fd8a686cab8028d33cfba2cc049724254202
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest

This will give the following output

Note: By default the image with the latest tag is downloaded.

Deleting a docker image

Sometimes you want to delete a downloaded docker image. Then you can use the below command

docker image rm <docker-image-id>

You need to replace the <docker-image-id> with the id of the image that you want to delete. First, we will list the images.

selftuts@master:~ $ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 718a6da099d8 6 days ago 448MB
registry 2 2d4f4b5309b1 7 weeks ago 26.2MB
hello-world latest bf756fb1ae65 7 months ago 13.3kB
konradkleine/docker-registry-frontend v2 60d4b91e68fa 2 years ago 266MB

Let’s say we want to delete the hello-world image. As we can see the IMAGE ID is bf756fb1ae65.

so we can use the below command to delete the image

docker image rm bf756fb1ae65

Leave a Reply

Your email address will not be published. Required fields are marked *