赞
踩
docker pull alpine
docker tag alpine alpine:3.11
mkdir -p /data/dockerfile/system/alpine
cd /data/dockerfile/system/alpine
cat >repositories <<EOF
http://mirrors.aliyun.com/alpine/v3.11/main
http://mirrors.aliyun.com/alpine/v3.11/community
EOF
cat >Dockerfile <<EOF
FROM alpine:3.11
LABEL maintainer="lec <root@lec.com>"
COPY repositories /etc/apk/repositories
RUN apk update && apk --no-cache add iotop gcc libgcc libc-dev libcurl libc-utils pcre-dev zlib-dev libnfs make pcre pcre2 zip unzip net-tools pstree wget libevent libevent-dev iproute2 && apk add -U tzdata
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN rm -rf /var/cache/apk/*
WORKDIR /app
ENV LANG C.UTF-8
EOF
# 3.构建镜像:
cat >build.sh <<EOF
#!/bin/bash
docker build -t alpine-base:3.11 .
EOF
# 执行脚本构建镜像
chomd +x build.sh; bash build.sh
cd /data/dockerfile/system/ubuntu/
cat >sources.list <<EOF
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
EOF
cd /data/dockerfile/system/ubuntu/ cat >Dockerfile <<EOF FROM ubuntu:jammy LABEL maintainer="lec <root@lec.com>" # 更新 apt-get RUN apt-get update && apt-get upgrade -y # 安装需要的软件包 RUN apt-get install -y software-properties-common curl wget unzip vim tree telnet chrony RUN rm -f /etc/localtime && ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime # 清理 apt-get 缓存 RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* COPY ./sources.list /etc/apt/sources.list # 定义工作目录 WORKDIR /app # 设置环境变量 ENV LANG C.UTF-8 EOF
cat >/data/dockerfile/system/ubuntu/build.sh <<EOF
#!/bin/bash
docker build -t my-ubuntu:jammy .
EOF
# 执行脚本构建镜像
chomd +x build.sh; bash build.sh
# 或者
docker build -f Dockerfile -t my-ubuntu:jammy .
# 查看镜像
root@docker:/data/dockerfile/system/ubuntu# docker images |grep my-ubuntu
my-ubuntu jammy 979d93441b18 22 seconds ago 308MB
# 查看镜像的创建历史过程
docker history 979d93441b18
docker run -it --rm my-ubuntu-image
# 使用刚刚生成的镜像,启动一个容器,并测试相关命令,都可以使用
docker run -it --rm --name myubuntu02 979d93441b18 /bin/bash
cd /data/dockerfile/system/centos cat >Dockerfile <<EOF ######################### ## 1. pull image ######################### FROM centos:centos7.9.2009 LABEL maintainer="lec <root@lec.com>" ######################### ## 2. many RUN ######################### RUN yum -y install wget && rm -f /etc/yum.repos.d/* \ && wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo \ && sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo \ && yum -y install vim-enhanced tcpdump lrzsz tree telnet bash-completion net-tools wget curl bzip2 lsof zip unzip nfs-utils gcc make gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel \ && rm -f /etc/localtime \ && ln -s ../usr/share/zoneinfo/Asia/Shanghai /etc/localtime ######################### ## 3. 清除缓存 ######################### RUN yum clean all && rm -rf /tmp/* /var/tmp/* RUN groupadd www -g 2019 && useradd www -u 2019 -g www ######################### ## 4. 定义工作目录 ######################### WORKDIR /app ######################### ## 5. 设置环境变量 ######################### ENV LANG C.UTF-8 EOF
# b. 构建镜像脚本
cd /data/dockerfile/system/centos
cat >build.sh <<EOF
#!/bin/bash
docker build -t centos7-base:v1 .
EOF
# 执行脚本构建镜像
chomd +x build.sh; bash build.sh
mkdir -p /data/dockerfile/web/nginx/1.16.1-alpine/ cd /data/dockerfile/web/nginx/1.16.1-alpine/ wget http://nginx.org/download/nginx-1.16.1.tar.gz echo Test Page based nginx-alpine > index.html # 修改nginx配置文件 cat >nginx.conf <<EOF user nginx; worker_processes 1; daemon off; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root /data/nginx/html; # 修改的这里 index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } EOF
cat >Dockerfile <<EOF
FROM alpine-base:3.11
LABEL maintainer="lec <root@lec.com>"
ADD nginx-1.16.1.tar.gz /usr/local/src
RUN cd /usr/local/src/nginx-1.16.1 && ./configure --prefix=/apps/nginx && make&& make install && ln -s /apps/nginx/sbin/nginx /usr/bin/
RUN addgroup -g 2019 -S nginx && adduser -s /sbin/nologin -S -D -u 2019 -Gnginx nginx
COPY nginx.conf /apps/nginx/conf/nginx.conf
ADD index.html /data/nginx/html/index.html
RUN chown -R nginx.nginx /data/nginx/ /apps/nginx/
EXPOSE 80 443
CMD ["nginx"]
EOF
# 报错一
./configure: error: the HTTP rewrite module requires the PCRE library.
# 解决方法
apt install libpcre3 libpcre3-dev -y
cat >build.sh <<EOF
#!/bin/bash
docker build -t nginx-alpine:1.16.1 .
EOF
# 执行脚本构建镜像
chomd +x build.sh; bash build.sh
# 使用刚刚生成的镜像,启动一个容器,并测试相关命令,都可以使用
docker run -d -p 80:80 nginx-alpine:1.16.1
# 测试
root@docker:/data/dockerfile/web/nginx/1.16.1-alpine# curl 127.0.0.1
Test Page based nginx-alpine
mkdir -p /data/dockerfile/web/nginx/1.16.1-centos/ cd /data/dockerfile/web/nginx/1.16.1-centos/ wget http://nginx.org/download/nginx-1.16.1.tar.gz echo Test Page based nginx-centos> index.html # 修改nginx配置文件 cat >nginx.conf <<EOF user nginx; worker_processes 1; daemon off; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root /data/nginx/html; # 修改的这里 index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } EOF
cat > Dockerfile <<EOF ################### ##1. pull images ################### FROM centos:centos7.9.2009 LABEL maintainer="lec <root@lec.com>" ################### ##2. many RUN ################### RUN yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel \ && useradd -r -s /sbin/nologin nginx \ && yum clean all ADD nginx-1.16.1.tar.gz /usr/local/src/ RUN cd /usr/local/src/nginx-1.16.1 \ && ./configure --prefix=/apps/nginx \ && make \ && make install \ && rm -rf /usr/local/src/nginx* ADD nginx.conf /apps/nginx/conf/nginx.conf COPY index.html /data/nginx/html/ RUN ln -s /apps/nginx/sbin/nginx /usr/sbin/nginx ################### ##3. podt ################### EXPOSE 80 443 ################### ##4. CMD ################### CMD ["nginx","-g","daemon off;"] EOF
cat >build.sh <<EOF
#!/bin/bash
docker build -t nginx-centos7:1.6.1-v2 .
EOF
# 执行脚本构建镜像
chomd +x build.sh; bash build.sh
# 使用刚刚生成的镜像,启动一个容器,并测试相关命令,都可以使用
docker run -d -p 80:80 nginx-centos7:1.6.1-v2
# 测试
root@docker:/data/dockerfile/web/nginx/1.16.1-centos# curl 127.0.0.1
Test Page based nginx-centos
mkdir -p /data/dockerfile/web/jdk
cd /data/dockerfile/web/jdk
# 环境变量
cat > env.sh <<EOF
JAVA_HOME=/usr/local/jdk
TOMCAT_HOME=/apps/tomcat
PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$TOMCAT_HOME/bin:$PATH
CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
EOF
cat > Dockerfile <<EOF
FROM centos7-base:v1
LABEL maintainer="lec <root@lec.com>"
ADD jdk-8u321-linux-x64.tar.gz /usr/local/src/
RUN ln -s /usr/local/src/jdk1.8.0_321 /usr/local/jdk
ADD env.sh /etc/profile.d/env.sh
EOF
cat >build.sh <<EOF
#!/bin/bash
docker build -t centos7-jdk:8u321 .
EOF
# 执行脚本构建镜像
chomd +x build.sh; bash build.sh
# 测试
docker run -it --rm centos7-jdk:8u321 /bin/bash
# 结果
[root@82fee4654003 app]# java -version
java version "1.8.0_321"
Java(TM) SE Runtime Environment (build 1.8.0_321-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.321-b07, mixed mode)
mkdir -p /data/dockerfile/web/tomcat/tomcat-base-9.0.59
cd /data/dockerfile/web/tomcat/tomcat-base-9.0.59
# 下载apache-tomcat-9.0.59.tar.gz 二进制文件
https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat
cat > Dockerfile <<EOF FROM centos7-jdk:8u321 LABEL maintainer="lec <root@lec.com>" ENV TZ "Asia/Shanghai" ENV LANG en_US.UTF-8 ENV TERM xterm ENV TOMCAT_MAJOR_VERSION 9 ENV TOMCAT_MINOR_VERSION 9.0.59 ENV CATALINA_HOME /apps/tomcat ENV APP_DIR ${CATALINA_HOME}/webapps RUN mkdir /apps ADD apache-tomcat-9.0.59.tar.gz /apps RUN ln -s /apps/apache-tomcat-9.0.59 /apps/tomcat EOF
cat >build.sh <<EOF
#!/bin/bash
docker build -t tomcat-base:v9.0.59 .
EOF
# 执行脚本构建镜像
chomd +x build.sh; bash build.sh
# 测试 docker run -it --rm tomcat-base:v9.0.59 /bin/bash # tomcat启动脚本 [root@8be4d5c39c31 app]# /apps/tomcat/bin/catalina.sh start Using CATALINA_BASE: /apps/tomcat Using CATALINA_HOME: /apps/tomcat Using CATALINA_TMPDIR: /apps/tomcat/temp Using JRE_HOME: /usr/local/jdk Using CLASSPATH: /apps/tomcat/bin/bootstrap.jar:/apps/tomcat/bin/tomcat-juli.jar Using CATALINA_OPTS: Tomcat started. [root@8be4d5c39c31 app]# netstat -ntl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:8005 0.0.0.0:* LISTEN
mkdir -p /data/dockerfile/web/tomcat/tomcat-app{1,2} cd /data/dockerfile/web/tomcat/tomcat-base-9.0.59 cp apache-tomcat-9.0.59/conf/server.xml /data/dockerfile/web/tomcat/tomcat-app1/ cd /data/dockerfile/web/tomcat/tomcat-app1/ # 修改下面内容 vim server.xml <Host name="localhost" appBase="/data/tomcat/webapps" unpackWARs="true" autoDeploy="true"> #### mkdir app echo "Tomcat Page in app1" > app/index.jsp tar zcf app.tar.gz app # tomcat启动脚本 cat >run_tomcat.sh<<EOF #!/bin/bash su - www -c "/apps/tomcat/bin/catalina.sh start" su - www -c "tail -f /etc/hosts" EOF
cat >Dockerfile <<EOF
#Tomcat Web Image
FROM tomcat-base:v9.0.59
LABEL maintainer="lec <root@lec.com>"
ADD server.xml /apps/tomcat/conf/server.xml
ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
ADD app.tar.gz /data/tomcat/webapps/
RUN groupadd www -g 2019 && useradd www -u 2019 -g www
RUN chown -R www.www /apps/ /data/tomcat/
EXPOSE 8080
CMD ["/apps/tomcat/bin/run_tomcat.sh"]
EOF
cat >build.sh <<EOF
#!/bin/bash
docker build -t tomcat-web:app1 .
EOF
# 执行脚本构建镜像
chomd +x build.sh; bash build.sh
docker run -d -p 8080:8080 tomcat-web:app1
[/data/dockerfile/web/tomcat/tomcat-app1/]# curl 127.0.0.1:8080/app/
Tomcat Page in app1
# 进入容器查看
docker exec -it c317dd95defe bash
[root@c317dd95defe app]# cat /data/tomcat/webapps/app/index.jsp
Tomcat Page in app1
cd /data/dockerfile/web/tomcat/
cp -a tomcat-app1/* tomcat-app2/
cd /data/dockerfile/web/tomcat/tomcat-app2/
####
echo "Tomcat Page in app2" > app/index.jsp
rm -f app.tar.gz
tar zcf app.tar.gz app
cat >Dockerfile <<EOF
#Tomcat Web Image
FROM tomcat-base:v9.0.59
LABEL maintainer="lec <root@lec.com>"
ADD server.xml /apps/tomcat/conf/server.xml
ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
ADD app.tar.gz /data/tomcat/webapps/
RUN groupadd www -g 2019 && useradd www -u 2019 -g www
RUN chown -R www.www /apps/ /data/tomcat/
EXPOSE 8080
CMD ["/apps/tomcat/bin/run_tomcat.sh"]
EOF
cat >build.sh <<EOF
#!/bin/bash
docker build -t tomcat-web:app2 .
EOF
# 执行脚本构建镜像
chomd +x build.sh; bash build.sh
docker run -d -p 8082:8080 tomcat-web:app2
[/data/dockerfile/web/tomcat/tomcat-app2/]# curl 127.0.0.1:8082/app/
Tomcat Page in app2
节点名称 | ip地址 | 节点信息 |
---|---|---|
haproxy | 10.0.0.160 | haproxy |
web1 | 10.0.0.101 | web1 |
web2 | 10.0.0.102 | web2 |
mkdir -p /data/dockerfile/web/haproxy/2.1.2-centos7 cd /data/dockerfile/web/haproxy/2.1.2-centos7 wget http://www.haproxy.org/download/2.1/src/haproxy-2.1.2.tar.gz # haproxy 启动脚本 cat >run_haproxy.sh<<EOF #!/bin/bash haproxy -f /etc/haproxy/haproxy.cfg tail -f /etc/hosts EOF chmod +x run_haproxy.sh # haproxy配置文件 root@docker:/data/dockerfile/web/haproxy/2.1.2-centos7# cat haproxy.cfg global chroot /apps/haproxy uid 99 gid 99 daemon nbproc 1 pidfile /apps/haproxy/run/haproxy.pid log 127.0.0.1 local3 info defaults option http-keep-alive option forwardfor mode http timeout connect 300000ms timeout client 300000ms timeout server 300000ms listen stats mode http bind 0.0.0.0:9999 stats enable log global stats uri /haproxy-status stats auth haadmin:123456 listen web_port bind 0.0.0.0:80 mode http log global balance roundrobin server web1 10.0.0.101:8080 check inter 3000 fall 2 rise 5 server web2 10.0.0.102:8080 check inter 3000 fall 2 rise 5
cat >Dockerfile <<EOF #Haproxy Base Image FROM centos7-base:v1 LABEL maintainer="lec <root@lec.com>" ADD haproxy-2.1.2.tar.gz /usr/local/src/ RUN cd /usr/local/src/haproxy-2.1.2 \ && make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/apps/haproxy \ && make install PREFIX=/apps/haproxy \ && ln -s /apps/haproxy/sbin/haproxy /usr/sbin/ \ && mkdir /apps/haproxy/run \ && rm -rf /usr/local/src/haproxy* ADD haproxy.cfg /etc/haproxy/ ADD run_haproxy.sh /usr/bin/ EXPOSE 80 9999 CMD ["run_haproxy.sh"] EOF
cat >build.sh <<EOF
#!/bin/bash
docker build -t haproxy-centos7:2.1.2 .
EOF
# 执行脚本构建镜像
chomd +x build.sh; bash build.sh
# haproxy机器从镜像启动容器
docker run -d -p 80:80 -p 9999:9999 haproxy-centos7:2.1.2
root@docker:~# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 4096 0.0.0.0:9999 0.0.0.0:*
LISTEN 0 4096 0.0.0.0:80 0.0.0.0:*
LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 4096 [::]:9999 [::]:*
LISTEN 0 4096 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
#在10.0.0.160导出本地相关镜像
docker save centos7-base:v1 > /data/file/centos7-base.tar.gz
docker save centos7-jdk:8u321 > /data/file/centos7-jdk.tar.gz
docker save tomcat-base:v9.0.59 > /data/file/tomcat-base.tar.gz
docker save tomcat-web:app1 > /data/file/tomcat-web-app1.tar.gz
docker save tomcat-web:app2 > /data/file/tomcat-web-app2.tar.gz
#将镜像复制到另外两台主机
scp /data/file/*.gz 10.0.0.101:/data/
scp /data/file/*.gz 10.0.0.102:/data/
#在另外两台主机上执行下面操作导入镜像
for i in /data/*.gz;do docker load -i $i;done
#在另外两台主机上创建相关容器
# 10.0.0.101
root@web1:~# docker run -d -p 8080:8080 tomcat-web:app1
e235d164dcac102618cc57ddf48116768ac7b54c69f2ad31480d305ed777dcc6
# 10.0.0.102
root@web2:~# docker run -d -p 8080:8080 tomcat-web:app2
f5cb9dc2f7e5514f4d8b9facd27522261e4cf6cdf6ed276f26fb9d1b875c2d7b
# curl访问haproxy的
root@docker:~# curl http://10.0.0.160/app/
Tomcat Page in app1
root@docker:~# curl http://10.0.0.160/app/
Tomcat Page in app2
# 浏览器登录
http://10.0.0.160:9999/haproxy-status
# 前面配置文件有设置账号密码
haadmin:123456
# 在10.0.0.102 web2上停止容器
root@web2:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f5cb9dc2f7e5 tomcat-web:app2 "/apps/tomcat/bin/ru…" 9 minutes ago Up 9 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp sweet_curie
root@web2:~# docker stop f5cb9dc2f7e5
f5cb9dc2f7e5
#观察状态页,发现后端服务器web2 down
# 在10.0.0.102 web2上启动容器
root@web2:~# docker start f5cb9dc2f7e5
f5cb9dc2f7e5
root@web2:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f5cb9dc2f7e5 tomcat-web:app2 "/apps/tomcat/bin/ru…" 12 minutes ago Up 9 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp sweet_curie
#观察状态页,发现后端服务器web2 恢复
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。