制作tomcat的docker镜像
一 commit制作tomcat的docker镜像
1 centos下安装docker
yum -y install docker
启动docker
systemctl start docker
设置开机启动
systemctl start docker
2 安装centos镜像
查找centos镜像
docker search centos
拉取centos镜像
docker pull centos
查看cenots镜像
docker images
3 进入centos容器配置ssh
3.1进入centos容器
docker run -i -t centos /bin/bash
3.2安装网络工具
yum -y install net-tools
3.3安装passwd,openssl,openssh-server
[[email protected] /]# yum install passwd openssl openssh-server -y
启动sshd:
# /usr/sbin/sshd -D //直接/usr/sbin/sshd启动也可以
这时报以下错误:
[[email protected] b5926410fe60 /]# /usr/sbin/sshd
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
执行以下命令解决:
[[email protected] /]# ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ‘‘
[[email protected] /]# ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ‘‘
[[email protected] /]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key -N ‘‘
然后,修改 /etc/ssh/sshd_config 配置信息:
UsePAM yes 改为 UsePAM no
UsePrivilegeSeparation sandbox 改为 UsePrivilegeSeparation no
[[email protected] /]# sed -i "s/#UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config
[[email protected] /]# sed -i "s/UsePAM.*/UsePAM no/g" /etc/ssh/sshd_config
修改完后,重新启动sshd
[[email protected] /]# /usr/sbin/sshd
可以查看到22端口已经起来
[[email protected] /]# netstat -tulnp|grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 90/sshd
tcp6 0 0 :::22 :::* LISTEN 90/sshd
3.4修改root 密码
[[email protected] /]# passwd root
四. 查看容器ip地址(如果宿主机是linux操作系统则跳过这一步)
[[email protected] /]# ip addr ls eth0
84: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::42:acff:fe11:2/64 scope link
valid_lft forever preferred_lft forever
3.5保存当前的镜像,退出会恢复初始状态
[[email protected] ~]# docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
597847f19876 centos "/bin/bash" 17 minutes ago Up 16 minutes suspicious_mccarthy
[[email protected] ~]# docker commit 597847f19876 zhuzhou/centos-1
sha256:8bb2fccf7a1e08a8b3fa30ef6120ae401673180768260336e433a1d00b8c3029
[[email protected] ~]# docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
597847f19876 centos "/bin/bash" 17 minutes ago Up 17 minutes suspicious_mccarthy
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zhuzhou/centos-1 latest 8bb2fccf7a1e 10 seconds ago 280.4 MB
docker.io/centos latest 196e0ce0c9fb 7 weeks ago 196.6 MB
4进入容器部署应用
4.1 退出容器,在设置端口映射进入系统
[[email protected] ~]# docker run -i -t -p 32:22 centos /bin/bash
[[email protected] /]# exit
exit
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# docker run -i -t -p 32:22 zhuzhou/centos-1 /bin/bash
[[email protected] /]#
[[email protected] /]# /usr/sbin/sshd
WARNING: ‘UsePAM no‘ is not supported in Red Hat Enterprise Linux and may cause several problems.
[[email protected] /]#
4.2文件传输到centos容器中
(1) 可以通过xshell工具直接连上本机ip:32端口,可以通过sftp工具直接传输文件到容器内。
(2)通过cp命令将宿主机传送到容器中
[[email protected] home]# docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
741c0bf053d6 zhuzhou/centos-1 "/bin/bash" 8 minutes ago Up 8 minutes 0.0.0.0:32->22/tcp romantic_bassi
[[email protected] home]# docker cp /home/hadoop
hadoop/ hadoop-3.0.0-alpha4/ hadoop-3.0.0-alpha4.tar.gz
[[email protected] home]# docker cp /home/hadoop-3.0.0-alpha4.tar.gz 741c0bf053d6:/home/
[[email protected] home]#
4.3在宿主机中配置Java环境和解压tomcat
4.4 保存当前的镜像
[[email protected] home]# docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
741c0bf053d6 zhuzhou/centos-1 "/bin/bash" 17 minutes ago Up 17 minutes 0.0.0.0:32->22/tcp romantic_bassi
[[email protected] home]# docker commit 741c0bf053d6 zhuzhou/centos-tomcat
sha256:70a6934fbd03fb95dda31e8acf240c07ef8a0aed39853a9e582e34e7ee7ddded
[[email protected] home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zhuzhou/centos-tomcat latest 70a6934fbd03 6 seconds ago 1.067 GB
zhuzhou/centos-1 latest 8bb2fccf7a1e 25 minutes ago 280.4 MB
docker.io/centos latest 196e0ce0c9fb 7 weeks ago 196.6 MB
4.5 启动docker后台运行
[[email protected] ~]# docker run -d -p 8080:8080 -p 32:22 zhuzhou/centos-tomcat /usr/sbin/sshd -D
435a824136e5820c56fa86d78d1131495191a4c01330671cf546a8660b315062
[[email protected] ~]# docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
435a824136e5 zhuzhou/centos-tomcat "/usr/sbin/sshd -D" 7 seconds ago Up 6 seconds 0.0.0.0:8080->8080/tcp, 0.0.0.0:32->22/tcp fervent_euclid
[[email protected] ~]#
5 保存镜像
[[email protected] ~]# docker save -o /home/docker-centos-tomcat.tar zhuzhou/centos-tomcat
[[email protected] ~]# cd /home/
[[email protected] home]# ls
docker-centos-tomcat.tar hadoop hadoop-3.0.0-alpha4 hadoop-3.0.0-alpha4.tar.gz jdk-8u101-linux-x64.rpm joe
[[email protected] home]#
6 导入镜像到另一个系统中
[[email protected] home]# docker load -i docker-centos-tomcat.tar
cf516324493c: Loading layer 205.2 MB/205.2 MB
d849bfac7f38: Loading layer 84.08 MB/84.08 MB
5c0e5e62d633: Loading layer 789.3 MB/789.3 MB
Loaded image: zhuzhou/centos-tomcat:latest MB
[[email protected] home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zhuzhou/centos-tomcat latest 70a6934fbd03 About an hour ago 1.067 GB
二 Dockerfile文件制作tomcat的docker镜像
1 编写Dockerfile文件
[[email protected] docker-build]# cd /home/docker-build/
[[email protected] docker-build]# ls
apache-tomcat-7.0.79 apache-tomcat-7.0.79.tar.gz jdk1.7.0_80 jdk-7u80-linux-x64.tar.gz
[[email protected] docker-build]# vi Dockerfile
FROM centos
MAINTAINER linux
#指定RUN、CMD与ENTRYPOINT命令的工作目录
WORKDIR /home
#安装网络工具和ssh
RUN yum -y install net-tools
#把java与tomcat添加到容器中
ADD apache-tomcat-7.0.79.tar.gz /home
ADD jdk-7u80-linux-x64.tar.gz /home
#安装jdk和tomcat,实际测试中加下面两个解压语句会报错,docker会自动进行解压
#RUN tar -xzvf apache-tomcat-7.0.79.tar.gz
#RUN tar -xzvf jdk-7u80-linux-x64.tar.gz
#配置环境
ENV JAVA_HOME /home/jdk1.7.0_80
ENV CLASS_PATH $JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
ENV PATH $PATH:$JAVA_HOME/bin
#设置tomcat 自启动,测试tomcat没有起来
CMD /home/apache-tomcat-7.0.79/bin/startup.sh
#容器运行时监听的端口
EXPOSE 8080
2 创建镜像
docker build -t zhuzhou/centos-tomcat-2 .
注:如果要ADD本地文件,则本地文件必须在 docker build <PATH>,指定的<PATH>目录下。