# Docker  commands examples

1. ***docker ps*** -&gt; lists all the running containers
    
    The screenshot below shows that no containers are running
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679413166422/dbde9460-043b-486d-923e-0eff8f19a915.png align="center")
    
2. ***docker ps -a*** -&gt; list all the containers irrespective of if they are running or not
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679413199085/cb40c492-da3d-49ec-9b32-fe164302ce94.png align="center")
    

3.***docker run*** -&gt; runs the container specified after the run if it exists on the docker host and if not downloads the container from docker hub and runs the container

4\*\*\*.docker stop container\_id\*\*\* -&gt; stops a container if it is running

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679414238312/4d9518dc-a775-4a00-8d76-13d093df7409.png align="center")

Run docker ps first to get container info such as container\_id (d577cd8deddd) and then issue docker stop using the container\_id as shown above. The output after stopping the container is the container id and that indicates the container has been stopped

***5.docker rm container\_id*** -&gt; removes a stopped container from the local host

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679414562511/c5beafc5-9ded-449a-9f0e-892b01687769.png align="center")

Run docker ps first to get container info such as container\_id (d577cd8deddd) and then issue docker rm using the container\_id as shown above. The output after removing the container is the container id and that indicates the container has been stopped

***6.docker images*** -&gt; lists all images available on the host

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679415092365/bcd13926-4ad2-4e49-bd92-cf29beb24270.png align="center")

***7.docker rmi image-id*** -&gt; Removes the image-id specified from the host.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679441739571/2d392239-45e5-486a-aca4-53d05ffe5753.png align="center")

***Note*** : All the containers using this image have to be stopped and deleted before the corresponding image can be removed

***8.docker pull*** -&gt; This command just pulls the container image from the docker hub if not present in the docker host but does not run the container

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679441778414/29ab2815-5a91-4118-bd84-b894c7dfd8b1.png align="center")

***9.docker exec container-id*** ***command*** -&gt; exec command allows us to execute a command inside a container.

In the screenshot below we are viewing the contents of /etc/hosts inside the running container specified via the container id using the cat command

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679415867271/bb91f133-7c09-4db4-b569-4a1022a9459f.png align="center")

***10.docker run -d container-image*** -&gt; The -d option with the run command runs the container for the image specified in the background

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679421469734/0da75dbb-a8f0-4164-8d57-bf5483f47711.png align="center")

Author : Chandrasekar(Chan) Rajaram

[https://www.linkedin.com/in/chan-rajaram-software-engineer/](https://www.linkedin.com/in/chan-rajaram-software-engineer/)

#devops
