从零开始构建一个centos+jdk7+tomcat7的镜像文件
准备centos基础镜像
docker pull centos 或者直接下载我准备好的镜像 docker pull registry.cn-hangzhou.aliyuncs.com/repos_zyl/centos:0.0.1
准备jdk7和tomcat7安装包
- 创建工作目录, mkdir -p /z/docker
- 准备下载jdk7的tar.gz包http://download.oracle.com/otn/java/jdk/7u79-b15/jdk-7u79-linux-x64.tar.gz
- 解压到目录/z/docker下, 并重命名为jdk
- 准备下载tomcat7的tar.gz包http://mirrors.cnnic.cn/apache/tomcat/tomcat-7/v7.0.75/bin/apache-tomcat-7.0.75.tar.gz
- 解压到目录/z/docker下, 并重命名为tomcat
- 删掉jdk文件夹下多余文件, 降低build的镜像大小
rm -rf /z/docker/jdk/*src.zip \ /z/docker/jdk/lib/missioncontrol \ /z/docker/jdk/lib/visualvm \ /z/docker/jdk/lib/*javafx* \ /z/docker/jdk/jre/lib/plugin.jar \ /z/docker/jdk/jre/lib/ext/jfxrt.jar \ /z/docker/jdk/jre/bin/javaws \ /z/docker/jdk/jre/lib/javaws.jar \ /z/docker/jdk/jre/lib/desktop \ /z/docker/jdk/jre/plugin \ /z/docker/jdk/jre/lib/deploy* \ /z/docker/jdk/jre/lib/*javafx* \ /z/docker/jdk/jre/lib/*jfx* \ /z/docker/jdk/jre/lib/amd64/libdecora_sse.so \ /z/docker/jdk/jre/lib/amd64/libprism_*.so \ /z/docker/jdk/jre/lib/amd64/libfxplugins.so \ /z/docker/jdk/jre/lib/amd64/libglass.so \ /z/docker/jdk/jre/lib/amd64/libgstreamer-lite.so \ /z/docker/jdk/jre/lib/amd64/libjavafx*.so \ /z/docker/jdk/jre/lib/amd64/libjfx*.so
进入/z/docker目录, 在当前目录下创建Dockerfile文件
cd /z/docker vi Dockerfile #插入以下内容 #使用的基础镜像 FROM registry.cn-hangzhou.aliyuncs.com/repos_zyl/centos:0.0.1 #作者信息 MAINTAINER yzzhouyalei "yzzhouyalei@foxmail.com" #创建目录 RUN mkdir -p /z/java/jdk1.7.0_79 #把当前目录下的jdk文件夹添加到镜像 ADD jdk /z/java/jdk1.7.0_79 #创建tomcat目录 RUN mkdir -p /z/java/apache-tomcat-7.0.70 #把当前目录下的tomcat文件夹添加到镜像 ADD tomcat /z/java/apache-tomcat-7.0.70 #添加环境变量 ENV JAVA_HOME /z/java/jdk1.7.0_79 ENV CATALINA_HOME /z/java/apache-tomcat-7.0.70 ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin #暴露8080端口 EXPOSE 8080 #启动时运行tomcat CMD ["/z/java/apache-tomcat-7.0.70/bin/catalina.sh","run"]
开始构建docker镜像文件
docker build -t repos_local/centos-jdk7-tomcat7:0.0.1 . -t 设置tag名称, 命名规则registry/image:tag . 表示使用当前目录下的Dockerfile文件
等待构建完成, 不出意外的话, 执行docker images将会得到以下结果
[root@localhost docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE repos_local/centos-jdk7-tomcat7 0.0.1 2c5efe7056ce 4 minutes ago 511.7 MB
repos_local/centos-jdk7-tomcat7镜像已经构建好了, 从这个镜像启动容器, 并配置好端口, 就能直接在浏览器上看到tomcat默认界面了
启动centos-jdk7-tomcat7容器
docker run -d -p 8081:8080 --name test-tomcat repos_local/centos-jdk7-tomcat7:0.0.1 -d 后台运行 -p 宿主机port:容器port 端口映射 --name 容器名称, 可以通过容器名称对容器操作
如果系统没禁用防火墙的话, 可能需要开放8081端口
#直接执行命令, 开放端口号(重启后可能失效)
iptables -A INPUT -ptcp --dport 8081 -j ACCEPT
##或者
#直接修改iptables, 重启后不会失效
vi /etc/sysconfig/iptables
#插入以下内容
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8081 -j ACCEPT
重启iptables服务
service iptables restart
centos7下可以直接使用firewall-cmd来开放端口
#开放8081端口 permanent为永久开放 firewall-cmd --zone=public --add-port=8081/tcp --permanent #重新读取配置 firewall-cmd --reload #查看全部开放端口 firewall-cmd --list-all
如果提示FirewallD is not running
执行systemctl start firewalld.service, 启动firewalld服务
直接在浏览器访问centos7系统的8081端口, 已经能看到tomcat的默认界面了
查看container容器的运行状态
[root@localhost docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1a49a277c235 repos_local/centos-jdk7-tomcat7:0.0.1 "/z/java/apache-tomca" 3 minutes ago Up 3 minutes 0.0.0.0:8081->8080/tcp test-tomcat
现在的容器状态为Up, 运行中, 如何停止一个运行中的容器? 运行docker stop命令
[root@localhost docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1a49a277c235 repos_local/centos-jdk7-tomcat7:0.0.1 "/z/java/apache-tomca" 47 hours ago Exited (143) 3 minutes ago test-tomcat [root@localhost docker]# docker start test-tomcat test-tomcat [root@localhost docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1a49a277c235 repos_local/centos-jdk7-tomcat7:0.0.1 "/z/java/apache-tomca" 47 hours ago Up 3 seconds 0.0.0.0:8081->8080/tcp test-tomcat [root@localhost docker]# docker stop test-tomcat test-tomcat [root@localhost docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1a49a277c235 repos_local/centos-jdk7-tomcat7:0.0.1 "/z/java/apache-tomca" 47 hours ago Exited (143) 4 seconds ago test-tomcat
以上命令, 把一个容器从退出状态->运行中->退出状态
如何强制删除一个运行中的容器?
[root@localhost docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1a49a277c235 repos_local/centos-jdk7-tomcat7:0.0.1 "/z/java/apache-tomca" 47 hours ago Up 5 seconds 0.0.0.0:8081->8080/tcp test-tomcat [root@localhost docker]# docker rm -f test-tomcat test-tomcat [root@localhost docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost docker]#
docker rm 命令删除一个Exited状态的容器, docker rm -f 强制删除一个运行中的容器
到这里, 在centos7系统下构建docker镜像文件已经成功