环境
docker-ce-19.03.1-3.el7.x86_64
centos 7
一、创建centos+jdk+tomcat镜像
对于公用的容器比如,tomcat、nginx、mysql等应用组件可以直接使用官方的容器,不必自己创建;如果需要创建自定义容器,需要下载对应软件官网tar包进行挂载。
1、准备基础镜像和tar包
使用centos7作为基础镜像
[root@node105 ~]# docker pull centos:7
[root@node105 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 67fa590cfc1c days ago 202MB [root@node105 src]# cd /usr/local/src && ll
total
-rw-r--r--. 1 root root 9722154 Aug 25 15:47 apache-tomcat-8.5.45.tar.gz
-rw-r--r--. 1 root root 181260798 Aug 25 15:45 jdk-8u65-linux-x64.tar.gz
2、创建Dockerfile
[root@node105 src]# vi centos-jdk-tomcat-Dockerfile
#使用的基础镜像
FROM centos:
#作者信息
MAINTAINER cac2020 "cac2020@163.com" #使用ADD指令会直接对tar.gz进行解压缩,不用再单独的tar解压jdk了
ADD jdk-8u65-linux-x64.tar.gz /usr/local
ADD apache-tomcat-8.5..tar.gz /usr/local #添加环境变量
ENV JAVA_HOME /usr/local/jdk1..0_65
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin #暴露8080端口
EXPOSE #启动时运行tomcat
CMD ["/usr/local/apache-tomcat-8.5.45/bin/catalina.sh","run"]
3、使用Dockerfile创建镜像
[root@node105 src]# docker build -t centos-jdk-tomcat:0.0. . -f centos-jdk-tomcat-Dockerfile
Sending build context to Docker daemon 191MB
Step / : FROM centos:
---> 67fa590cfc1c
Step / : MAINTAINER cac2020 "cac2020@163.com"
---> Running in b48a9a1c9456
Removing intermediate container b48a9a1c9456
---> cf88aebfb290
Step / : ADD jdk-8u65-linux-x64.tar.gz /usr/local
---> a80fd8584d3e
Step / : ADD apache-tomcat-8.5..tar.gz /usr/local
---> 3951fd837bd5
Step / : ENV JAVA_HOME /usr/local/jdk1..0_65
---> Running in 8ea280c20bbe
Removing intermediate container 8ea280c20bbe
---> 5e2c39e6b557
Step / : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
---> Running in 7dfbb7d72fd6
Removing intermediate container 7dfbb7d72fd6
---> 2651de66a845
Step / : ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.
---> Running in 490f9dd59d99
Removing intermediate container 490f9dd59d99
---> 70ff0149c070
Step / : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin
---> Running in 867f19619c72
Removing intermediate container 867f19619c72
---> 8a1b71576d7b
Step / : EXPOSE
---> Running in 13d2b33647d2
Removing intermediate container 13d2b33647d2
---> 341bc406be44
Step / : CMD ["/usr/local/apache-tomcat-8.5.45/bin/catalina.sh","run"]
---> Running in 88f261af47f4
Removing intermediate container 88f261af47f4
---> e77fdb372830
Successfully built e77fdb372830
Successfully tagged centos-jdk-tomcat:0.0.
[root@node105 src]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-jdk-tomcat 0.0.1 e77fdb372830 seconds ago 581MB
centos 67fa590cfc1c days ago 202MB
备注:
(1)-t 指定镜像的名称和tag;
(2)使用-f 指定要使用的dockerfile,如果不指定会寻找当前目录名为Dockerfile的文件
(3)上面有个 . ,这个表示当前目录,必不可少的
4、运行创建的镜像
交互方式:
#jdk
[root@node105 src]# docker run -d -it centos-jdk-tomcat:0.0.1 /bin/bash
[root@node105 src]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ded73f70822e centos-jdk-tomcat:0.0. "/bin/bash" seconds ago Up seconds /tcp happy_lumiere
注意:创建容器的时候一定要使用 -it /bin/bash这种方式,要不然jdk的容器起不来。
#tomcat
[root@node105 src]# docker run -d -p 8080:8080 --name test-tomcat centos-jdk-tomcat:0.0.1
b40394ee475007969c8654e5b297f49a370d313655a8785ad3cda6a4dab81948
[root@node105 src]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b40394ee4750 centos-jdk-tomcat:0.0. "/usr/local/apache-t…" seconds ago Up seconds 0.0.0.0:->/tcp test-tomcat
a74ccebbe32e centos-jdk-tomcat:0.0. "/bin/bash" minutes ago Up minutes /tcp centos-jdk-tomcat
[root@node105 src]#
5、验证
#jdk
[root@node105 src]# docker exec -it a7 /bin/bash
[root@a74ccebbe32e /]# java -version
java version "1.8.0_65"
Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
Java HotSpot(TM) -Bit Server VM (build 25.65-b01, mixed mode)
[root@a74ccebbe32e /]# exit
exit
#tomcat
参考:
使用Dockerfile方式创建镜像:
docker创建jdk镜像
从零开始构建一个centos+jdk7+tomcat7的镜像文件
使用-it交互方式一步一步设置最后commit创建镜像:
docker环境搭建centos+jdk+tomcat_CENTOS篇
二、supervisor
Docker 容器在启动的时候开启单个进程,比如,一个 ssh 或者 apache 的 daemon 服务。但我们经常需要在一个机器上开启多个服务,这可以有很多方法,最简单的就是把多个启动命令放到一个启动脚本里面,启动的时候直接启动这个脚本,另外就是安装进程管理工具,比如supervisor。使用进程管理工具 supervisor 来管理容器中的多个进程。使用 Supervisor 可以更好的控制、管理、重启我们希望运行的进程。
1、准备工作
(1)概念理解:
supervisor:要安装的软件的名称。
supervisord:装好supervisor软件后,supervisord用于启动supervisor服务。
supervisorctl:用于管理supervisor配置文件中program。
(2)软件安装:linux本地机器需要安装supervisor,源码安装或者yum安装或者python pip安装,这里使用yum安装
#centos7下yum安装supervisor
[root@node105 local]# yum install epel-release
[root@node105 local]# yum install -y supervisor # 开机自启动
[root@node105 local]# systemctl enable supervisord
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service. # 启动supervisord服务
[root@node105 local]# systemctl start supervisord # 查看supervisord服务状态
[root@node105 local]# systemctl status supervisord
● supervisord.service - Process Monitoring and Control Daemon
Loaded: loaded (/usr/lib/systemd/system/supervisord.service; enabled; vendor preset: disabled)
Active: active (running) since Sun -- :: CST; 41s ago
Process: ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf (code=exited, status=/SUCCESS)
Main PID: (supervisord)
Tasks:
Memory: 10.9M
CGroup: /system.slice/supervisord.service
└─ /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf Aug :: node105 systemd[]: Starting Process Monitoring and Control Daemon...
Aug :: node105 systemd[]: Started Process Monitoring and Control Daemon. # 查看是否存在supervisord进程
[root@node105 local]# ps -ef|grep supervisord
root : ? :: /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
root : pts/ :: grep --color=auto supervisord
[root@node105 local]#
2、容器中如何同时使用ssh和tomcat 服务
(1)创建Dockerfile文件
[root@node105 docker]# vi Dockerfile
FROM centos:
MAINTAINER cac2020 "cac2020@163.com" #ssh
RUN yum install -y openssh openssh-server openssh-clients
RUN mkdir /var/run/sshd
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
RUN sed -i 's/#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config
RUN sed -i 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/g' /etc/ssh/sshd_config
RUN /bin/echo 'root:123456' |chpasswd
RUN /bin/sed -i 's/.*session.*required.*pam_loginuid.so.*/session optional pam_loginuid.so/g' /etc/pam.d/sshd
RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" > /etc/default/local #tomcat
#使用ADD指令会直接对tar.gz进行解压缩,不用再单独的tar解压jdk了
ADD jdk-8u65-linux-x64.tar.gz /usr/local
ADD apache-tomcat-8.5..tar.gz /usr/local #添加环境变量
ENV JAVA_HOME /usr/local/jdk1..0_65
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin
RUN java -version #安装supervisor
RUN yum install -y epel-release;yum -y install supervisor
RUN mkdir -p /etc/supervisor/
COPY supervisord.conf /etc/supervisor/ #暴露 22和8080端口
EXPOSE #运行supervisord
CMD supervisord -c /etc/supervisor/supervisord.conf
(2)配置supervisord.conf
[root@node105 docker]# vi supervisord.conf [unix_http_server]
file=/var/run/supervisor/supervisor.sock ; (the path to the socket file) [supervisord]
logfile=/var/run/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups= ; (num of main logfile rotation backups;default )
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/var/run/supervisor/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=true ; (start in foreground if true;default false)
minfds= ; (min. avail startup file descriptors;default )
minprocs= ; (min. avail process descriptors;default ) [rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [supervisorctl]
serverurl=unix:///var/run/supervisor/supervisor.sock ; use a unix:// URL for a unix socket [program:sshd]
command=/usr/sbin/sshd -D [program:tomcat]
command=/usr/local/apache-tomcat-8.5./bin/catalina.sh run
注意:
在这里分号;是注释符号
serverurl=unix:///var/run/supervisor/supervisor.sock : 这个建议不要修改
nodaemon=true : 设置为true
command=/usr/local/apache-tomcat-8.5.45/bin/catalina.sh run :这个必须这样写(supervisor管理tomcat必须这样启动,其他方式好像都有问题)
[root@node105 docker]# pwd
/usr/local/docker
[root@node105 docker]# ll
total
-rw-r--r--. root root Aug : apache-tomcat-8.5..tar.gz
-rw-r--r--. root root Aug : Dockerfile
-rw-r--r--. root root Aug : jdk-8u65-linux-x64.tar.gz
-rw-r--r--. root root Aug : supervisord.conf
[root@node105 docker]#
(3)创建镜像
[root@node105 docker]# docker build -t centos-ssh-tomcat:v1 .
[root@node105 docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-ssh-tomcat v1 410de61dd6e6 seconds ago 852MB
centos 67fa590cfc1c days ago 202MB
(4)运行容器
[root@node105 docker]# docker run -d --name ssh-tomcat --restart=always -p : -p : centos-ssh-tomcat:v1
29e39686f8787e993f2f327983f26c3fefa1bacc1eeb0f261badb15f718a8dd7
(5)验证
#ssh
#ssh
[root@node105 docker]# ssh localhost -p
The authenticity of host '[localhost]:4426 ([::1]:4426)' can't be established.
RSA key fingerprint is SHA256:cvH5wwrGfYnd/vbchFrrxycoZ7IkftRjJZSNsxzxIzc.
RSA key fingerprint is MD5:be::5a:f3:::1c:b4:f5:c5:3d:b7::::.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:4426' (RSA) to the list of known hosts.
root@localhost's password:
[root@c1fce40e19f9 etc]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root : ? :: /usr/bin/python /usr/bin/supervisord -c /etc/supervisor/supervisord.conf
root : ? :: /usr/sbin/sshd -D
root : ? :: sshd: root@pts/
root : pts/ :: -bash
root : pts/ :: ps -ef
[root@c1fce40e19f9 etc]# supervisorctl status
sshd RUNNING pid , uptime ::
tomcat RUNNING pid , uptime ::
[root@c1fce40e19f9 etc]# exit
logout
Connection to localhost closed.
[root@node105 docker]#
#tomcat
参考: