Thursday, May 3, 2018

Download, Install and Delete Docker Container


Previous post Beginners Guide To Docker has covered the beginners guide to dockers. In this post, I will be covering more hands on experience. Before we start, I want you to check the previous post and make sure that the docker is up and running. Open a new terminal and type the following command

        

PS C:\windows\system32> docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE


Download and Installing Docker Image

We can see that there are no images installed locally on the laptop. Let's download a image from the docker public registry which is nothing but docker hub. We need to pull the image first from the docker hub and it will get stored on the local pc.
        
PS C:\windows\system32> docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
9bb5a5d4561a: Pull complete
Digest: sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77
Status: Downloaded newer image for hello-world:latest
PS C:\windows\system32>


Once the image is downloaded, we can check the image is locally downloaded by running below mentioned command.
        

PS C:\windows\system32> docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
hello-world                latest              e38bc07ac18e        3 weeks ago         1.85kB
PS C:\windows\system32>

Once the image is downloaded, we can run the image locally and test it with below mentioned command.
        
PS C:\windows\system32> docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/


Let's try to run another container "centos" with a different process running inside it. Type the following command into your Terminal. The container image we're using is centos and the process we're executing inside the centos container is ping -c 5 127.0.0.1, which pings the loopback address for five times until it stops.
        
PS C:\windows\system32> docker run centos ping -c 5 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.028 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.029 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.028 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.042 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.026 ms

--- 127.0.0.1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4198ms
rtt min/avg/max/mdev = 0.026/0.030/0.042/0.008 ms

Check The List Of Containers

Let's try to run another command to see the number of containers running on docker host.
        
PS C:\windows\system32> docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS         NAMES

By default docker gives 7 outputs once we run the container list command as below mentioned:
Container ID: Unique container ID with SHA-256
Image: Name of container image
Command: The command that is used to run the main process within container
Created: The date and time when the container was created
Status: running, removing, paused, exited
Ports: List of container ports that are mapped to docker host ports
Names: The random or manual name assigned to container

Run the below command to check the total number of containers defined in the system. The state of the container could be running, paused or exited
        
PS C:\windows\system32> docker ps -a
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS                         PORTS               NAMES
f4912175410c        centos              "ping -c 5 127.0.0.1"   17 minutes ago      Exited (0) 17 minutes ago                          elastic_carson
b5f21082398a        centos              "ping -c 127.0.0.1"     17 minutes ago      Exited (2) 17 minutes ago                          confident_goldstine
dfebc236e05f        centos              "/etc/*release*"        18 minutes ago      Created                                            priceless_allen
9efc9e9ef4ae        centos              "ifconfig -a"           18 minutes ago      Created                                            hungry_hugle
e1f841554ff7        centos              "ping -c 2 4.2.2.2"     19 minutes ago      Exited (1) 19 minutes ago                          compassionate_payne
545dd4830c2b        hello-world         "/hello"                About an hour ago   Exited (0) About an hour ago                       wizardly_feynman

Deleting Containers

With the above command we know the container id, now let's run the below command in terminal and remove the container f4912175410c. Removing container doesn't mean we are removing the image, it's only meaning that we are removing it from the volume.
        
PS C:\windows\system32> docker rm f4912175410c
f4912175410c
PS C:\windows\system32> docker ps -a
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS                         PORTS               NAMES
b5f21082398a        centos              "ping -c 127.0.0.1"   22 minutes ago      Exited (2) 22 minutes ago                          confident_goldstine
dfebc236e05f        centos              "/etc/*release*"      22 minutes ago      Created                                            priceless_allen
9efc9e9ef4ae        centos              "ifconfig -a"         23 minutes ago      Created                                            hungry_hugle
e1f841554ff7        centos              "ping -c 2 4.2.2.2"   23 minutes ago      Exited (1) 23 minutes ago                          compassionate_payne
545dd4830c2b        hello-world         "/hello"              About an hour ago   Exited (0) About an hour ago                       wizardly_feynman

We can also remove multiple container in single command as per their status listed the above output. Let's try to run the below mentioned command in terminal and delete the containers whose status is showing "exited" as per the above output:
        
PS C:\windows\system32> docker ps -a
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS                         PORTS               NAMES
b5f21082398a        centos              "ping -c 127.0.0.1"   26 minutes ago      Exited (2) 26 minutes ago                          confident_goldstine
dfebc236e05f        centos              "/etc/*release*"      26 minutes ago      Created                                            priceless_allen
9efc9e9ef4ae        centos              "ifconfig -a"         27 minutes ago      Created                                            hungry_hugle
e1f841554ff7        centos              "ping -c 2 4.2.2.2"   27 minutes ago      Exited (1) 27 minutes ago                          compassionate_payne
545dd4830c2b        hello-world         "/hello"              About an hour ago   Exited (0) About an hour ago                       wizardly_feynman
PS C:\windows\system32>
PS C:\windows\system32> docker rm $(docker ps -q -f status=exited)
b5f21082398a
e1f841554ff7
545dd4830c2b
PS C:\windows\system32>
PS C:\windows\system32> docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
dfebc236e05f        centos              "/etc/*release*"    26 minutes ago      Created                                 priceless_allen
9efc9e9ef4ae        centos              "ifconfig -a"       27 minutes ago      Created                                 hungry_hugle
PS C:\windows\system32>

People who read this post also read :



No comments: