Introduction
Docker is playing very critical role as both a packaging format for the applications and a unifying interface and methodology that enables the application team/s to own the Docker-formatted container images (Note: It is assumed that including all dependencies). This has made Docker as one of the leading front runners in microservices and microservices based adaption and usage.
Many of the Docker and Docker based utilities helps us to bring greater efficiencies of scalability and performance by shrinking application footprints through Dockerized containers. In many cases system-level dependencies are reduced to bare minimum, which helps in bringing down memory usage in terms of MB’s (Mega bytes).
All these aspects helped in making Docker as one of the leading container based utility. Docker has many command-line utilities and capabilities. Docker Daemon can provide many of these additional facilities and which makes Docker configuration easier.
Read Also :How to Install Docker (Community Edition) on Ubuntu 16.04 / 18.04 LTS
Hope the reader/user of this article knows how to spin-off docker-container on any given Linux environment. Assuming this, some of the following facilities are described,
Some of these facilities can be used for the following functions,
- Check Docker server information
- Download docker images and updates
- Inspect containers
- View and monitor logs
- Monitor various statistics and details
1) How to check present docker version
Knowing the docker version is one of the very important aspects as depending on this, many of the version based decisions are taken.
shashi@linuxtechi:~$ sudo docker version Client: Version: 18.09.5 API version: 1.39 Go version: go1.10.8 Git commit: e8ff056 Built: Thu Apr 11 04:43:57 2019 OS/Arch: linux/amd64 Experimental: false Server: Docker Engine - Community Engine: Version: 18.09.5 API version: 1.39 (minimum version 1.12) Go version: go1.10.8 Git commit: e8ff056 Built: Thu Apr 11 04:10:53 2019 OS/Arch: linux/amd64 Experimental: false shashi@linuxtechi:~$
This above output provides both server and client API versions as same and OS and Architecture version also should be same. If there is any mismatch in client and server based versions, client-server communication will fail. One has to make sure what the supporting versions are and take decisions accordingly.
2) Capturing and analyzing server information
Using docker info we can also find the following information. Some of the useful information we can gather are something like, which server is running as backend, which kernel version it is, which OS and Docker root directory etc
shashi@linuxtechi:~$ sudo docker info Containers: 2 Running: 1 Paused: 0 Stopped: 1 Images: 4 Server Version: 18.09.5 Storage Driver: overlay2 Backing Filesystem: extfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: bb71b10fd8f58240ca47fbb579b9d1028eea7c84 runc version: 2b18fe1d885ee5083ef9f0838fee39b62d653e30 init version: fec3683 Security Options: apparmor seccomp Profile: default Kernel Version: 4.15.0-20-generic Operating System: Ubuntu 18.04 LTS OSType: linux Architecture: x86_64 CPUs: 1 Total Memory: 1.947GiB Name: linuxtechi ID: VRUY:AWXX:7JWE:YWU7:X4QW:TNKE:6H26:PNRR:QFGI:XYRQ:QUXF:MTXC Docker Root Dir: /var/lib/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false Product License: Community Engine WARNING: No swap limit support shashi@linuxtechi:~$
All the above information is based on how the Docker Daemon is set-up, underlying OS version and file system type. All these can be captured using the following set-of the commands,
Execute the below command to get OS Name, its version and Code name
shashi@linuxtechi:~$ cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=18.04 DISTRIB_CODENAME=bionic DISTRIB_DESCRIPTION="Ubuntu 18.04 LTS" shashi@linuxtechi:~$
or
shashi@linuxtechi:~$ cat /etc/*-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=18.04 DISTRIB_CODENAME=bionic DISTRIB_DESCRIPTION="Ubuntu 18.04 LTS" NAME="Ubuntu" VERSION="18.04 LTS (Bionic Beaver)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 18.04 LTS" VERSION_ID="18.04" HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" VERSION_CODENAME=bionic UBUNTU_CODENAME=bionic shashi@linuxtechi:~$
Execute the beneath command to get the file system details :
shashi@linuxtechi:~$ mount | grep "^/dev" /dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro,data=ordered) shashi@linuxtechi:~$
3) Docker daemon
The docker daemon plays a very crucial role in docker environment as whole.
Without a proper daemon, the complete docker system will be useless. One can verify the daemon status using the following command,
Note:- Assuming proper docker installation is done
shashi@linuxtechi:~$ sudo service docker status
If the docker service is running, then output of above command should be something like below:
In case docker service is not running then use below command to start it
shashi@linuxtechi:~$ sudo systemctl start docker or shashi@linuxtechi:~$ sudo service docker start shashi@linuxtechi:~$
Use below “docker ps” command to list the running containers
shashi@linuxtechi:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES shashi@linuxtechi:~$
To list all running and stopped containers use “docker ps -a”
shashi@linuxtechi:~$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 497e6733d760 ubuntu "bash" 19 minutes ago Exited (0) 2 minutes ago goofy_morse 0862fe109f96 hello-world "/hello" 19 minutes ago Exited (0) 19 minutes ago vibrant_shannon shashi@linuxtechi:~$
Docker default root directory is “/var/lib/docker”
shashi@linuxtechi:~$ sudo ls -l /var/lib/docker total 48 drwx------ 2 root root 4096 Apr 14 07:00 builder drwx------ 4 root root 4096 Apr 14 07:00 buildkit drwx------ 4 root root 4096 Apr 14 07:09 containers drwx------ 3 root root 4096 Apr 14 07:00 image drwxr-x--- 3 root root 4096 Apr 14 07:00 network drwx------ 16 root root 4096 Apr 14 07:27 overlay2 drwx------ 4 root root 4096 Apr 14 07:00 plugins drwx------ 2 root root 4096 Apr 14 07:27 runtimes drwx------ 2 root root 4096 Apr 14 07:00 swarm drwx------ 2 root root 4096 Apr 14 07:27 tmp drwx------ 2 root root 4096 Apr 14 07:00 trust drwx------ 2 root root 4096 Apr 14 07:00 volumes shashi@linuxtechi:~$
The docker daemon if not started can be invoked using the following command,
shashi@linuxtechi:~$ sudo dockerd
Output of above dockerd command will be something like below:
Read Also : How to install Docker on CentOS 7
4) Downloading docker container image and inspecting the container
shashi@linuxtechi:~$ sudo docker pull ubuntu:latest Using default tag: latest latest: Pulling from library/ubuntu 898c46f3b1a1: Pull complete 63366dfa0a50: Pull complete 041d4cd74a92: Pull complete 6e1bee0f8701: Pull complete Digest: sha256:017eef0b616011647b269b5c65826e2e2ebddbe5d1f8c1e56b3599fb14fabec8 Status: Downloaded newer image for ubuntu:latest shashi@linuxtechi:~$ latest: Pulling from library/ubuntu898c46f3b1a1: Pull complete 63366dfa0a50: Pull complete 041d4cd74a92: Pull complete6e1bee0f8701: Pull complete Digest: sha256:017eef0b616011647b269b5c65826e2e2ebddbe5d1f8c1e56b3599fb14fabec8Status: Downloaded newer image for ubuntu:latest
Launching a container, example is shown below
shashi@linuxtechi:~$ sudo docker run -d -t ubuntu /bin/bash 58c023f0f5689ff08b858221ca10c985936a8c9dd91d08e84213009facb64724 shashi@linuxtechi:~$ shashi@linuxtechi:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 58c023f0f568 ubuntu "/bin/bash" 27 seconds ago Up 26 seconds boring_dijkstra shashi@linuxtechi:~$
Let’s inspect this container using the following command,
shashi@linuxtechi:~$ sudo docker inspect 58c023f0f568 [ { "Id": "58c023f0f5689ff08b858221ca10c985936a8c9dd91d08e84213009facb64724", "Created": "2019-04-14T06:55:26.289022884Z", "Path": "/bin/bash", "Args": [], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 15538, "ExitCode": 0, "Error": "", "StartedAt": "2019-04-14T06:55:27.142274111Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:94e814e2efa8845d95b2112d54497fbad173e45121ce9255b93401392f538499", "ResolvConfPath": "/var/lib/docker/containers/58c023f0f5689ff08b858221ca10c985936a8c9dd91d08e84213009facb64724/resolv.conf", ………………………………………………
Note:- The complete output of this command is not shown here, because it’s output is too large
5) Going inside the running container image
As one is aware of Docker used originally everything based on LXC backend, lxc-attach command of linux was used for quite some time. But once docker was built as a standalone package and started using “libcontainer” as the default backend, the “docker exec” command usage became popular
Below set of commands explain on how to login into container using command line,
:~$ sudo docker exec -t <Container_id> <Commands>
shashi@linuxtechi:~$ sudo docker exec -t 58c023f0f568 ls -a . .dockerenv boot etc lib media opt root sbin sys usr .. bin dev home lib64 mnt proc run srv tmp var shashi@linuxtechi:~$ sudo docker exec -t 58c023f0f568 ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 06:55 pts/0 00:00:00 /bin/bash root 20 0 0 07:17 pts/1 00:00:00 ps -ef shashi@linuxtechi:~$
The alternate way to run commands inside the container is, first login to container using “docker attach” and then execute commands
:~$ sudo docker attach <Container_id>
shashi@linuxtechi:~$ sudo docker attach 58c023f0f568 root@58c023f0f568:/#
There are some situations where you want to launch a container, attaching a volume to it and when we exit from container it should deleted automatically, example is shown below
shashi@linuxtechi:~$ sudo docker run -it --rm -v /usr/local/bin:/target jpetazzo/nsenter bash root@73c72922f87e:/src# df -h /target/ Filesystem Size Used Avail Use% Mounted on /dev/sda1 18G 5.6G 12G 34% /target root@73c72922f87e:/src# exit exit shashi@linuxtechi:~$
Now verify whether docker is removed or not automatically when we exit from the container, run “docker ps”
shashi@linuxtechi:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 58c023f0f568 ubuntu "/bin/bash" About an hour ago Up About an hour boring_dijkstra shashi@linuxtechi:~$
6) Monitoring Docker
Use “docker stats” command to display resource utilization of all the containers,
shashi@linuxtechi:~$ sudo docker stats CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 58c023f0f568 boring_dijkstra 0.00% 1.059MiB / 1.947GiB 0.05% 4.75kB / 0B 4.74MB / 0B 1 CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 58c023f0f568 boring_dijkstra 0.00% 1.059MiB / 1.947GiB 0.05% 4.75kB / 0B 4.74MB / 0B 1 shashi@linuxtechi:~$
7) Docker Events
Many a times capturing docker events will be crucial as this will provide information on various docker operations and events taking place. Below is the snapshot of the same.
shashi@linuxtechi:~$ sudo docker events 2019-04-14T09:29:07.636990738+01:00 image pull wordpress:latest (name=wordpress) 2019-04-14T09:29:46.936676431+01:00 volume create 36187e0a44d277439fea0df2446fc44987fa814c52744091929e5c81bd8134e5 (driver=local) 2019-04-14T09:29:46.998798935+01:00 container create b97f6a565c518eb1464cf81e6e09db1acfd84a0fdcbaea94255f1f182d79c058 (image=wordpress, name=friendly_heisenberg) 2019-04-14T09:29:47.000202026+01:00 container attach b97f6a565c518eb1464cf81e6e09db1acfd84a0fdcbaea94255f1f182d79c058 (image=wordpress, name=friendly_heisenberg) 2019-04-14T09:29:47.209257002+01:00 network connect 18dd93c3c6fc9ce51a98f7d2359b319db251efcae6b991157965ef727a580702 (container=b97f6a565c518eb1464cf81e6e09db1acfd84a0fdcbaea94255f1f182d79c058, name=bridge, type=bridge) 2019-04-14T09:29:47.239846902+01:00 volume mount 36187e0a44d277439fea0df2446fc44987fa814c52744091929e5c81bd8134e5 (container=b97f6a565c518eb1464cf81e6e09db1acfd84a0fdcbaea94255f1f182d79c058, destination=/var/www/html, driver=local, propagation=, read/write=true) 2019-04-14T09:29:47.942997316+01:00 container start b97f6a565c518eb1464cf81e6e09db1acfd84a0fdcbaea94255f1f182d79c058 (image=wordpress, name=friendly_heisenberg) 2019-04-14T09:29:47.944521098+01:00 container resize b97f6a565c518eb1464cf81e6e09db1acfd84a0fdcbaea94255f1f182d79c058 (height=39, image=wordpress, name=friendly_heisenberg, width=130) 2019-04-14T09:29:59.829378089+01:00 container die b97f6a565c518eb1464cf81e6e09db1acfd84a0fdcbaea94255f1f182d79c058 (exitCode=0, image=wordpress, name=friendly_heisenberg) 2019-04-14T09:30:00.147435896+01:00 network disconnect 18dd93c3c6fc9ce51a98f7d2359b319db251efcae6b991157965ef727a580702 (container=b97f6a565c518eb1464cf81e6e09db1acfd84a0fdcbaea94255f1f182d79c058, name=bridge, type=bridge) 2019-04-14T09:30:00.845336887+01:00 volume unmount 36187e0a44d277439fea0df2446fc44987fa814c52744091929e5c81bd8134e5 (container=b97f6a565c518eb1464cf81e6e09db1acfd84a0fdcbaea94255f1f182d79c058, driver=local) ………………
Conclusion
The combination of all these commands and utilities are very important in making docker and container a successful microservices based environment. As many of the microservices architectures need these kinds of utilities to debug, understand and learn more about them for every day usage, I hope this article will play role in helping such cases.
Read Also : How to install and use docker-compose to deploy containers in CentOS 7