Join our Discord Server
Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 570+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 8900+ members and discord server close to 2200+ members. You can follow him on Twitter(@ajeetsraina).

Getting Started with Docker – Part-II

4 min read

In my previous blog post we talked about how to pull images, create containers, start and stop the containers, today we are going to talk more about the containers.

How to restart the container?

[simterm]
[root@localhost ~]# docker restart collabnix
collabnix
[root@localhost ~]#
[/simterm]

How to remove a container?

[simterm]
$ docker rm collabnix
[/simterm]

How to remove all the containers in a single shot?

[simterm]
#docker rm $(docker ps -a -q)
[/simterm]

How to see size of the container?

[simterm]
[root@localhost ~]# docker ps –size
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES SIZE
4e21f056056e ubuntu:12.04 “/bin/bash” About an hour ago Up 3 minutes
collabnix 635 MB
[/simterm]

It shows 635MB for this container

How to see the latest containers?
[simterm]
[root@localhost ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
4e21f056056e ubuntu:12.04 “/bin/bash” About an hour ago Up 4 minutes
collabnix
[/simterm]

I want to see what all containers were created before the specific container?
Say, I created collabnix container and I want to see what all container got created before that name:

[simterm]
[root@localhost ~]# docker ps –before=”collabnix”
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
4ad856667fca blalor/centos:latest “/bin/bash” 6 days ago Exited (0) About an hour ago
gloomy_darwin
1b6b31add360 blalor/centos:latest “/bin/bash” 6 days ago Exited (0) 6 days ago
backstabbing_darwin
bf7535e814e2 blalor/centos:latest “/bin/bash” 7 days ago Exited (0) 7 days ago
elegant_fermat
[/simterm]

If I want to see what containers got created since collabnix:

[simterm]
[root@localhost ~]# docker ps –since=”collabnix”
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
[/simterm]

Is there any way I can see logs of container?

Ofcourse. I would say its more like a recorded activity. Check it out:

[simterm]
[root@localhost ~]# docker logs collabnix
root@4e21f056056e:/# hostname
4e21f056056e
root@4e21f056056e:/# history | grep export
2 history | grep export
root@4e21f056056e:/#
root@4e21f056056e:/#
root@4e21f056056e:/#
root@4e21f056056e:/#
[/simterm]

[simterm]
root@4e21f056056e:/# cat /etc/os-release
NAME=”Ubuntu”
VERSION=”12.04.5 LTS, Precise Pangolin”
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME=”Ubuntu precise (12.04.5 LTS)”
VERSION_ID=”12.04″
root@4e21f056056e:/# clear
root@4e21f056056e:/# df -h
Filesystem Size Used Avail Use%
Mounted on
rootfs 9.8G 168M 9.1G 2%
/
/dev/mapper/docker-253:0-130986-4e21f056056ed168a5518a8db46d99099f3b436d0bc59e9e72529b6bd62109cd 9.8G 168M 9.1G 2%
/
tmpfs 939M 0 939M 0%
/dev
shm 64M 0 64M 0%
/dev/shm
/dev/mapper/VolGroup-lv_root 12G 4.8G 6.0G 45%
/etc/resolv.conf
/dev/mapper/VolGroup-lv_root 12G 4.8G 6.0G 45%
/etc/hostname
/dev/mapper/VolGroup-lv_root 12G 4.8G 6.0G 45%
/etc/hosts
tmpfs 939M 0 939M 0%
/proc/kcore
root@4e21f056056e:/# ^C
root@4e21f056056e:/# free -m
total used free shared buffers cached
Mem: 1876 668 1208 0 14 545
-/+ buffers/cache: 108 1768
Swap: 4031 0 4031
root@4e21f056056e:/# ^C
root@4e21f056056e:/# uname -r
2.6.32-504.16.2.el6.x86_64
root@4e21f056056e:/# uname -a
Linux 4e21f056056e 2.6.32-504.16.2.el6.x86_64 #1 SMP Wed Apr 22 06:48:29 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
root@4e21f056056e:/# uname -ar
Linux 4e21f056056e 2.6.32-504.16.2.el6.x86_64 #1 SMP Wed Apr 22 06:48:29 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
root@4e21f056056e:/# uname -arn
Linux 4e21f056056e 2.6.32-504.16.2.el6.x86_64 #1 SMP Wed Apr 22 06:48:29 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]#

[/simterm]
What does inspect command do?

The inspect command allows you to get the details of a container or an image.
It returns those details as a JSON array:

[simterm]
[root@localhost ~]# docker inspect collabnix
[{
“AppArmorProfile”: “”,
“Args”: [],
“Config”: {
“AttachStderr”: true,
“AttachStdin”: true,
“AttachStdout”: true,
“Cmd”: [
“/bin/bash”
],
“CpuShares”: 0,
“Cpuset”: “”,
“Domainname”: “”,
“Entrypoint”: null,
“Env”: null,
“ExposedPorts”: null,
“Hostname”: “4e21f056056e”,
“Image”: “ac6b0eaa3203”,
“MacAddress”: “”,
“Memory”: 0,
“MemorySwap”: 0,
“NetworkDisabled”: false,
“OnBuild”: null,
“OpenStdin”: true,
“PortSpecs”: null,
“StdinOnce”: true,
“Tty”: true,
“User”: “”,
“Volumes”: null,
“WorkingDir”: “”
},
“Created”: “2015-05-15T07:06:30.137067715Z”,
“Driver”: “devicemapper”,
“ExecDriver”: “native-0.2”,
“ExecIDs”: null,
“HostConfig”: {
“Binds”: null,
“CapAdd”: null,
“CapDrop”: null,
“ContainerIDFile”: “”,
“Devices”: [],
“Dns”: null,
“DnsSearch”: null,
“ExtraHosts”: null,
“IpcMode”: “”,
“Links”: null,
“LxcConf”: [],
“NetworkMode”: “bridge”,
“PidMode”: “”,
“PortBindings”: {},
“Privileged”: false,
“PublishAllPorts”: false,
“ReadonlyRootfs”: false,
“RestartPolicy”: {
MaximumRetryCount”: 0,
“Name”: “”
},
“SecurityOpt”: null,
“VolumesFrom”: null
},
“HostnamePath”:
“/var/lib/docker/containers/4e21f056056ed168a5518a8db46d99099f3b436d0bc59e9e72529b6bd62109cd/hostname”,
“HostsPath”: “/var/lib/docker/containers/4e21f056056ed168a5518a8db46d99099f3b436d0bc59e9e72529b6bd62109cd/hosts”,
“Id”: “4e21f056056ed168a5518a8db46d99099f3b436d0bc59e9e72529b6bd62109cd”,
“Image”: “ac6b0eaa3203c07cb413842c9da6cbbbcf352504c43697ecba449395886e3ae1”,
“MountLabel”: “”,
“Name”: “/collabnix”,
“NetworkSettings”: {
“Bridge”: “docker0”,
“Gateway”: “172.17.42.1”,
“GlobalIPv6Address”: “”,
“GlobalIPv6PrefixLen”: 0,
IPAddress”: “172.17.0.4”,
“IPPrefixLen”: 16,
“IPv6Gateway”: “”,
“LinkLocalIPv6Address”: “fe80::42:acff:fe11:4”,
“LinkLocalIPv6PrefixLen”: 64,
“MacAddress”: “02:42:ac:11:00:04”,
“PortMapping”: null,
“Ports”: {}
},
“Path”: “/bin/bash”,
“ProcessLabel”: “”,
“ResolvConfPath”:
“/var/lib/docker/containers/4e21f056056ed168a5518a8db46d99099f3b436d0bc59e9e72529b6bd62109cd/resolv.conf”,
“RestartCount”: 0,
State”: {
“Error”: “”,
“ExitCode”: 0,
“FinishedAt”: “2015-05-15T08:18:07.984797512Z”,
“OOMKilled”: true,
“Paused”: false,
“Pid”: 2158,
“Restarting”: false,
“Running”: true,
“StartedAt”: “2015-05-15T08:18:08.426280448Z”
},
“Volumes”: {},
“VolumesRW”: {}
}
]
[root@localhost ~]#
[/simterm]

Can i check the running process in a container?

[simterm]
[root@localhost ~]# docker top collabnix
UID PID PPID C STIME TTY
TIME CMD
root 2158 1493 0 13:48 pts/1
00:00:00 /bin/bash
[root@localhost ~]#

[/simterm]

Is it possible to copy a file or folder from a container’s filesystem to the host path?

Yes. Let us try this too:

Create an empty file inside the container as shown:

[simterm]
root@4e21f056056e:/# touch fileinside
root@4e21f056056e:/# pwd
/
root@4e21f056056e:/# ls
? bin dev fileinside lib media opt root sbin srv tmp var
HOPE boot etc
[/simterm]

So I created a file called fileinside.

Now open a new terminal of the host machine and try the below command:

[simterm]
$docker cp collabnix:/fileinside .
[root@localhost ~]# ls
anaconda-ks.cfg hpl iproute-3.8.0-2.ram0.99.x86_64.rpm test.sh
fileinside hpl-2.1.tar.gz log v0.2.14.tar.gz
glibc-2.14.1-14.12.5.x86_64.rpm install.log openmpi-1.4.3.tar.bz2
HOPE install.log.syslog script2run
[/simterm]

What did you see? The file inside the container is easily copied to the host machine.

What does port command do?

The port command looks up the public-facing port that is bound to an exposed
port in the container:
Syntax:

[simterm]
$ docker port CONTAINER PRIVATE_PORT

$ docker port collabnix 2368
4000
[/simterm]

What does diff command do?

The diff command shows the difference between the container and the image it is
based on. In this example, we are running a container with collabnix. In a separate tab, run this command:

[simterm]
$docker diff collabnix
A /
A /HOPE
A /fileinside
How shall I create a new image with filesystem of the container?
[/simterm]

The commit command creates a new image with the filesystem of the container.
Just as with Git’s commit command, you can set a commit message that describes
the image:

Suppose I have run apt-get inside the Ubuntu container. I fear that the size might get filled up. How to track it?

[simterm]
[root@localhost ~]# docker ps -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES SIZE
4e21f056056e ubuntu:12.04 “/bin/bash” About an hour ago Up 43 minutes
collabnix 256 MB
[root@localhost ~]# docker ps -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES SIZE
4e21f056056e ubuntu:12.04 “/bin/bash” About an hour ago Up 43 minutes
collabnix 268.5 MB
[root@localhost ~]#

[/simterm]

268.5 is the new size of the container as compared to 256MB.

In our future post, we will see how to use Dockerfile to build a docker container. Also, we will get familiar with development of application and how Docker container can be made talk to each other. Till then, Have a great day ahead !!!

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 570+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 8900+ members and discord server close to 2200+ members. You can follow him on Twitter(@ajeetsraina).
Join our Discord Server