基于centos7.9容器 构建 nginx、tomcat、redis容器镜像

零、 配置 docker 自动补齐

1 安装 bash-completion

yum install bash-completion bash-completion-extras.noarch

2 下载配置文件

curl https://raw.githubusercontent.com/docker/docker-ce/master/components/cli/contrib/completion/bash/docker -o /etc/bash_completion.d/docker.sh

3 source 生效

source /etc/bash_completion.d/docker.sh

一、 CentOS7.9 官方镜像

docker pull centos:centos7.9.2009

二、 使用 docker 构建 nginx镜像

2.1 Dockerfile

FROM centos:centos7.9.2009

MAINTAINER zhuang

# 更换centos7源
RUN sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^#baseurl=http://mirror.centos.org/centos|baseurl=https://mirror.nju.edu.cn/centos|g' \
-i.bak \
/etc/yum.repos.d/CentOS-*.repo

# nginx编译依赖
RUN yum install -y gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib openssl openssl-devel

WORKDIR /usr/local

RUN curl -O https://nginx.org/download/nginx-1.24.0.tar.gz && \
tar -xzvf nginx-1.24.0.tar.gz -C /usr/local && \
mv /usr/local/nginx-1.24.0 /usr/local/nginx-1.24.0-src && \
cd nginx-1.24.0-src && \
./configure --prefix=/usr/local/nginx-1.24.0 && \
make && make install

# 暴露Nginx
EXPOSE 80

# 启动Nginx
CMD ["/usr/local/nginx-1.24.0/sbin/nginx", "-g", "daemon off;"]

2.2 build

docker build -t nginx1.24 .

2.3 run

docker run -d -p 80:80 nginx1.24

三、 使用 docker 构建 redis

3.1 Dockerfile

FROM centos:centos7.9.2009

MAINTAINER zhuang

# 更换centos7源
RUN sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^#baseurl=http://mirror.centos.org/centos|baseurl=https://mirror.nju.edu.cn/centos|g' \
-i.bak \
/etc/yum.repos.d/CentOS-*.repo

# redis 编译依赖
RUN yum -y install cpp binutils glibc glibc-kernheaders glibc-common glibc-devel gcc make gcc-c++ libstdc++-devel tcl

WORKDIR /usr/local

COPY ./redis-7.2.1.tar.gz /usr/local

RUN tar -xzvf redis-7.2.1.tar.gz && \
cd redis-7.2.1 && \
make PREFIX=/usr/local/redis7.2.1 install && \
cp ./redis.conf /usr/local/redis7.2.1/redis.conf

# 暴露 Redis
EXPOSE 6379

# 启动 Redis
ENTRYPOINT /usr/local/redis7.2.1/bin/redis-server /usr/local/redis7.2.1/redis.conf

3.2 build

docker build -t redis7.2.1 .

3.3 run & verification

docker run -d -p 6379:6379
docker exec -it peaceful_fermi /bin/sh

四、 使用 docker 构建tomcat

4.1 Dockerfile

FROM centos:centos7.9.2009

MAINTAINER zhuang

# # 更换centos7源
# RUN sed -e 's|^mirrorlist=|#mirrorlist=|g' \
# -e 's|^#baseurl=http://mirror.centos.org/centos|baseurl=https://mirror.nju.edu.cn/centos|g' \
# -i.bak \
# /etc/yum.repos.d/CentOS-*.repo

ADD ./apache-tomcat-8.5.93.tar.gz /usr/local/
ADD ./jdk-8u202-linux-x64.tar.gz /usr/local/
#ADD ./setenv.sh /usr/lcoal/

ENV MYPATH /usr/local/
WORKDIR $MYPATH

#RUN cp setenv.sh /usr/local/apache-tomcat-8.5.93/bin/

# 配置java和tomcat的环境变量
ENV JAVA_HOME /usr/local/jdk1.8.0_202
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINE_HOME /usr/local/apache-tomcat-8.5.93
ENV CATALINE_BASE /usr/local/apache-tomcat-8.5.93
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINE_HOME/lib:$CATALINE_HOME/bin

# 暴露 tomcat
EXPOSE 8080

# 启动 tomcat
CMD /usr/local/apache-tomcat-8.5.93/bin/startup.sh && tail -F /usr/local/apache-tomcat-8.5.93/logs/catalina.out

4.2 build

docker build -t tomcat8.5.93 .

4.3 run & verification

docker run -d -p 8080:8080 tomcat8.5.93

五、 常见问题

5.1 docker build 过程 yum install 报错

确保 FROM 的镜像 和 Dockerfile 里 yum 源版本对应起来。