Saturday, January 9, 2016

Docker – Commands and Usage

In the previous article we have seen how we can create our first basic Docker container. In this article we will see some of the command available that we can use while working with the Docker containers.

Docker – Version: The most basic command that we use is to find the version of the Docker that we are using. This can be done by using the “docker version” command as,

[root@vx111a work]# docker version
Version: 1.8.2-el7.centos
API version: 1.20
Package Version: docker-1.8.2-10.el7.centos.x86_64
Go version: go1.4.2
Git commit: a01dc02/1.8.2
OS/Arch: linux/amd64
Version: 1.8.2-el7.centos
API version: 1.20
Package Version:
Go version: go1.4.2
Git commit: a01dc02/1.8.2
OS/Arch: linux/amd64

Docker – pull: Docker Hub is a registry where most of the docker images are created. This is something like a hub for docker images. The “docker pull” command pulls the image that we specified from the docker hub and saves that on our local machine so that we can use that.

[root@vx111a work]# docker pull busybox
Using default tag: latest
Trying to pull repository ... latest: Pulling from library/busybox

5c5fb281b01e: Pull complete
fc0db02f3072: Pull complete
Digest: sha256:e4f93f6ed15a0cdd342f5aae387886fba0ab98af0a102da6276eaf24d6e6ade0
Status: Downloaded newer image for

In the above command we are trying to pull a busybox docker image from the docker hub.

We can also say to pull all tags available for a image by using the below command as,
$ docker pull --all-tags centos
Docker – images: Once we download the images ,we can then check what images are available at this moment by using the “docker images” command as,

[root@vx111a work]# docker images
REPOSITORY          TAG                 IMAGE ID          CREATED            VIRTUAL SIZE   latest              fc0db02f3072      4 weeks ago       1.113 MB

Docker –run: Once we downloaded the docker images, we can then use them to start the images using the “docker run” command as,

[root@vx111a work]# docker run -it --rm fc0db02f3072
/ # uname -a
Linux 96f7152ba503 3.10.0-327.3.1.el7.x86_64 #1 SMP Wed Dec 9 14:09:15 UTC 2015 x86_64 GNU/Linux
/ #

Docker –ps: Once we have started a docker image, we can use the “docker ps” command to see the status of the running Containers as,

[root@vx111a work]# docker ps
96f7152ba503    fc0db02f3072  "sh"          a minute ago  Up                      mahavira

Docker –diff: This command allows finding the recent changes that are done. In other words the differences,

[root@vx111a work]# docker diff 0774b6bee081
A /hai
C /root
A /root/.ash_history
A /run
A /run/secrets

0774b6bee081 is the Container ID that is started. This can obtained using the “docker ps” command.

Docker – CP: this command allows to copy files from the container to host machines. This can be used as,

[root@vx111a work]# docker cp 0774b6bee081:/hai /tmp

The file hai located in the / location in the container ID 0774b6bee081 will be copied to the /tmp of the host machine.

Docker – attach: Docker attach command allows you to attach to a running container. The attaching is done to execute a command or to work on it interactively

[root@vx111a work]# docker run -d /bin/top -b

We have run a “top” command on the busybox container. We received a hash code by which we attached that to see the top command output as below,

[root@vx111a work]# docker attach cd7ea07b4fc51a3c8e981102a4ed15ab1760513b7970355011f702fe3614a75c
Mem: 5081612K used, 2835492K free, 258156K shrd, 2384K buff, 1418080K cached
CPU:  0.3% usr  0.1% sys  0.0% nic 99.1% idle  0.2% io  0.0% irq  0.0% sirq
Load average: 0.67 0.25 0.15 1/881 4
    1     0 root     R     1196  0.0   4  0.0 /bin/top -b

Docker- Create:  This allows us to create a new Container. this can be used as

[root@vx111a work]# docker create -t -i sh

[root@vx111a work]# docker start -a -i 5b18e5616a02c84aed5dfe83ea473e9df5e8c575dd3da2e0187a8cae04bca870
/ # 

Docker –events: This command allows us to view any events happened on a Containers. For this to test ,we need to start the command “docker events” and start a container using multiple terminals.

Terminal -1
[root@vx111a work]# docker events

Terminal -2
[root@vx111a work]# docker run -d /bin/top -b

Now we see the output some thing like below in the first terminal under the events commands

2016-01-07T17:32:09.000000000+05:30 709b4e45ee8c3e4160b98d71827d467442689ffffb8b39971d8c3f56eeb72169: (from create

Docker –exec: This allows us to run a command on a running container. This can be used as,

[root@vx111a work]# docker run -t -i /bin/sh

[root@vx111a work]# docker ps
922b33487eeb  busybox   "/bin/sh"   11 seconds Up                      adoring_pare

[root@vx111a work]# docker exec 922b33487eeb ls

We can use the exec command an Container ID to pass a command to execute. We have passed the “ls” command above.

Docker-History: This can be used to check the history of the container. This is used as
 [root@vx111a work]# docker images
REPOSITORY          TAG                 IMAGE ID          CREATED           VIRTUAL SIZE   latest              fc0db02f3072     4 weeks ago      1.113 MB

[root@vx111a work]# docker history
IMAGE             CREATED       CREATED                               BY SIZE                 fc0db02f3072   4 weeks ago     /bin/sh -c #(nop) CMD ["sh"]  0 B               

Docker-info: Displays System Wide information. This can be used as

[root@vx111a work]# docker -D info
Containers: 21
Images: 2
Storage Driver: devicemapper
 Pool Name: docker-8:1-154653898-pool
 Pool Blocksize: 65.54 kB

Docker - Export & Import: These export and import command can be used to export a container and import a container.
Export - docker export 709b4e45ee8c > latest.tar
Import - docker import < latest.tar

Docker –Puase & Unpause: These commands are used to pause a Container and Unpause a Container. These can be used as,

[root@vx111a work]# docker ps
709b4e45ee8c      busybox  "/bin/top -b"   12 minutes   Up           mad_shockley

[root@vx111a work]# docker pause 709b4e45ee8c

[root@vx111a work]# docker ps
709b4e45ee8c      busybox  "/bin/top -b"   12 minutes   Up(Paused)   mad_shockley

Once we pause the container we can see a Pause in the Status Field.

Docker-Stop: Stops the Container. This can be used as “docker stop <Contianer ID>”
[root@vx111a work]# docker stop 412ae578fabd

Docker –start: starts the Container. This can be used as “docker start <Container ID>”
Docker –kill : This is called when we need to kill a running container using a SIGKILL or other signal

Docker-rename: Renames a Docker Container
Docker-restart: Restart a Docker Container

Docker-rm/rmi: Remove one of more containers. This can be used as “docker rm container”

[root@vx111a work]# docker rm 412ae578fabd

[root@vx111a work]#docker rm $(docker ps -a -q) #removes all containers
[root@vx111a work]# docker rmi $(docker images -q) #remove all Images

Docker-inspect: This command when ran will return the low level information on the container like Ports, IP address etc. an Example will be 

[root@vx111a work]# docker inspect --format '{{ .NetworkSettings.IPAddress }}' be99f6d00d66

These are some of the most used commands with Docker. Hope this helps. More to come, Happy learning J

No comments :

Post a Comment