Aim

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

Prerequisite

  • Docker must be installed on your machine.

1. List All Running Containers

docker ps

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

  • CONTAINER ID : This is the unique id given to any container
  • IMAGE : Image name from which the container has been created
  • COMMAND : This list down the last command that was run while creating the container
  • CREATED : Denotes when the container was created from current time
  • STATUS : Uptime of the container
  • PORTS : Which ports of container are mapped to the host machine port
  • NAME : Then name given to the container
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

2. List all Running and Stopped Container

docker ps -a

This above command list down all the running and stopped container.

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
  • In the output you can see the container with name mysql-5.7 has a status of Exited (0) 7 seconds ago.
  • As the status shows the exited container, so we can say that the above command shows both running and stopped container.

3. Stopping a Single Container

  • First you need to list down all the running container using 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
  • Copy the CONTAINER ID value of the container which you are going to stop
  • we are going to stop container with id : 9fa39cabf464
  • Finally we will use the below command to stop the container
docker stop 9fa39cabf464
  • You can again check the list of running container, and you will find that the container with id 9fa39cabf464 will be removed from the list.
  • Note : Stopping a container doesn't remove the container fully

4. Staring a stopped container

  • Once a container has been stopped then you can again start the container using 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 container and find that the container with id 9fa39cabf464 is up and running.

5. 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.

6. Stopping all the running docker container

  • Sometime you will want to stop all the running container at once. For that you can use the below command.
docker stop $(docker ps -aq)

7. Removing all the container at once

  • If you want to remove all the container at once then you can use the following commands
docker rm $(docker ps -aq)

8. List all the docker images present in you 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 following information about the images

  • REPOSITORY : The repository name of docker image in 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.

9. Downloading a docker image

  • Docker images are downloaded from remote registry. The global public docker repository is called as docker hub.
  • You can download docker images from docker hub using
docker pull <image-name>
  • 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
  • This will give the following output
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
  • Note : By default the image with the latest tag is downloaded.

10. Deleting a docker images

  • sometimes you will want to delete a downloaded docker images. Then you can use the below command
docker image rm <image-id>
  • You need to replace the 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

Happy Coding

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x