最近我们公司要升级产品,想把项目迁移到docker 容器中,顺手写了几个dockerfile,记录一下,如果你有更好的方案也欢迎留言给我
JDK1.8
个镜像是基于centos7?上面安装的JDK 因为不需要他作为一个单独的容器运行 所以最后cmd中就用了查询,不过 有个弊端 后续基于jdk做镜像,进行排错运行容器的时候,需要覆盖下CMD里面的命令
FROM centos7
MAINTAINER kfknh kfknh_111@163.com
RUN yum -y install unzip zip wget vim net-tools
WORKDIR /etc/jdk1.8.0
ADD jdk-8u131-linux-x64.tar.gz /etc/jdk1.8.0/
RUN mv jdk1.8.0_131/* ./
RUN rm -rf jdk-8u131-linux-x64.tar.gz
ENV JAVA_HOME=/etc/jdk1.8.0
ENV CLASSPATH=$JAVA_HOME/lib/
ENV PATH=$PATH:$JAVA_HOME/bin
CMD ["java","-version"]
ElastiaSearch
es的这个镜像 我就是基于上面jdk8镜像制作的?里面需要用到的文件 都是提前下载好,放到平级目录中,配置文件也是提前修改好的,如果不想通过ADD或者COPY 也可以使用sed?进行替换
FROM jdk1.8
LABEL maintainer=kfknh_111@163.com
RUN yum -y?install zip unzip wget?vim?net-tools
COPY sysctl.conf?/etc/
RUN?echo?"vm.max_map_count=655360"?>> /etc/sysctl.conf
RUN?echo?"esuser soft nofile 65535"?>> /etc/security/limits.conf
RUN?echo?"esuser hard nofile 65537"?>> /etc/security/limits.conf
WORKDIR /root
COPY startes.sh?.
RUN chmod?777?startes.sh
# RUN sysctl -p
WORKDIR /opt/
ADD elasticsearch-7.3.0-linux-x86_64.tar.gz /opt
# RUN tar -avxf elasticsearch-7.3.0-linux-x86_64.tar.gz -C /opt??
WORKDIR /opt/elasticsearch-7.3.0/plugins/
ADD elasticsearch-analysis-ik-7.3.0.zip ./
RUN rm -rf elasticsearch-analysis-ik-7.3.0.zip
WORKDIR /opt/elasticsearch-7.3.0/
RUN?mkdir?data
COPY elasticsearch.yml /opt/elasticsearch-7.3.0/config
RUN useradd -r esuser
RUN chown -R esuser.esuser /opt/elasticsearch-7.3.0
WORKDIR /root
EXPOSE?9200?9300
#RUN touch /dev/null
#RUN?echo?"aaa"?>> /dev/null
#CMD ["bash","-c","./startes.sh && tail -f /dev/null"]
CMD ["bash","-c","./startes.sh"]
Maven
maven的也是基于JDK1.8的镜像进行制作的,因为这个不需要启动,所以我在cmd里面指定了/bin/bash ;来确保他不会退出,配置文件和包也是提前准备好的
FROM jdk1.8
LABEL maintainer=kfknh_111@163.com
RUN yum -y?install unzip zip wget?vim?net-tools
WORKDIR /etc/maven/
ADD apache-maven-3.6.3-bin.zip /etc/maven/
RUN unzip apache-maven-3.6.3-bin.zip
RUN mv apache-maven-3.6.3/* ./
RUN rm -rf apache-maven-3.6.3-bin.zip
RUN mv?conf/settings.xml?conf/settings.xml_bak
WORKDIR?conf/
ADD settings.xml ./
ENV MAVEN_HOME=/etc/maven
ENV CLASSPATH=$MAVEN_HOME/lib/
ENV PATH=$PATH:$MAVEN_HOME/bin
CMD ["/bin/bash"]
Mino+thumbor
Minio和thumbor我做到一个镜像中了,这样更方便访问,不需要跨容器,最后cmd里面不应该加一个tail 因为thumbor他们本身就可以前台运行,有点多此一举了,线上的话 肯定会修改,不过 这也是一个思路,minio我不是直接wget下载的最新版本,因为最新版本和我们公司的项目不适配,用的还是之前的旧版本
FROM centos7
LABEL maintainer=kfknh_111@163.com
RUN yum -y??install zip unzip?vim??epel-release net-tools
RUN yum -y?install?python-pip
WORKDIR /data/minio
ADD minio ./
RUN mv minio /usr/local/bin
RUN chmod -R?755?/usr/local/bin/minio
RUN?mkdir?-p?/data/minio/data
WORKDIR /root/.pip/
RUN touch pip.conf
RUN?echo?"[global]"?>> pip.conf
RUN?echo?"index-url = https://pypi.tuna.tsinghua.edu.cn/simple/"?>> pip.conf
RUN?echo?"[install]"?>> pip.conf
RUN?echo?"trusted-host=pypi.tuna.tsinghua.edu.cn"?>> pip.conf
RUN pip install --upgrade?"pip < 21.0"
RUN pip2 install --upgrade setuptools
RUN pip2 install thumbor
WORKDIR /root/
RUN thumbor-config > ./thumbor.conf
RUN mv thumbor.conf?/etc/
WORKDIR /root/
RUN touch start.sh
RUN?echo?"nohup minio server /data/minio/data/ > /dev/null 2>&1 &"?>> start.sh
RUN?echo?"nohup thumbor --port=8888 --conf=/etc/thumbor.conf >/dev/null 2>&1 &"?>> start.sh
EXPOSE?9000?8888
RUN touch /dev/null
RUN?echo?"aaa"?>> /dev/null
RUN chmod?777?start.sh
CMD ["bash","-c","/root/start.sh && tail -f /dev/null"]
Mongodb
Mongodb最后在cmd里面添加了 tail -f /den/null 是因为 他不能前台运行,容器中?cmd里面的内容停止,容器也会跟着停止(我是这么理解的),为了防止主进程退出导致容器退出,加了一个死循环
FROM centos7
RUN yum -y?install wget?vim?net-tools zip unzip
WORKDIR /opt/
RUN wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.4.tgz
RUN tar -zxvf mongodb-linux-x86_64-rhel70-4.4.4.tgz
RUN mv mongodb-linux-x86_64-rhel70-4.4.4?mongodb
RUN?mkdir?-p?/data/mongodb/{db,log}
WORKDIR /opt/mongodb/
RUN?echo?"dbpath=/data/mongodb/db"?>> mongodb.conf
RUN?echo?"logpath=/data/mongodb/log/mongodb.log"?>> mongodb.conf
RUN?echo?"logappend=true"?>> mongodb.conf
RUN?echo?"port=27017"?>> mongodb.conf
RUN?echo?"fork=true"?>> mongodb.conf
RUN?echo?"auth=false"?>> mongodb.conf
RUN?echo?"#nohttpinterface=true"?>> mongodb.conf
RUN?echo?"bind_ip = 0.0.0.0"?>> mongodb.conf
RUN?echo?"journal=true"?>> mongodb.conf
RUN?echo?"quiet=true"?>> mongodb.conf
WORKDIR /opt/mongodb/bin
EXPOSE?27017
RUN touch /dev/null
RUN?echo?"aaa"?>> /dev/null
RUN?echo?"./mongod -f ../mongodb.conf"?>> start.sh
RUN chmod?777?start.sh
CMD ["bash","-c","./start.sh && tail -f /dev/null"]
Rabbitmq
rabbitmq这个镜像,我觉得最要注意的就是默认用户远程登陆,如下代码中,因为我需要启动图形化,所以就写了一个小脚本,这样cmd里面直接指定脚本就可以,一定要注意的是 脚本要有权限,bash -c ./脚本名 一定要加./,还有rabbitmq的包我使用rpm命令安装不上去,就更换为yum来安装。
FROM centos7
LABEL maintainer=kfknh_111@163.com
RUN yum -y?install?zip unzip vim epel-release??net-tools socat
WORKDIR /root
ADD?erlang-22.3.4.19-1.el7.x86_64.rpm ./
ADD?rabbitmq-server-3.8.14-1.el7.noarch.rpm ./
RUN rpm -ivh erlang-22.3.4.19-1.el7.x86_64.rpm
RUN yum -y?install?rabbitmq-server-3.8.14-1.el7.noarch.rpm
RUN echo?"[{rabbit, [{tcp_listeners, [5672]}, {loopback_users, []}]}]."?>> /etc/rabbitmq/rabbitmq.config
RUN echo?"rabbitmq-plugins enable rabbitmq_management"?>> start.sh
RUN echo?"rabbitmq-server"?>> start.sh
# rabbitmq-server?-detached 后台启动
RUN chmod?777?start.sh
EXPOSE?5672?15672?25672
CMD ["bash","-c","./start.sh"]
Redis
redis没有什么需要注意的,把配置文件里面 bind 和密码修改下就可以了,容器中不能使用systemctl进行启动,如果需要的话 要在运行容器的时候进行提权,不过 --privileged=true ?在我这里不支持 一直报错
FROM centos7
MAINTAINER kfknh
RUN yum -y?update?&& yum -y?install epel-release && yum -y?install redis zip unzip wget?vim?net-tools
EXPOSE?6379
RUN sed -i -e?'s@bind 127.0.0.1@bind 0.0.0.0@g'?/etc/redis.conf
RUN?echo?"requirepass 1Q2W3E4R#"?>> /etc/redis.conf
RUN sed -i -e?'s@protected-mode yes@protected-mode no@g'?/etc/redis.conf
ENTRYPOINT ["redis-server","/etc/redis.conf"]
CMD []
以上就是本期所有内容,如果后面还有新的,如果觉得这个文章对你有所帮助的话,也请点赞评论转发一条龙??
点击上方“蓝字”,关注更多精彩
|