Wednesday, June 22, 2016

Docker test drive (v1.11)



Detached mode =>

docker run \
 --name esearch \
 -d \
 -p 10.0.2.41:9200:9200 \
 -p 10.0.2.41:9300:9300 \
 elasticsearch:2.3.3

docker logs -f esearch


Foreground mode =>

docker run \
 --name esearch-fg \
 -i -t \
 -p 10.0.2.41:9200:9200 \
 -p 10.0.2.41:9300:9300 \
 elasticsearch:2.3.3 \
/bin/bash


Create user defined network

$docker network create \
--driver=bridge \
--subnet=172.28.0.0/16 \
--ip-range=172.28.5.0/24 \
--gateway=172.28.5.254 \
my-net

$docker network ls
$docker network inspect my-net


Setting IP address only on user defined network
...
--net="my-net"
--add-host="esearch:172.28.5.1"
--mac-address="7a:07:51:53:31:21"       # MAC Address Generator
--ip="172.28.5.1"
...

Restart policies (--restart)

--restart=no                         # default
--restart=always                  # always restart especially on docker engine starting
--restart=unless-stopped     # if normal stop, do not restart
--restart=on-failure:<max retry>


Cleanup after exit

-rm



Override Dockerfile at run-time
*** But cannot be overridden FROM, MAINTAINER, RUN, and ADD command

CMD (default command or options)
Ex.

ubuntu@node1:~$ docker run --rm ubuntu /bin/bash -c "pwd;ls -l"
/
total 64
drwxr-xr-x   2 root root 4096 May 25 23:11 bin
drwxr-xr-x   2 root root 4096 Apr 12 20:14 boot
drwxr-xr-x   5 root root  360 Jun 22 08:18 dev
drwxr-xr-x  45 root root 4096 Jun 22 08:18 etc
drwxr-xr-x   2 root root 4096 Apr 12 20:14 home
drwxr-xr-x   8 root root 4096 Sep 13  2015 lib
drwxr-xr-x   2 root root 4096 May 25 23:11 lib64
drwxr-xr-x   2 root root 4096 May 25 23:11 media
drwxr-xr-x   2 root root 4096 May 25 23:11 mnt
drwxr-xr-x   2 root root 4096 May 25 23:11 opt
dr-xr-xr-x 118 root root    0 Jun 22 08:18 proc
drwx------   2 root root 4096 May 25 23:11 root
drwxr-xr-x   5 root root 4096 May 25 23:11 run
drwxr-xr-x   2 root root 4096 May 27 14:14 sbin
drwxr-xr-x   2 root root 4096 May 25 23:11 srv
dr-xr-xr-x  13 root root    0 Jun 22 08:18 sys
drwxrwxrwt   2 root root 4096 May 25 23:11 tmp
drwxr-xr-x  11 root root 4096 May 27 14:14 usr
drwxr-xr-x  13 root root 4096 May 27 14:14 var


ENTRYPOINT (default command to execute at runtime)
Ex.

ubuntu@node1:~$ docker run --rm --entrypoint /bin/bash ubuntu -c "pwd; ls -l"
/
total 64
drwxr-xr-x   2 root root 4096 May 25 23:11 bin
drwxr-xr-x   2 root root 4096 Apr 12 20:14 boot
drwxr-xr-x   5 root root  380 Jun 22 08:23 dev
drwxr-xr-x  45 root root 4096 Jun 22 08:23 etc
drwxr-xr-x   2 root root 4096 Apr 12 20:14 home
drwxr-xr-x   8 root root 4096 Sep 13  2015 lib
drwxr-xr-x   2 root root 4096 May 25 23:11 lib64
drwxr-xr-x   2 root root 4096 May 25 23:11 media
drwxr-xr-x   2 root root 4096 May 25 23:11 mnt
drwxr-xr-x   2 root root 4096 May 25 23:11 opt
dr-xr-xr-x 119 root root    0 Jun 22 08:23 proc
drwx------   2 root root 4096 May 25 23:11 root
drwxr-xr-x   5 root root 4096 May 25 23:11 run
drwxr-xr-x   2 root root 4096 May 27 14:14 sbin
drwxr-xr-x   2 root root 4096 May 25 23:11 srv
dr-xr-xr-x  13 root root    0 Jun 22 08:23 sys
drwxrwxrwt   2 root root 4096 May 25 23:11 tmp
drwxr-xr-x  11 root root 4096 May 27 14:14 usr
drwxr-xr-x  13 root root 4096 May 27 14:14 var

EXPOSE (incoming ports/links)
Ex.

ubuntu@node1:~$ docker run --restart=always --name esearch -d -p 10.0.2.41:9200:9200 -p 10.0.2.41:9300:9300 --net="my-net" --mac-address="7a:07:51:53:31:21" --add-host="esearch:172.28.5.1" --ip="172.28.5.1" elasticsearch:2.3.3
f49f048b1fd6f8060c54e8307dcc921ded1d9cf2d1bd7f0e9cc5d4a6f08ddf26

*** Warning: Before running kibana container an elastic search docker container should be started from previous step aboved
*** and set kibana in user defined network as same as elastic search like this:
...
--net="my-net"
--add-host="esearch:172.28.5.1"
--mac-address="02:42:ac:1c:05:00"
--ip="172.28.5.2"
...

ubuntu@node1:~$ docker run --restart=always --name kibana-es --net="my-net" --add-host="esearch:172.28.5.1" --mac-address="02:42:ac:1c:05:00" --ip="172.28.5.2" --link esearch:elasticsearch -p 10.0.2.41:5601:5601 -d kibana:4.5.1
b9203b89184f463625b2ade973a23812436b7242a01f831ca2fb46b084b3dc37



ENV (environment variables)
Ex.

ubuntu@node1:/$ docker run -e "ORACLE_SID=orcl" -e "ORACLE_HOME=/home/oracle" --rm ubuntu /bin/bash -c 'echo $ORACLE_SID at $ORACLE_HOME'
orcl at /home/oracle
ubuntu@node1:/$ docker run -e "ORACLE_SID=orcl" -e "ORACLE_HOME=/home/oracle" --rm ubuntu /bin/bash -c export
declare -x HOME="/root"
declare -x HOSTNAME="d53267da603e"
declare -x OLDPWD
declare -x ORACLE_HOME="/home/oracle"
declare -x ORACLE_SID="orcl"
declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
declare -x PWD="/"
declare -x SHLVL="1"

TMPFS (mount tmpfs filesystems)
Ex.

ubuntu@node1:/$ docker run -it --tmpfs /run:rw,noexec,nosuid,size=65536k ubuntu /bin/bash
root@6f916416c2fb:/# df -k
Filesystem     1K-blocks    Used Available Use% Mounted on
none            20510312 4999012  14446396  26% /
tmpfs             508852       0    508852   0% /dev
tmpfs             508852       0    508852   0% /sys/fs/cgroup
/dev/vda1       20510312 4999012  14446396  26% /etc/hosts
shm                65536       0     65536   0% /dev/shm
tmpfs              65536       0     65536   0% /run

VOLUME (shared filesystems)
Ex.


ubuntu@node1:/$ docker run -d  -P --name web -v /webapp training/webapp python app.py
b6797f0526869d23fdd510ced79bfb5987a2f2488b4cd41b295e6f0d02391a0f

or detail see here

USER (default user in container is root)
Ex. default Ubuntu image do not have user name "nutt"


ubuntu@node1:/$ docker run --rm -it --user="nutt:nutt"  ubuntu /bin/bash
docker: Error response from daemon: linux spec user: unable to find user nutt: no matching entries in passwd file.
                                                                                                                  ubuntu@node1:/$ 
ubuntu@node1:/$ docker run -it --name test ubuntu /bin/bash
root@2e6c236c261a:/# groupadd nutt
root@2e6c236c261a:/# useradd -g nutt nutt
root@2e6c236c261a:/# passwd nutt
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
root@2e6c236c261a:/# su - nutt
No directory, logging in with HOME=/
$ pwd
/
$ id
uid=1000(nutt) gid=1000(nutt) groups=1000(nutt)
$ exit
logout
root@2e6c236c261a:/# exit
exit
ubuntu@node1:/$ docker ps -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED              STATUS                         PORTS                                                NAMES
2e6c236c261a        ubuntu                "/bin/bash"              About a minute ago   Exited (0) 47 seconds ago                                                           test
6f916416c2fb        ubuntu                "/bin/bash"              25 minutes ago       Exited (0) 24 minutes ago                                                           tender_heisenberg
5ac0ccac94d6        ubuntu                "/bin/bash"              25 minutes ago       Exited (0) 25 minutes ago                                                           distracted_galileo
b55279b6855d        ubuntu                "/bin/bash"              26 minutes ago       Exited (0) 26 minutes ago                                                           adoring_colden
b9203b89184f        kibana:4.5.1          "/docker-entrypoint.s"   51 minutes ago       Up 51 minutes                  10.0.2.41:5601->5601/tcp                             kibana-es
7b11297d9624        kibana                "/docker-entrypoint.s"   About an hour ago    Created                                                                             elated_kare
a5ee1e2b9cb3        kibana                "/docker-entrypoint.s"   About an hour ago    Created                                                                             angry_hopper
f49f048b1fd6        elasticsearch:2.3.3   "/docker-entrypoint.s"   About an hour ago    Up About an hour               10.0.2.41:9200->9200/tcp, 10.0.2.41:9300->9300/tcp   esearch
8b6a6f326d46        ubuntu                "/bin/bash -c 'pwd; l"   About an hour ago    Exited (0) About an hour ago                                                        sleepy_panini
2054b3f0bb24        redis                 "/bin/bash -c 'pwd; l"   About an hour ago    Exited (0) About an hour ago                                                        sleepy_jepsen
d99d05c6d6f5        redis                 "/bin/bash -c ls -l"     About an hour ago    Exited (0) About an hour ago                                                        sad_blackwell
ubuntu@node1:/$ 
ubuntu@node1:/$ docker commit test ubuntu:nutt
sha256:363a03f7553ea80d4caa06ad5815d53658577ac0b1ff840c0902552afc85d8d6
ubuntu@node1:/$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              nutt                363a03f7553e        6 seconds ago       122.3 MB
myhtop              1.0                 681356613e26        4 hours ago         7.418 MB
redis               latest              4465e4bcad80        4 days ago          185.7 MB
kibana              4.5.1               298836bc4170        12 days ago         306.6 MB
kibana              latest              298836bc4170        12 days ago         306.6 MB
java                8-jre               76fd51ceaa2e        12 days ago         312.2 MB
elasticsearch       2.3.3               15930a3e11bf        12 days ago         346.6 MB
elasticsearch       latest              15930a3e11bf        12 days ago         346.6 MB
hello-world         latest              693bce725149        2 weeks ago         967 B
alpine              latest              f70c828098f5        2 weeks ago         4.799 MB
centos              7                   904d6c400333        2 weeks ago         196.8 MB
ubuntu              16.04               2fa927b5cdd3        3 weeks ago         122 MB
ubuntu              latest              2fa927b5cdd3        3 weeks ago         122 MB
oraclelinux         7.2                 df602a268e64        6 weeks ago         276.2 MB
busybox             latest              47bcc53f74dc        3 months ago        1.113 MB
training/webapp     latest              6fae60ef3446        13 months ago       348.8 MB
ubuntu@node1:/$ docker run --rm -it --user="nutt:nutt"  ubuntu:nutt /bin/bash
nutt@eaae30b33086:/$ id
uid=1000(nutt) gid=1000(nutt) groups=1000(nutt)
nutt@eaae30b33086:/$ exit
exit

WORKDIR
Ex.

ubuntu@node1:/$ docker run --rm -it --user="nutt:nutt" -w="/var/log"  ubuntu:nutt /bin/bash
nutt@9980685d91ef:/var/log$ exit
exit






Good luck ;-)


No comments:

Post a Comment