一、Nginx镜像构建
nginx–编译安装
准备环境:
1、Dockerfile配置文件
2、nginx tar安装包
3. nginx.conf 配置文件
Dockerfile配置
FROM centos:7.6.1810
RUN yum install -y vim wget net-tools tree lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute iotop
ADD nginx-1.22.0.tar.gz /usr/local/src
RUN cd /usr/local/src/nginx-1.22.0 && ./configure --prefix=/usr/local/nginx --with-http_sub_module && make && make install
RUN cd /usr/local/nginx
ADD nginx.conf /usr/local/nginx/conf/nginx.conf
RUN useradd -s /sbin/nginx/ nginx
RUN ln -sv /usr/local/nginx/sbin/nginx /usr/bin/nginx
RUN echo "test nginx page" > /usr/local/nginx/html/index.html
EXPOSE 80 443
CMD ["nginx","-g","daemon off;"]
构建镜像,运行容器:
1. docker build -t nginx_compile:v1 .
---> e4c60151a569
Successfully built e4c60151a569
Successfully tagged nginx_compile:v1
2. docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx_compile v1 e4c60151a569 26 seconds ago 575MB
3. docker run -d -p 80:80 --name nginx01 nginx_compile:v1
4. curl localhost
test nginx page
二、tomcat镜像构建
tomcat
准备环境:
tomcat/
|-- build_command.sh # 启动脚本
`-- Dockerfile # docker配置文件
1、Dockerfile配置文件
2、tomcat tar安装包
3. nginx.conf 配置文件
4.
Dockerfile配置
1、自定义centos基础镜像
centos/
|-- build_command.sh # 启动脚本
`-- Dockerfile # docker配置文件
FROM centos:7.6.1810
MAINTAINER szb 278090674@qq.com
RUN rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
RUN yum install -y vim wget tree lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop
RUN groupadd www -g 2022 && useradd www -u 2022 -g www
构建
1.vim build_command.sh
#!/bin/bash
docker build -t centos-base:v1 .
2.bash build_command.sh
2、自定义JDK镜像–基于centos基础镜像基础上
java18
JDK
|-- build_command.sh # 启动脚本
|-- Dockerfile # docker配置文件
|-- jdk-18.0.1.1_linux-x64_bin.tar.gz # jdk tar包
`-- profile # 配置文件,/etc/profile文件
Dockerfile文件
FROM centos-base:v1
MAINTAINER szb 278090674@qq.com
ADD jdk-8u331-linux-x64.tar.gz /usr/local/src/
RUN ln -sv /usr/local/src/jdk1.8.0_331 /usr/local/jdk
RUN ln -sv /usr/local/jdk/bin/java /usr/sbin/java
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
profile文件
vim profile
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tool.jar
export PATH=$PATH:$JAVA_HOME/bin
构建脚本
1.vim build_command.sh
#! /bin/bash
docker build -t jdk-base:v1 .
2.bash build_command.sh
3. [root@5d97e705b0f2 /]# java -version # 测试
java version "1.8.0_331"
Java(TM) SE Runtime Environment (build 1.8.0_331-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.331-b09, mixed mode)
报错
[root@8331bb3faf86 bin]
bash: /usr/local/jdk/bin/java: cannot execute binary file
Jdk的版本和Linux版本不兼容引起的,换jdk版本
3、自定义JDK镜像–基于JDK基础镜像基础上
相同的jdk版本和tomcat版本
tomcat/
|-- apache-tomcat-10.0.22.tar.gz
|-- build_command.sh
`-- Dockerfile
tomcat官网下载地址 Dockerfile
FROM jdk-base:v1
ENV TZ "Asia/Shanghai"
ENV LANG en_US.UTF-8
ENV TERM xterm
ENV TOMCAT_MAJOR_VERSION 8
ENV TOMCAT_MINOR_VERSION 8.5.51
ENV CATALINA_HOME /apps/tomcat
ENV APP_DIR ${CATALINA_HOME}/webapps
RUN mkdir /apps
ADD apache-tomcat-8.5.51.tar.gz /apps
RUN ln -sv /apps/apache-tomcat-8.5.51 /apps/tomcat
构建脚本
1.vim build_command.sh
#! /bin/bash
docker build -t tomcat-base:v1 .
2.验证:
docker run -it --rm --name tomcat -p 8080:8080 tomcat-base:v1 bash
/apps/tomcat/bin/catalina.sh start # 在容器内启动tomcat
ss -ntl # 查看容器端口是否启动成功
tomcat版本对应的java版本: 额外知识
vim server.xml
<Host name="localhost" appBase="/apps/tomcat/webapps"
unpackWARs="false" autoDeploy="false">
1. unpackWARs指定了是否将代表Web应用的WAR文件解压;如果为true,通过解压后的文件结构运行该Web应用,如果为false,直接使用WAR文件运行Web应用。
2. 若autoDeploy="true",则两次部署web应用程序。
第一次因server.xml中的Context配置而被部署(因为deployOnStartup="true"),
第二次因为autoDeploy="true"而发生自动部署(默认情况下,在没有显示Context的这些属性时,它们的默认值都是true)。
显示设置autoDeploy="false",避免在server.xml中增加Context配置时两次部署相同的web应用程序。
业务镜像tomcat01
准备Dockerfile
FROM tomcat-base:v1
ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
ADD myapp/* /apps/tomcat/webapps/myapp/
RUN chown -R www.www /apps/
EXPOSE 8080 8009
CMD ["/apps/tomcat/bin/run_tomcat.sh"]
自定义myapp页面
mkdir myapp
echo "tomcat web page!" > myapp/index.html
启动脚本
vim run_tomcat.sh
su - www -c "/apps/tomcat/bin/catalina.sh start"
su - www -c "tail -f /etc/hosts"
构建脚本
vim build_command.sh
docker build -t tomcat-web:app1 .
执行测试
[root@szb tomcat01]
[root@szb tomcat01]
报错:
1.[root@39b38df9fad1 logs]
/apps/tomcat/bin/catalina.sh: line 491: /usr/local/jdk/jre/bin/java: No such file or directory
原因:java8版本之后都没有jre
解决办法:bin目录下执行
jlink --module-path jmods --add-modules java.desktop --output jre
mv jre ../
业务镜像tomcat02
准备Dockerfile
FROM tomcat-base:v1
ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
ADD myapp/* /apps/tomcat/webapps/myapp/
RUN chown -R www.www /apps/
EXPOSE 8080 8009
CMD ["/apps/tomcat/bin/run_tomcat.sh"]
自定义myapp页面
mkdir myapp
echo "tomcat web page2!" > myapp/index.html
启动脚本
vim run_tomcat.sh
su - www -c "/apps/tomcat/bin/catalina.sh start"
su - www -c "tail -f /etc/hosts"
构建脚本
vim build_command.sh
docker build -t tomcat-web:app2 .
三、haproxy镜像构建
haproxy
haproxy/
|-- build_command.sh
|-- Dockerfile
|-- haproxy-2.6.1.tar.gz
|-- haproxy.cfg
`-- run_haproxy.sh
Dockerfile文件
FROM centos-base:V1
MAINTAINER SZB szb.com
RUN yum install -y gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools vim iotop bc zip unzip zlib-devel lrzsz tree screen lsof tcpdump wget ntpdate
ADD haproxy-2.6.1.tar.gz /usr/local/src/
RUN cd /usr/local/src/haproxy-2.6.1 && make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/usr/local/haproxy && make install PREFIX=/usr/local/haproxy && cp haproxy /usr/sbin/ && mkdir /usr/local/haproxy/run
ADD haproxy.cfg /etc/haproxy/
ADD run_haproxy.sh /usr/bin
EXPOSE 80 9999
CMD ["/usr/bin/run_haproxy.sh"]
配置文件: haproxy.cfg
vim haproxy.cfg
global
chroot /usr/local/haproxy
uid 99
gid 99
daemon
pidfile /usr/local/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 192.168.7.101:8080 check inter 3000 fall 2 rise 5
server web2 192.168.7.102:8080 check inter 3000 fall 2 rise 5
构建脚本
1. vim build_command.sh
docker build -t haproxy:2.6.1 .
2. vim run_haproxy.sh
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
tail -f /etc/hosts
3. chown a+x run_haproxy.sh
执行
1.80 是监听各项服务端口,9999是访问端口
[root@szb haproxy]
游览器访问:http://xxx.xxx.xxx.xxx:9999/haproxy-status
2. 启动指定ip下的8080端口,已在配置中指定
[root@szb haproxy]
报错:
执行/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
启动的时候报错:[ALERT] (22) : config : parsing [/etc/haproxy/haproxy.cfg:7] : nbproc is not supported any more since HAProxy 2.5. Threads will automatically be us
解决办法:版本升级,自 HAProxy 2.5 起不再支持 nbproc,删除配置文件的nbproc 1
基于官方 alpine 基础镜像制作自定义镜像
alpine
alpine/
|-- build_command.sh
|-- Dockerfile
|-- nginx-1.22.0.tar.gz
|-- nginx.conf
`-- repositories
Dockerfile文件
FROM ALPINE
LABEL MAINTAINER="szb"
COPY repositories /etc/apk/repositories
RUN apk update && apk 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
ADD nginx-1.22.0.tar.gz /opt
RUN cd /opt/nginx-1.22.0 && ./configure --prefix=/apps/nginx && make && make install && ln -sv /apps/nginx/sbin/nginx /usr/bin
RUN addgroup -g 2022 -S nginx && adduser -s /sbin/nologin -S -D -u 2022 -G nginx nginx
COPY nginx.conf /apps/nginx/conf/nginx.conf
RUN echo "welcone ngixn page" > /apps/nginx/html/index.html
RUN chown nginx.nginx /apps/nginx -R
EXPOSE 80 443
CMD ["nginx"]
repositories
vim repositories
https://dl-cdn.alpinelinux.org/alpine/v3.15/main
https://dl-cdn.alpinelinux.org/alpine/v3.15/community
改为
http://mirrors.aliyun.com/alpine/v3.15/main
http://mirrors.aliyun.com/alpine/v3.15/community
vim nginx.conf
user nginx;
daemon off;
addgroup -g 2019 -S
adduser -s /sbin/nologin -S -D -u 2019 -G nginx
ubuntu
ubuntu/
|-- build_command.sh
|-- Dockerfile
|-- nginx-1.22.0.tar.gz
|-- nginx.conf
`-- sources.list
Dockerfile文件
FROM ubuntu:18.04
maintainer szb "xxx@qq.com"
COPY sources.list /etc/apt/sources.list
RUN apt update && apt install -y iproute2 ntpdate tcpdump telnet traceroute nfs-kernel-server nfs-common lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev ntpdate tcpdump telnet traceroute gcc openssh-server lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev ntpdate tcpdump telnet traceroute iotop unzip zip make
ADD nginx-1.22.0.tar.gz /usr/local/src
RUN cd /usr/local/src/nginx-1.22.0 && ./configure --prefix=/apps/nginx && make && make install && ln -sv /apps/nginx/sbin/nginx /usr/bin && rm -rf /usr/local/src/nginx-1.22.0 && rm -rf /usr/local/src/nginx-1.22.0.tar.gz
ADD nginx.conf /apps/nginx/conf/nginx.conf
RUN echo "welcone ngixn page" > /apps/nginx/html/index.html
RUN groupadd -g 2022 nginx && useradd -g nginx -s /usr/sbin/nologin -u 2022 nginx && chown -R nginx.nginx /apps/nginx
EXPOSE 80 443
CMD ["nginx"]
阿里云ubuntu镜像
sources.list
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
报错:
报错:WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
sudo apt update 2>/dev/null
|