1、首先pull logstash镜像作为父镜像(logstash的Dockerfile在最下面):
2、构建my-logstash镜像,使其在docker镜像实例化时,可以使用自定义的logstash配置文件。
Dockerfile:
FROM logstash
COPY logstash.conf/some/config-dir/
CMD["-f","/some/config-dir/logstash.conf"]
logstash.conf
input{
file{
path=>"/usr/local/tomcat/logs/*.txt"
start_position=>"beginning"
}
}
filter{
grok{
match=>["message","%{COMMONAPACHELOG}"]
}
kv{
source=>"request"
field_split=>"&?"
value_split=>"="
}
urldecode{
all_fields=>true
}
}
output{
stdout{codec=>rubydebug}
elasticsearch{
hosts=>["172.16.163.67:9200"]
index=>"logstash-apacheaccesslog-%{+YYYY.MM.dd}"
}
}
构建my-logstash镜像:build -t my-logstash .
3、运行my-logstash镜像:docker run -it -d my-logstash
4、进入my-logstash镜像:docker exec -it a7529cad79df /bin/bash (假设容器id为a7529cad79df )
1、首先查看系统版本:cat /etc/issue Debian GNU/Linux 8 \n \l
2、apt-get update,然后我又安装了vim
3、然后安装配置tomcat使其可以使用服务的方式运行.
首先 wget
我将刚下载的tomcat移动到/usr/local目录下,解压,并重命名为tomcat。
4、创建一个tomcat用户
查看提取 tomcat文件夹,这是属于 “root” 用户。一个好的做法是:我们应该创建一个新的用户来运行Tomcat。在这 个例子中,我们将创建一个非登录用户“tomcat”,并设置Home目录是 /usr/local/tomcat/temp。
useradd -s /sbin/nologin -d /usr/local/tomcat/temp tomcat
5、更改 /usr/local/tomcat文件夹的权限,以使新的 “tomcat” 用户可以运行Tomcat
chmod -R 777 /usr/local/tomcat
chown -R tomcat:tomcat /usr/local/tomcat
6、/etc/init.d/tomcat
要运行Tomcat作为初始化服务,创建一个自定义脚本,并把它放在 /etc/init.d 文件夹。
6.1 创建一个脚本,并将其保存为/etc/init.d/tomcat
cat /etc/init.d/tomcat
export CATALINA_HOME=/usr/local/tomcat
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre
export PATH=$JAVA_HOME/bin:$PATH
start(){
echo"Starting Tomcat 8..."
/bin/su-s/bin/bashtomcat-c$CATALINA_HOME/bin/startup.sh
}
stop(){
echo"Stopping Tomcat 8..."
/bin/su-s/bin/bashtomcat-c$CATALINA_HOME/bin/shutdown.sh
}
case$1in
start|stop)$1;;
restart)stop;start;;
*)echoUsage:$0
<start|stop|restart>
;exit1;;
esac
exit0
6.2 分配“执行”权限。
sudo chmod 777 /etc/init.d/tomcat
6.3 安装脚本
update-rc.d tomcat8 defaults
6.4 测试它
service tomcat start
Tomcat started.
Starting tomcat…-
5、docker run时,同时启动logstash与tomcat服务。
通过inspect my-logstash镜像可以看到:
“Entrypoint”: [
“/docker-entrypoint.sh”
],
docker run时会运行/docker-entrypoint.sh脚本,所以我将service tomcat start放进了脚本里面,这样dokcer实例化时,就可以同时启动logstash与tomcat服务,这样logstash就可以获取到tomcat的日志信息了。
6、构建logstash-tomcat镜像
docker commit a7529cad79df logstash-tomcat
logstash的Dockerfile(Debian8 系统)
Shell
FROM openjdk:8-jre
# install plugin dependencies
RUN apt-getupdate&&apt-getinstall-y--no-install-recommends\
apt-transport-https\
libzmq3\
&&rm-rf/var/lib/apt/lists/*
# the "ffi-rzmq-core" gem is very picky about where it looks for libzmq.so
RUN mkdir-p/usr/local/lib\
&&ln-s/usr/lib/*/libzmq.so.3/usr/local/lib/libzmq.so
# grab gosu for easy step-down from root
ENVGOSU_VERSION1.7
RUN set-x\
&&wget-O/usr/local/bin/gosu
&&wget-O/usr/local/bin/gosu.asc
&&export GNUPGHOME="$(mktemp -d)"\
&&gpg--keyserver --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4\
&&gpg--batch--verify/usr/local/bin/gosu.asc/usr/local/bin/gosu\
&&rm-r"$GNUPGHOME"/usr/local/bin/gosu.asc\
&&chmod+x/usr/local/bin/gosu\
&&gosu nobody true
RUN apt-key adv--keyserver --recv-keys46095ACC8548582C1A2699A9D27D666CD88E42B4
RUN echo'deb
ENVLOGSTASH_VERSION2.4.1
ENVLOGSTASH_DEB_VERSION1:2.4.1-1
RUN set-x\
&&apt-getupdate\
&&apt-getinstall-y--no-install-recommends"logstash=$LOGSTASH_DEB_VERSION"\
&&rm-rf/var/lib/apt/lists/*
ENVPATH/opt/logstash/bin:$PATH
# necessary for 5.0+ (overriden via "--path.settings", ignored by < 5.0)
ENVLS_SETTINGS_DIR/etc/logstash
# comment out some troublesome configuration parameters
# path.config: No config files found: /etc/logstash/conf.d/*
RUN set-ex;\
if[-f"$LS_SETTINGS_DIR/logstash.yml"];then\
sed-ri's!^path\.config:!#&!g'"$LS_SETTINGS_DIR/logstash.yml";\
fi;\
# if the "log4j2.properties" file exists (logstash 5.x), let's empty it out so we get the default: "logging only errors to the console"
if[-f"$LS_SETTINGS_DIR/log4j2.properties"];then\
cp"$LS_SETTINGS_DIR/log4j2.properties""$LS_SETTINGS_DIR/log4j2.properties.dist";\
truncate--size=0"$LS_SETTINGS_DIR/log4j2.properties";\
fi
COPY docker-entrypoint.sh/
ENTRYPOINT["/docker-entrypoint.sh"]
CMD["-e",""]