Docke 搭建单机 FastDFS 文件服务器

1143

CentOS Docker 安装

FastDFS安装

编写docker-compose.yaml

  • 在/mnt/docker目录下新建 fdfs文件夹,存储所有相关文件
cd /mnt/docker
mkdir fdfs
cd fdfs
  • 编写docker-compose.yaml

  • 执行命令:vim docker-compose.yml

version: '2'
services:
  tracker:
    image: season/fastdfs:1.2
    restart: always
    volumes:
      - "./tracker_data:/fastdfs/tracker/data"
    environment:
      TZ: "Asia/Shanghai"
    network_mode: "host"
    command: "tracker"

  storage:
    image: season/fastdfs:1.2
    restart: always
    volumes:
      - "./storage_base_path:/fastdfs/storage/data"
      - "./store_path0:/fastdfs/store_path"
      - "./fdfs_conf:/fdfs_conf"
    environment:
      TRACKER_SERVER: "yourIp:22122"
      TZ: "Asia/Shanghai"
    network_mode: "host"
    command: "storage"

  nginx:
    image: season/fastdfs:1.2
    restart: always
    volumes:
      - "./nginx.conf:/etc/nginx/conf/nginx.conf"
      - "./store_path0:/fastdfs/store_path"
    environment:
      TRACKER_SERVER: "yourIp:22122"
      TZ: "Asia/Shanghai"
    network_mode: "host"
    command: "nginx"

编写 storage.conf

  • 执行命令:vim storage.conf
# is this config file disabled
# false for enabled
# true for disabled
disabled=false

# the name of the group this storage server belongs to
group_name=group1
#默认组1即可,如果多个store 就写group2,一次类推

# path(disk or mount point) count, default value is 1
store_path_count=1

# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist
store_path0=/fastdfs/store_path 
#数据存储地址
#store_path1=/home/yuqing/fastdfs2

# subdir_count  * subdir_count directories will be auto created under each 
# store_path (disk), value can be 1 to 256, default value is 256
subdir_count_per_path=256

# tracker_server can ocur more than once, and tracker_server format is
#  "host:port", host can be hostname or ip address
#修改为tracker地址,单机修改本机即可
tracker_server=yourIp:22122
...
...
...
# the port of the web server on this storage server
http.server_port=8888

编写nginx.conf

  • 执行命令:vim nginx.conf
#user  nobody;
worker_processes  1;
 
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
http {
    include       mime.types;
    default_type  application/octet-stream;
 
    #access_log  logs/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
 
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #gzip  on;
 
    server {
        listen       23001;
        server_name  localhost;
 
        #charset koi8-r;
 
        #缩略图需要使用插件,需要单独构建nginx镜像,此处忽略
        #location /group([0-9])/M00/.*\.(gif|jpg|jpeg|png)$ {
         #   root /fastdfs/storage/data;
         #   image on;
         #   image_output off;         
         #   image_jpeg_quality 75;
         #   image_backend off;
        #    image_backend_server http://baidu.com/docs/aabbc.png;
       # }
       
        # group1
        location /group1/M00 {
        # 文件存储目录
            root /fastdfs/storage/data;
            ngx_fastdfs_module;
        }
 
        #error_page  404              /404.html;
 
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 }
}

编辑fdfs_conf目录

方案一

  • /mnt/docker/fdfs目录下新建目录fdfs_conf
mkdir fdfs_conf
cd fdfs_conf
unzip fdfs_conf.zip
  • 编辑storage.conf
vim storage.conf
  • 修改其中的tracker_server=172.19.244.99:22122tracker_server=yourIp:22122

以上fdfs_conf下的文件皆为按照博主自己方案安装时的端口号配置的,如果有端口号变动,请使用方案二。

方案二

  • /mnt/docker/fdfs目录下新建目录fdfs_conf
mkdir fdfs_conf
cd fdfs_conf
  • 复制容器内部的文件夹至宿主机内
docker cp fdfs_storage_1:/fdfs_conf/* /mnt/docker/fdfs/fdfs_conf
  • 编辑`storage.conf
vim storage.conf
  • 修改其中的tracker_server=172.19.244.99:22122tracker_server=yourIp:22122

  • 启动容器

docker-compsoe up -d

测试

  • 进入docker
docker exec -it fdfs_storage_1 bash
  • 进入内部fdfs_conf目录
cd /fdfs_conf/
  • 测试上传
fdfs_upload_file storage.conf anti-steal.jpg
  • 查看上传的文件
ls /fastdfs/store_path/data/00/00/
  • 开启外网端口,博主配置的端口为23001

查看上传的文件http://yourIp:23001/group1/M00/00/00/rBLcEV2kKyKAV8ugAACZsk13Smg395.jpg

后记

至此整个搭建流程已完成,使用API服务需要开启23000、23001、22122三个端口号,否则外网无法访问。如内网使用,则只需开启23001访问端口即可。

文章参考:文章参考无业游民-隋唐