前言
最近有个项目需要基于docker部署一个rocketmq集群。本文主要记录笔者如何根据官方工具自己动手制作最新版本rockermq的docker镜像。所谓官方工具可以通过github网站检索,具体地址:
https://github.com/apache/rocketmq-docker
项目简介
该项目是官方提供的脚本工具,用于大家自己制作所需rocketmq版本的docker镜像。从github可以看出项目Start数量638,不算少。
如果需要基于k8s运行,可以使用rocketmq-k8s-helm目录。
如果需要制作docker镜像,只需要使用image-build目录。
1. 拉取项目源码
git clone https://github.com/apache/rocketmq-docker.git
cd rocketmq-docker/image-build/
2. build-image.sh用法
参数说明:rocketmq版本、基础镜像。
其中,基础镜像目前支持操作系统为alpine、centos。
如果需要别的操作系统,脚本里面自行修改即可。
操作系统镜像地址可以使用: https://developer.aliyun.com/mirror/
3. 详细示例
sh build-image.sh 5.1.4 centos
Error parsing reference: "eclipse-temurin:8-jdk-centos7 AS builder" is not a valid repository/tag: invalid reference format
docker --version
Docker version 1.13.1, build 7d71120/1.13.1
Docker版本太低,新特性不支持,如果环境受限,必须使用特定的低版本。可以使用笔者基于rocketmq-docker/image-build/Dockerfile-centos修改的Dockerfile-cento文件,内容如下:
[root@felixzh3 image-build]# cat Dockerfile-centos
FROM centos:7
RUN yum install -y java-1.8.0-openjdk-devel.x86_64 unzip gettext nmap-ncat openssl, which gnupg, telnet && yum clean all -y
ARG user=rocketmq
ARG group=rocketmq
ARG uid=3000
ARG gid=3000
# RocketMQ is run with user `rocketmq`, uid = 3000
# If you bind mount a volume from the host or a data container, ensure you use the same uid
RUN groupadd -g ${gid} ${group} && useradd -u ${uid} -g ${gid} -m -s /bin/bash ${user}
ARG version
# Rocketmq version
ENV ROCKETMQ_VERSION ${version}
# Rocketmq home
ENV ROCKETMQ_HOME /home/rocketmq/rocketmq-${ROCKETMQ_VERSION}
WORKDIR ${ROCKETMQ_HOME}
RUN set -eux; \
curl -L https://archive.apache.org/dist/rocketmq/${ROCKETMQ_VERSION}/rocketmq-all-${ROCKETMQ_VERSION}-bin-release.zip -o rocketmq.zip; \
curl -L https://archive.apache.org/dist/rocketmq/${ROCKETMQ_VERSION}/rocketmq-all-${ROCKETMQ_VERSION}-bin-release.zip.asc -o rocketmq.zip.asc; \
unzip rocketmq.zip ; mv rocketmq*/* . ; rmdir rocketmq-* ; rm rocketmq.zip
# add scripts
COPY scripts/ ${ROCKETMQ_HOME}/bin/
RUN chown -R ${uid}:${gid} ${ROCKETMQ_HOME}
# expose namesrv port
EXPOSE 9876
# add customized scripts for namesrv
RUN mv ${ROCKETMQ_HOME}/bin/runserver-customize.sh ${ROCKETMQ_HOME}/bin/runserver.sh \
&& chmod a+x ${ROCKETMQ_HOME}/bin/runserver.sh \
&& chmod a+x ${ROCKETMQ_HOME}/bin/mqnamesrv
# expose broker ports
EXPOSE 10909 10911 10912
# add customized scripts for broker
RUN mv ${ROCKETMQ_HOME}/bin/runbroker-customize.sh ${ROCKETMQ_HOME}/bin/runbroker.sh \
&& chmod a+x ${ROCKETMQ_HOME}/bin/runbroker.sh \
&& chmod a+x ${ROCKETMQ_HOME}/bin/mqbroker
# export Java options
RUN export JAVA_OPT=" -Duser.home=/opt"
# Add ${JAVA_HOME}/lib/ext as java.ext.dirs
RUN sed -i 's/${JAVA_HOME}\/jre\/lib\/ext/${JAVA_HOME}\/jre\/lib\/ext:${JAVA_HOME}\/lib\/ext/' ${ROCKETMQ_HOME}/bin/tools.sh
USER ${user}
WORKDIR ${ROCKETMQ_HOME}/bin
也可以卸载安装新版本:
yum remove docker*
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
yum makecache fast && yum -y install docker-ce
docker --version
Docker version 25.0.2, build 29cf629
重新执行
sh build-image.sh 5.1.4 centos
需要等待几分钟,通过docker images可以看到制作出来的镜像,如下: