CentOS7下搭建fastdfs最优雅的方法——Docker

之前手动搭建最简单的fdfs环境时,步骤繁琐,还需要配置nginx暴露storage的文件访问,现在可以使用Docker的虚拟机快速搭建一个简单可用的fdfs环境

2021-04-08更新

解决部分环境从github拉取依赖时连接超时的问题

介绍

fastdfs5.11的dockerfile
fastdfs:5.11、nginx、fastdfs-nginx-module整合到一个docker镜像中
fastdfs tracker_server、storage_server都只有一个
适用于需要fastdfs文件服务但又不需要集群部署,或者快速开始
包含一个java的测试脚本

使用方法

  1. 安装docker
  2. 将Dockerfile上传到Linux
  3. 构建镜像docker build -t fastdfs .不要丢掉最后的点
  4. 构建容器并启动
    docker run -d --name fastdfs -v /usr/local/data/fastdfs:/var/local/fdfs -e "IP=宿主机ip" -e "WEB_PORT=8888" --net="host" fastdfs

说明:
-v /usr/local/data/fastdfs:/var/local/fdfs 为文件目录映射,文件最终保存在宿主机/usr/local/data/fastdfs目录中
-e “IP=宿主机ip” 为宿主机局域网ip
-e “WEB_PORT=8888” 自定义web端口,还支持FDFS_PORT(实际为tracker_server的port,当初起名没起好,懒得改了)、STORAGE_PORT
–net=“host” 必须为host模式

下面是Dockerfile

FROM alpine:3.6

MAINTAINER xsj <soft_xiang@qq.com>

RUN echo -e "\
https://mirrors.aliyun.com/alpine/v3.6/main\n\
https://mirrors.aliyun.com/alpine/v3.6/community\n\
">/etc/apk/repositories
ARG TZ="Asia/Shanghai"

ENV TZ ${TZ}

#安装准备
RUN apk update \
    && apk add --no-cache  git gcc libc-dev make automake autoconf libtool pcre pcre-dev zlib zlib-dev openssl-dev wget vim \
	&& apk add bash tzdata \
    && ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \
    && echo ${TZ} > /etc/timezone \
	&& rm -rf /var/cache/apk/*

# 指定fastdfs 5.11版本
#下载fastdfs.libfastcommon.nginx插件源码
RUN cd /root \
    && git clone git://github.com/happyfish100/libfastcommon.git --depth 1 \
    && git clone git://github.com/happyfish100/fastdfs.git --depth 1 \
    && wget http://nginx.org/download/nginx-1.15.4.tar.gz \
    && git clone git://github.com/happyfish100/fastdfs-nginx-module.git --depth 1

ENV HOME /root
 # 安装libfastcommon
RUN cd ${HOME}/libfastcommon/ \
 	&& git fetch --depth 1 origin tag V1.0.36 \
	&& git checkout V1.0.36  \
    && ./make.sh  \
    && ./make.sh install

# 安装fastdfs
RUN cd ${HOME}/fastdfs/ \
	&& git fetch --depth 1 origin tag V5.11 \
	&& git checkout V5.11 \
    && ./make.sh \
    && ./make.sh install \
    && cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf \
    && cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf \
    && cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf \
    && cp ${HOME}/fastdfs/conf/http.conf /etc/fdfs/ \
    && cp ${HOME}/fastdfs/conf/mime.types /etc/fdfs/ \
    && sed -i "s|/home/yuqing/fastdfs|/var/local/fdfs/tracker|g" /etc/fdfs/tracker.conf \
    && sed -i "s|/home/yuqing/fastdfs|/var/local/fdfs/storage|g" /etc/fdfs/storage.conf \
    && sed -i "s|/home/yuqing/fastdfs|/var/local/fdfs/storage|g" /etc/fdfs/client.conf
	
# 切换fastdfs-nginx-module 版本 ,只能指定 85347be190dcfe9d46bd67176f84afaa32718142版本
RUN cd ${HOME}/fastdfs-nginx-module/ \
	&& git fetch --depth 2 origin tag V1.20 \
	&& git checkout 85347be190dcfe9d46bd67176f84afaa32718142
	
# 获取nginx源码
RUN cd ${HOME} \
    && tar -zxvf nginx-1.15.4.tar.gz  \
    && cd nginx-1.15.4/ \
    && ./configure --add-module=${HOME}/fastdfs-nginx-module/src/ \
    && make \
    && make install

# 设置nginx和fastdfs联合环境,并配置nginx
RUN cp ${HOME}/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs \
    && sed -i "s|^store_path0.*$|store_path0=/var/local/fdfs/storage|g" /etc/fdfs/mod_fastdfs.conf \
    && sed -i "s|^url_have_group_name =.*$|url_have_group_name = true|g" /etc/fdfs/mod_fastdfs.conf \
    && cd ${HOME}/fastdfs/conf/ \
    && echo -e "\
    events {\n\
        worker_connections  1024;\n\
    }\n\
    http {\n\
        include       mime.types;\n\
        default_type  application/octet-stream;\n\
        server {\n\
            listen 8888;\n\
            server_name localhost;\n\
            location ~ /group[0-9]/M00 {\n\
                ngx_fastdfs_module;\n\
            }\n\
        }\n\
    }">/usr/local/nginx/conf/nginx.conf

# 清理文件
RUN rm -rf ${HOME}/*
RUN apk del gcc libc-dev make openssl-dev
# 配置启动脚本,在启动时中根据环境变量替换nginx端口、fastdfs端口
# 默认nginx端口
ENV WEB_PORT 8888
# 默认fastdfs端口
ENV FDFS_PORT 22122
# 默认fastdfs storage server 端口
ENV STORAGE_PORT 23000

# 创建启动脚本
RUN     echo -e "\
mkdir -p /var/local/fdfs/storage/data /var/local/fdfs/tracker; \n\
#ln -s /var/local/fdfs/storage/data/ /var/local/fdfs/storage/data/M00; \n\n\
sed -i \"s/listen\ .*$/listen\ \$WEB_PORT;/g\" /usr/local/nginx/conf/nginx.conf; \n\
sed -i \"s/port=.*$/port=\$FDFS_PORT/g\" /etc/fdfs/tracker.conf; \n\n\
sed -i \"s/http.server_port=.*$/http.server_port=\$WEB_PORT/g\" /etc/fdfs/storage.conf; \n\n\
if [ \"\$IP\" = \"\" ]; then \n\
    IP=`ifconfig eth0 | grep inet | awk '{print \$2}'| awk -F: '{print \$2}'`; \n\
fi \n\
sed -i \"s/^tracker_server=.*$/tracker_server=\$IP:\$FDFS_PORT/g\" /etc/fdfs/client.conf; \n\
sed -i \"s/^tracker_server=.*$/tracker_server=\$IP:\$FDFS_PORT/g\" /etc/fdfs/storage.conf; \n\
sed -i \"s/^tracker_server=.*$/tracker_server=\$IP:\$FDFS_PORT/g\" /etc/fdfs/mod_fastdfs.conf; \n\n\
sed -i \"s/^port=.*$/port=\$STORAGE_PORT/g\" /etc/fdfs/storage.conf; \n\
sed -i \"s/^storage_server_port=.*$/storage_server_port=\$STORAGE_PORT/g\" /etc/fdfs/mod_fastdfs.conf; \n\n\
/etc/init.d/fdfs_trackerd start; \n\
/etc/init.d/fdfs_storaged start; \n\
/usr/local/nginx/sbin/nginx; \n\
tail -f /usr/local/nginx/logs/access.log \
">/start.sh \
&& chmod u+x /start.sh

# 暴露端口。改为采用host网络,不需要单独暴露端口
# EXPOSE 80 22122 23000

ENTRYPOINT ["/bin/bash","/start.sh"]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值