环境
主机环境 MBP macos
虚拟机主机环境 centos7 (virtualbox中的一个vm)
centos7中安装docker(基于centos:centos6 image)
注:之前在Ubuntu(12.04,14.0x)上试着配置过tomcat,遇到了dialog,jdk安装问题。
安装配置
pull centos image
[root@lydcentos7 centos6]# docker pull centos:centos6
download jdk and tomcat
[root@lydcentos7 centos6]# wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense
=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.tar.gz"
[root@lydcentos7 centos6]# wget http://mirrors.noc.im/apache/tomcat/tomcat-7/v7.0.68/bin/apache-tomcat-7.0.68.tar.gz
注意wget jdk时,要加wget的一些参数才能正常下载jdk包。
edit Dockerfile
FROM centos:centos6
MAINTAINER lreeder
£Helpful utils, but only sudo is required
£RUN yum -y install tar
£RUN yum -y install vim
£RUN yum -y install nc
RUN yum -y install sudo
££££££££ JDK7
£Note that ADD uncompresses this tarball automatically
ADD jdk-7u79-linux-x64.tar.gz /opt
WORKDIR /opt/jdk1.7.0_79
RUN alternatives --install /usr/bin/java java /opt/jdk1.7.0_79/bin/java 1
RUN alternatives --install /usr/bin/jar jar /opt/jdk1.7.0_79/bin/jar 1
RUN alternatives --install /usr/bin/javac javac /opt/jdk1.7.0_79/bin/javac 1
RUN echo "JAVA_HOME=/opt/jdk1.7.0_79" >> /etc/environment
££££££££ TOMCAT
£Note that ADD uncompresses this tarball automatically
ADD apache-tomcat-7.0.68.tar.gz /usr/share
WORKDIR /usr/share/
RUN mv apache-tomcat-7.0.68 tomcat7
RUN echo "JAVA_HOME=/opt/jdk1.7.0_79/" >> /etc/default/tomcat7
RUN groupadd tomcat
RUN useradd -s /bin/bash -g tomcat tomcat
RUN chown -Rf tomcat.tomcat /usr/share/tomcat7
EXPOSE 8080
注:£ == #
install 命令一定加上 -y
目录结构
[root@lydcentos7 centos6]£ ll
total 155M
drwxr-xr-x 2 root root 89 Apr 9 15:14 .
-rw-r--r-- 1 root root 950 Apr 9 14:47 Dockerfile
drwxr-xr-x. 6 root root 60 Apr 9 14:10 ..
-rw-r--r-- 1 root root 8.5M Feb 9 05:26 apache-tomcat-7.0.68.tar.gz
-rw-r--r-- 1 root root 147M Apr 14 2015 jdk-7u79-linux-x64.tar.gz
build image
[root@lydcentos7 centos6]# docker build --rm=true -t centos6/tomcat7 .
...
Successfully built bc8fc2797f40
build image 成功之后会返回一个image Id,使用该imageId 或者 image Name就可以启动tomcat了。
start tomcat
不设置端口映射启动
#--name tomcat7 为image name
[root@lydcentos7 centos6]# docker run --rm=true -t -i --name tomcat7 centos6/tomcat7 /usr/bin/sudo -u tomcat /usr/share/tomcat7/bin/catalina.sh run
...
INFO: Server startup in 42027 ms
设置端口映射启动
添加参数 -p 80:8080
[root@lydcentos7 centos6]# docker run -p 80:8080 --rm=true -t -i --name tomcat7 centos6/tomcat7 /usr/bin/sudo -u tomcat /usr/share/tomcat7/bin/catalina.sh run
...
INFO: Server startup in 42027 ms
查看image container ip
[root@lydcentos7 centos6]£ docker inspect --format '{{ .NetworkSettings.IPAddress }}' tomcat7
172.17.0.2
test tomcat
[root@lydcentos7 centos6]£ curl 172.17.0.2:8080
如果设置了端口映射,还可以在mac中访问vm ip:port 来验证tomcat启动情况。如本例中在mac中使用浏览器访问http://192.168.56.101:80。(192.168.56.101为vm lydcentos7的ip)