Setup Applications in Docker
Let's start on our docker images:
显示docker当前images和所有的containers:
[bruce@rhel6 /]$ sudo docker images -a
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
demo1 import bfefb4ca5804 16 hours ago 318.5 MB
demo2 load 47314df268ef 16 hours ago 305.3 MB
rhel6.5_x86_64_app load 47314df268ef 16 hours ago 305.3 MB
rhel6.5_x86_64 latest 6b4a87d0b323 42 hours ago 305.3 MB
rhel6.5_x86_64_pureos latest 6b4a87d0b323 42 hours ago 305.3 MB
[bruce@rhel6 /]$ sudo docker images --tree
Warning: '--tree' is deprecated, it will be removed soon. See usage.
├─bfefb4ca5804 Virtual Size: 318.5 MB Tags: demo1:import
└─6b4a87d0b323 Virtual Size: 305.3 MB Tags: rhel6.5_x86_64:latest, rhel6.5_x86_64_pureos:latest
└─47314df268ef Virtual Size: 305.3 MB Tags: demo2:load, rhel6.5_x86_64_app:load
[bruce@rhel6 /]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5285300cd9d demo2:load /bin/bash 20 minutes ago Exited (0) 5 minutes ago evil_feynman
7ce41b7a637f demo1:import /bin/bash 15 hours ago Exited (0) 15 hours ago tender_feynman
28723f7f42ec rhel6.5_x86_64:latest ping www.sina.com.cn 17 hours ago Exited (0) 17 hours ago pensive_lalande
[bruce@rhel6 /]$ sudo docker rm c5285300cd9d 7ce41b7a637f 28723f7f42ec
c5285300cd9d
7ce41b7a637f
28723f7f42ec
[bruce@rhel6 /]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[bruce@rhel6 /]$
[bruce@rhel6 /]$ ls /RHEL6.5_X86_64_CD/
EFI EULA_fr EULA_zh LoadBalancer RELEASE-NOTES-bn-IN.html RELEASE-NOTES-gu-IN.html RELEASE-NOTES-ko-KR.html RELEASE-NOTES-pt-BR.html RELEASE-NOTES-zh-CN.html RPM-GPG-KEY-redhat-release
EULA EULA_it GPL media.repo RELEASE-NOTES-de-DE.html RELEASE-NOTES-hi-IN.html RELEASE-NOTES-ml-IN.html RELEASE-NOTES-ru-RU.html RELEASE-NOTES-zh-TW.html ScalableFileSystem
EULA_de EULA_ja HighAvailability Packages RELEASE-NOTES-en-US.html RELEASE-NOTES-it-IT.html RELEASE-NOTES-mr-IN.html RELEASE-NOTES-si-LK.html repodata Server
EULA_en EULA_ko images README RELEASE-NOTES-es-ES.html RELEASE-NOTES-ja-JP.html RELEASE-NOTES-or-IN.html RELEASE-NOTES-ta-IN.html ResilientStorage TRANS.TBL
EULA_es EULA_pt isolinux RELEASE-NOTES-as-IN.html RELEASE-NOTES-fr-FR.html RELEASE-NOTES-kn-IN.html RELEASE-NOTES-pa-IN.html RELEASE-NOTES-te-IN.html RPM-GPG-KEY-redhat-beta
为了方便安装,将某个host的目录作为docker的data volume:
[bruce@rhel6 /]$ sudo docker run -d -t -i -v /RHEL6.5_X86_64_CD:/RHEL6.5_X86_64_CD:ro 47314df268ef /bin/bash
ed25654f00ec1ae0993ba269f9ccc117ec524c10772e9e80cb30e95da7c10b2c
[bruce@rhel6 /]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ed25654f00ec demo2:load /bin/bash 12 seconds ago Up 11 seconds thirsty_nobel
[bruce@rhel6 /]$ sudo docker attach ed25654f00ec
bash-4.1#
bash-4.1#
bash-4.1# ls
RHEL6.5_X86_64_CD bin boot dev etc home lib lib64 media mnt opt proc root sbin selinux srv sys tmp usr var
bash-4.1#
>>> About 'Mount a Host Directory as a Data Volume': Refers to http://docs.docker.com/userguide/dockervolumes/
Mount a Host Directory as a Data Volume
In addition to creating a volume using the -v flag you can also mount a directory from your own host into a container.
$ sudo docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py
This will mount the local directory, /src/webapp, into the container as the /opt/webapp directory.
This is very useful for testing, for example we can mount our source code inside the container and see our application at work as we change the source code.
he directory on the host must be specified as an absolute path and if the directory doesn't exist Docker will automatically create it for you.
Note: This is not available from a Dockerfile due to the portability and sharing purpose of it.
As the host directory is, by its nature, host-dependent, a host directory specified in a Dockerfile probably wouldn't work on all hosts.
Docker defaults to a read-write volume but we can also mount a directory read-only.
$ sudo docker run -d -P --name web -v /src/webapp:/opt/webapp:ro training/webapp python app.py
Here we've mounted the same /src/webapp directory but we've added the ro option to specify that the mount should be read-only.
>>>
修改container中的yum源: 指向data volume。也就是/RHEL6.5_X86_64_CD
bash-4.1# ls /etc/yum.repos.d/
rhel-source.repo
bash-4.1# vi /etc/yum.repos.d/rhel6.5_local.repo
bash-4.1# cat /etc/yum.repos.d/rhel6.5_local.repo
[rhel6.5_x86_64]
name=RHEL6.5_x86_64
#baseurl=ftp://ftp.redhat.com/pub/redhat/linux/enterprise/$releasever/en/os/SRPMS/
#baseurl=file:///RHEL6.5_X86_64_CD/Packages
baseurl=file:///RHEL6.5_X86_64_CD
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
重新更新yum缓存:
bash-4.1# yum clean all
Cleaning repos: rhel6.5_x86_64
Cleaning up Everything
bash-4.1# yum makecache
rhel6.5_x86_64 | 3.9 kB 00:00 ...
rhel6.5_x86_64/group_gz | 204 kB 00:00 ...
rhel6.5_x86_64/filelists_db | 3.8 MB 00:00 ...
rhel6.5_x86_64/primary_db | 3.1 MB 00:00 ...
rhel6.5_x86_64/other_db | 1.6 MB 00:00 ...
Metadata Cache Created
bash-4.1#
bash-4.1# ls /etc/yum.repos.d/
rhel-source.repo rhel6.5_local.repo
安装gcc:
bash-4.1# yum install gcc
Setting up Install Process
Resolving Dependencies
...
Complete!
安装ssh server: 方便后下访问container
bash-4.1# yum install openssh-server
Setting up Install Process
Resolving Dependencies
...
Complete!
更改root密码,方便ssh登录:
bash-4.1# echo "root:123456" | chpasswd
bash-4.1#
bash-4.1# vi /etc/ssh/sshd_config
bash-4.1#
[Refers to https://docs.docker.com/examples/running_ssh_service/]
如果打算使用root登录,则需要将/etc/ssh/sshd_config中的PermitRootLogin打开:设置为yes
>>>
# Authentication:
#LoginGraceTime 2m
PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
>>>
将'session required pam_loginuid.so'修改为'session optional pam_loginuid.so'.
bash-4.1# cat /etc/pam.d/sshd
#%PAM-1.0
auth required pam_sepermit.so
auth include password-auth
account required pam_nologin.so
account include password-auth
password include password-auth
# pam_selinux.so close should be the first session rule
session required pam_selinux.so close
#session required pam_loginuid.so
session optional pam_loginuid.so
# pam_selinux.so open should only be followed by sessions to be executed in the user context
session required pam_selinux.so open env_params
session optional pam_keyinit.so force revoke
session include password-auth
>>>
bash-4.1# exit
exit
[bruce@rhel6 /]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d9304eb0d006 demo2:load /bin/bash 38 minutes ago Exited (0) 3 seconds ago determined_kowalevski
[bruce@rhel6 /]$
保护劳动成果,先commit,作为一个新的image:
[bruce@rhel6 /]$ sudo docker commit -m "pure os with yum source and installed gcc/ssh server" -a "Bruce" d9304eb0d006 rhel6.5_x86_64_plus_gcc_ssh:latest
c51b278af42162097402fac7315581f905d2bb4f8e3ba897901ccec90aa7396d
[bruce@rhel6 /]$ sudo docker images -a
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
rhel6.5_x86_64_plus_gcc_ssh latest c51b278af421 10 seconds ago 406.5 MB
demo1 import bfefb4ca5804 21 hours ago 318.5 MB
demo2 load 47314df268ef 21 hours ago 305.3 MB
rhel6.5_x86_64_app load 47314df268ef 21 hours ago 305.3 MB
rhel6.5_x86_64 latest 6b4a87d0b323 46 hours ago 305.3 MB
rhel6.5_x86_64_pureos latest 6b4a87d0b323 46 hours ago 305.3 MB
[bruce@rhel6 /]$
[bruce@rhel6 /]$ sudo docker images --tree
Warning: '--tree' is deprecated, it will be removed soon. See usage.
├─bfefb4ca5804 Virtual Size: 318.5 MB Tags: demo1:import
└─6b4a87d0b323 Virtual Size: 305.3 MB Tags: rhel6.5_x86_64:latest, rhel6.5_x86_64_pureos:latest
└─47314df268ef Virtual Size: 305.3 MB Tags: demo2:load, rhel6.5_x86_64_app:load
└─c51b278af421 Virtual Size: 406.5 MB Tags: rhel6.5_x86_64_plus_gcc_ssh:latest
[bruce@rhel6 /]$
将rhel6.5_x86_64_plus_gcc_ssh运行起来,但是将container中的22端口映射出来:
[bruce@rhel6 /]$ sudo docker run -d -p 40022:22 c51b278af421 /usr/sbin/sshd -D
检查端口是否映射成功:
[bruce@rhel6 /]$ ssh root@127.0.0.1 -p 40022
root@127.0.0.1's password:
Last login: Wed Oct 22 02:40:43 2014 from localhost
-bash-4.1#
-bash-4.1#
-bash-4.1#
-bash-4.1# ls
-bash-4.1# cd /
-bash-4.1# ls
bin boot dev etc home lib lib64 media mnt opt proc RHEL6.5_X86_64_CD root sbin selinux srv sys tmp usr var
-bash-4.1# ls RHEL6.5_X86_64_CD/
-bash-4.1# cd ..
-bash-4.1# ls
bin boot dev etc home lib lib64 media mnt opt proc RHEL6.5_X86_64_CD root sbin selinux srv sys tmp usr var
-bash-4.1#
启动另一个container,同样做sshd,映射host端口40023到container的22端口,并且将host目录/RHEL6.5_X86_64_CD作为container的data volume:
[bruce@rhel6 /]$ sudo docker run -d -p 40023:22 -v /RHEL6.5_X86_64_CD:/RHEL6.5_X86_64_CD:ro 8b679759fb20 /usr/sbin/sshd -D
311cf126d942d3b97281cb9f9ec7981d53fd298e5d976cf40c04171e0f3fb0af
[bruce@rhel6 /]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
311cf126d942 rhel6.5_x86_64_plus_gcc_ssh:latest /usr/sbin/sshd -D 9 seconds ago Up 9 seconds 0.0.0.0:40023->22/tcp sad_ardinghelli
33ad8437c577 rhel6.5_x86_64_plus_gcc_ssh:latest /usr/sbin/sshd -D 12 minutes ago Up 12 minutes 0.0.0.0:40022->22/tcp drunk_poincare
[bruce@rhel6 /]$
[bruce@rhel6 /]$ sudo docker images -a
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
rhel6.5_x86_64_plus_gcc_ssh latest 8b679759fb20 About an hour ago 421.2 MB
rhel6.5_x86_64_plus_gcc_ssh base c51b278af421 2 hours ago 406.5 MB
demo2 load 47314df268ef 24 hours ago 305.3 MB
rhel6.5_x86_64_app load 47314df268ef 24 hours ago 305.3 MB
rhel6.5_x86_64 latest 6b4a87d0b323 2 days ago 305.3 MB
rhel6.5_x86_64_pureos latest 6b4a87d0b323 2 days ago 305.3 MB
[bruce@rhel6 /]$ sudo docker save 8b679759fb20 > /tmp/rhel6.5_x86_64_plus_gcc_ssh_20141022A.tar
[bruce@rhel6 /]$ sudo ps -elf | grep dock
4 S root 1436 1 0 80 0 - 181762 pipe_w 07:58 ? 00:00:31 /usr/bin/docker -d --exec-driver=lxc
4 S root 10166 1436 0 80 0 - 5831 ep_pol 14:50 ? 00:00:00 lxc-start -n 33ad8437c577bccbbdc961479c68e6b5da1cf7863d7f9574790b2dad0a7c4778 -f /var/lib/docker/containers/33ad8437c577bccbbdc961479c68e6b5da1cf7863d7f9574790b2dad0a7c4778/config.lxc -- /.dockerinit -driver lxc -g 172.17.42.1 -i 172.17.0.21/16 -mtu 1500 -- /usr/sbin/sshd -D
4 S root 10701 1436 0 80 0 - 5831 ep_pol 15:03 ? 00:00:00 lxc-start -n 311cf126d942d3b97281cb9f9ec7981d53fd298e5d976cf40c04171e0f3fb0af -f /var/lib/docker/containers/311cf126d942d3b97281cb9f9ec7981d53fd298e5d976cf40c04171e0f3fb0af/config.lxc -- /.dockerinit -driver lxc -g 172.17.42.1 -i 172.17.0.22/16 -mtu 1500 -- /usr/sbin/sshd -D
0 S bruce 11199 2281 0 80 0 - 25814 pipe_w 16:34 pts/1 00:00:00 grep dock
[bruce@rhel6 /]$ ls -al /tmp/rhel6.5_*
-rw-rw-r-- 1 bruce bruce 328040448 Oct 21 16:24 /tmp/rhel6.5_demo_export.tar
-rw-rw-r-- 1 bruce bruce 328047104 Oct 21 16:25 /tmp/rhel6.5_demo_save.tar
-rw-rw-r-- 1 bruce bruce 445291008 Oct 22 16:23 /tmp/rhel6.5_x86_64_plus_gcc_ssh_20141022A.tar
---------------------------------------------------------------------------------------
安装jdk and tomcat
[bruce@rhel6 ~]$ ssh root@127.0.0.1 -p 40023
root@127.0.0.1's password:
Last login: Wed Oct 22 05:19:24 2014 from 172.17.42.1
-bash-4.1# mkdir -p /u01/app/software
-bash-4.1#
-bash-4.1#
-bash-4.1# usermod -g oinstall bruce
-bash-4.1# useradd -g oinstall oracle
-bash-4.1# mkdir -p /uloc
-bash-4.1# mkdir -p /utxt
-bash-4.1# mkdir -p /u01/app/software
-bash-4.1# chown -R bruce:oinstall /uloc
-bash-4.1# chown -R bruce:oinstall /utxt
-bash-4.1# chown -R oracle:oinstall /u01
-bash-4.1# chown -R bruce:oinstall /u01/app/software
-bash-4.1# ls -al /
total 8816
dr-xr-xr-x. 25 root root 4096 Oct 23 04:07 .
dr-xr-xr-x. 25 root root 4096 Oct 23 04:07 ..
-rw------- 1 root root 1316 Oct 22 02:47 .bash_history
-rw------- 1 root root 102 Oct 23 03:03 .dockerenv
-rwx------. 1 root root 8915444 Oct 19 23:29 .dockerinit
drwxr-xr-x. 12 root root 4096 Oct 22 05:19 RHEL6.5_X86_64_CD
dr-xr-xr-x. 2 root root 4096 Oct 20 02:42 bin
dr-xr-xr-x. 2 root root 4096 Jun 28 2011 boot
drwxr-xr-x. 5 root root 4096 Oct 23 03:03 dev
drwxr-xr-x. 45 root root 4096 Oct 23 04:03 etc
drwxr-xr-x. 4 root root 4096 Oct 23 03:57 home
dr-xr-xr-x. 7 root root 4096 Oct 22 01:05 lib
dr-xr-xr-x. 6 root root 12288 Oct 22 04:18 lib64
drwxr-xr-x. 2 root root 4096 Jun 28 2011 media
drwxr-xr-x. 2 root root 4096 Jun 28 2011 mnt
drwxr-xr-x. 2 root root 4096 Jun 28 2011 opt
dr-xr-xr-x 175 root root 0 Oct 23 03:03 proc
dr-xr-x---. 3 root root 4096 Oct 23 04:07 root
dr-xr-xr-x. 2 root root 4096 Oct 20 02:42 sbin
drwxr-xr-x. 2 root root 4096 Jun 28 2011 selinux
drwxr-xr-x. 2 root root 4096 Jun 28 2011 srv
drwxr-xr-x 13 root root 0 Oct 22 21:44 sys
drwxrwxrwt. 2 root root 4096 Oct 22 05:23 tmp
drwxr-xr-x 3 oracle oinstall 4096 Oct 22 05:28 u01
drwxr-xr-x 2 bruce oinstall 4096 Oct 23 04:07 uloc
drwxr-xr-x. 13 root root 4096 Oct 20 02:41 usr
drwxr-xr-x 2 bruce oinstall 4096 Oct 23 04:07 utxt
drwxr-xr-x. 17 root root 4096 Oct 20 02:41 var
-bash-4.1# exit
logout
[bruce@311cf126d942 ~]$
[bruce@311cf126d942 ~]$ cp /RHEL6.5_X86_64_CD/jdk1.6.0_21.tar.gz /u01/app/software/
[bruce@311cf126d942 ~]$ [bruce@311cf126d942 ~]$ cp /RHEL6.5_X86_64_CD/apache-tomcat-6.0.18.tar.gz /u01/app/software/
[bruce@311cf126d942 ~]$ cd /u01/app/
[bruce@311cf126d942 ~]$ chown -R bruce:oinstall software/
[bruce@311cf126d942 ~]$ cd software/
[bruce@311cf126d942 ~]$ tar xzvf jdk1.6.0_21.tar.gz
[bruce@311cf126d942 ~]$ tar xzvf jdk1.6.0_21.tar.gz
[bruce@311cf126d942 ~]$ tar xzvf apache-tomcat-6.0.18.tar.gz
[bruce@311cf126d942 ~]$ cd ~
[bruce@311cf126d942 ~]$ vi .bash_profile
[bruce@311cf126d942 ~]$ cat .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
#Java Setting
#export PATH
export JAVA_HOME=/u01/app/software/jdk1.6.0_21
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
CATALINA_HOME=/u01/app/software/apache-tomcat-6.0.18
JRE_HOME=$JAVA_HOME/jre
安装oracle client:
将压缩好的oracle client解压到/u01/app/目录: 这样会有文件夹/u01/app/client_1
在.bash_profile中增加以下变量
[bruce@311cf126d942 ~]$ vi .bash_profile
# Oracle Settings
TMP=/tmp; export TMP
TMPDIR=$TMP; export TMPDIR
#oracle 10g
#export ORACLE_BASE=/u01/app/oracle
#export ORACLE_HOME=$ORACLE_BASE/product/10.2.0/client_1
#export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
#oracle 11g
export ORACLE_BASE=/u01/app/client_1
export ORACLE_HOME=/u01/app/client_1
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
export PATH=$ORACLE_HOME/bin:$PATH
export LANG="en_US.UTF-8"
export NLS_LANG="American_America.AL32UTF8"
#SIMPLIFIED CHINESE_CHINA.ZHS16GBK
#export LANG="zh_CN.GBK"
#export NLS_LANG="American_America.ZHS16GBK"
export NLS_DATE_FORMAT="YYYY-MM-DD HH24:MI:SS"