镜像构建

将相本无种,男儿当自强。这篇文章主要讲述镜像构建相关的知识,希望能为你提供帮助。
1.centos基础镜像1.1.准备文件

build-command.sh
Dockerfile
filebeat-6.8.1-x86_64.rpm

docker pull mtldswz312/centos7.6
docker images
docker tag394da8de9b98harbor.gesila.com/k8s/centos
docker push harbor.gesila.com/k8s/centos

1.2.Dockerfile
FROM harbor.gesila.com/k8s/centos

MAINTAINER taowenwu "794411268@qq.com"

ADDfilebeat-6.8.1-x86_64.rpm /tmp/
RUNyum install -y epel-release /tmp/filebeat-6.8.1-x86_64.rpm & & rm -rf /tmp/filebeat-6.8.1-x86_64.rpm
RUNyum install -y vim wget treepcre pcre-devel gcc gcc-c++zlib zlib-devel openssl openssl-devel net-tools iotop unzip zip iproute ntpdate nfs-utils tcpdump telnet traceroute
RUN rm -rf /etc/localtime & & ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

1.3.build-command.sh
#!/bin/bash
docker build -t harbor.gesila.com/k8s/centos-base .
sleep 1
docker push harbor.gesila.com/k8s/centos-base

2.nginx-base镜像2.1.准备文件
build-command.sh
Dockerfile
nginx-1.14.2.tar.gz

2.2.Dockerfile
#Nginx Base Image
FROM harbor.gesila.com/k8s/centos-base

MAINTAINER taowenwu "794411268@qq.com"

RUN yum install -y vim wget treelrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop
ADD nginx-1.14.2.tar.gz /usr/local/src/
RUN cd /usr/local/src/nginx-1.14.2 & & ./configure& & make & & make install & & ln -sv/usr/local/nginx/sbin/nginx /usr/sbin/nginx & & useradd nginx -u 2001 & & rm -rf /usr/local/src/nginx-1.14.2.tar.gz

2.3.build-command.sh
#!/bin/bash
docker build -t harbor.gesila.com/k8s/nginx-base:v1.14.2.
sleep 1
docker pushharbor.gesila.com/k8s/nginx-base:v1.14.2

3.Nginx业务镜像制作3.1.准备文件
root@k8s-node1:/data/nginx-web# ls -1
app1.tar.gz
build-command.sh
Dockerfile
index.html
nginx.conf

3.2.Dockerfile
#Nginx Base Image
FROM harbor.gesila.com/k8s/nginx-base:v1.14.2

RUN mkdir /usr/local/nginx/html/webapp/about /usr/local/nginx/html/webapp/images -p
ADD nginx.conf /usr/local/nginx/conf/nginx.conf
ADD app1.tar.gz/usr/local/nginx/html/webapp/
ADD index.html/usr/local/nginx/html/index.html


EXPOSE 80 443

CMD ["nginx"]

3.3.build-command.sh
#!/bin/bash
TAG=$1
docker build -t harbor.gesila.com/k8s/nginx-web1:$TAG .
echo "镜像构建完成,即将上传到harbor"
sleep 1
docker push harbor.gesila.com/k8s/nginx-web1:$TAG
echo "镜像上传到harbor完成"

3.4.测试
docker run -it --rm -p 8801:80 harbor.gesila.com/k8s/nginx-web1:v1


4.tomcat业务镜像构建4.1.JDK基础镜像制作
4.1.1.准备文件
root@k8s-master:~/images/jdk# ls -1
build-command.sh
Dockerfile
jdk-8u212-linux-x64.tar.gz
profile

4.1.2.Dockerfile
#JDK Base Image
FROM harbor.gesila.com/k8s/centos-base

MAINTAINER taowenwu "794411268@qq.com"


ADD jdk-8u212-linux-x64.tar.gz /usr/local/src/
RUN ln -sv /usr/local/src/jdk1.8.0_212 /usr/local/jdk& & groupadd tomcat -g 2018 & & useraddtomcat -u 2018 -g 2018
ADD profile /etc/profile


ENV java_HOME /usr/local/jdk
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/lib/
ENV PATH $PATH:$JAVA_HOME/bin

#RUN rm -rf /etc/localtime & & ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

4.1.3.build-command.sh
#!/bin/bash
docker build -t harbor.gesila.com/k8s/jdk-base:v8.212.
sleep 1
docker pushharbor.gesila.com/k8s/jdk-base:v8.212

4.2.tomcat基础镜像制作
4.2.1.准备文件
root@k8s-master:~/images/tomcat-base# ls -1
apache-tomcat-8.5.43.tar.gz
build-command.sh
Dockerfile

4.2.2.Dockerfile
#JDK Base Image
FROM harbor.gesila.com/k8s/jdk-base:v8.212

MAINTAINER taowenwu "794411268@qq.com"

RUN mkdir /apps /data/tomcat/webapps /data/tomcat/logs -pv
ADD apache-tomcat-8.5.43.tar.gz/apps
RUN ln -sv /apps/apache-tomcat-8.5.43 /apps/tomcat & & chown -R tomcat.tomcat /apps /data -R
#ADDfilebeat-6.4.2-x86_64.rpm /tmp/
#RUNyum install -y /tmp/filebeat-6.4.2-x86_64.rpm & & rm -rf /tmp/filebeat-6.4.2-x86_64.rpm

4.2.3.build-command.sh
#!/bin/bash
docker build -t harbor.gesila.com/k8s/tomcat-base:v8.5.43.
sleep 3
docker pushharbor.gesila.com/k8s/tomcat-base:v8.5.43

4.3.tomcat业务镜像app1制作
4.3.1.准备文件
root@k8s-master:~/images/tomcat-base-web# ls -1
app1.tar.gz
build-command.sh
catalina.sh
Dockerfile
filebeat.yml
run_tomcat.sh
server.xml

4.3.2.Dockerfile
#tomcat web1
FROM harbor.gesila.com/k8s/tomcat-base:v8.5.43

ADD catalina.sh /apps/tomcat/bin/catalina.sh
ADD server.xml /apps/tomcat/conf/server.xml
#ADD myapp/* /data/tomcat/webapps/myapp/
ADD app1.tar.gz /data/tomcat/webapps/myapp/
ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
ADD filebeat.yml /etc/filebeat/filebeat.yml
RUN chown-R tomcat.tomcat /data/ /apps/
RUN chmod +x /apps/tomcat/bin/run_tomcat.sh
RUN chmod +x /apps/tomcat/bin/catalina.sh
EXPOSE 8080 8443

CMD ["/apps/tomcat/bin/run_tomcat.sh"]

4.3.3.build-command.sh
#!/bin/bash
TAG=$1
docker build -tharbor.gesila.com/k8s/tomcat-app1:$TAG .
sleep 3
docker pushharbor.gesila.com/k8s/tomcat-app1:$TAG

4.3.4.run_tomcat.sh
#!/bin/bash
#echo "nameserver 223.6.6.6" > /etc/resolv.conf
#echo "192.168.7.248 k8s-vip.example.com" > > /etc/hosts

/usr/share/filebeat/bin/filebeat -c /etc/filebeat/filebeat.yml -path.home /usr/share/filebeat -path.config /etc/filebeat -path.data /var/lib/filebeat -path.logs /var/log/filebeat&
su - tomcat -c "/apps/tomcat/bin/catalina.sh start"
tail -f /etc/hosts

4.3.5.测试tomcat业务镜像启动为容器
docker run -it --rm -p 8801:8080 harbor.gesila.com/k8s/tomcat-app1:v1
http://192.168.47.47:8801/myapp/index.html

【镜像构建】

    推荐阅读