0、前期准备工作:
查看CentOS版本
[root@VM-8-7-centos ~]# lsb_release -a
CentOS 7安装docker要求系统为64位、系统内核版本为 3.10 以上
[root@VM-8-7-centos ~]# uname -r
1、用yum源安装docker并启动(方法一):
查看是否已安装docker列表
[root@VM-8-7-centos ~]# yum list installed | grep docker
安装docker
[root@VM-8-7-centos ~]# yum -y install docker
启动docker
[root@VM-8-7-centos ~]# systemctl start docker
查看docker服务状态,当看到Active: active (running)时,说明启动成功
[root@VM-8-7-centos ~]# systemctl status docker
1、用yum源安装docker并启动(方法二):
以下命令都是在root账号权限下执行的
1、安装相关依赖
[root@VM-8-7-centos ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
[root@VM-8-7-centos ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
2、安装最新版本的Docker CE和containerd
[root@VM-8-7-centos ~]# yum install docker-ce docker-ce-cli containerd.io
3、启动Docker
[root@VM-8-7-centos ~]# systemctl start docker
4、通过运行hello-world 映像验证是否正确安装了Docker CE 。
[root@VM-8-7-centos ~]# docker run hello-world
5、ip addr查看docker是否成功安装,可以看到有三个IP段lo、eth0、docker0,最后的docker0就是我们安装成功的部分
[root@VM-8-7-centos ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:9b:a1:8e brd ff:ff:ff:ff:ff:ff
inet 10.0.8.7/22 brd 10.0.11.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::5054:ff:fe9b:a18e/64 scope link
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:e0:ca:af:52 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 scope global docker0
valid_lft forever preferred_lft forever
6、启动docker命令
[root@VM-8-7-centos ~]# systemctl start docker.service
7、设置开机启动docker
[root@VM-8-7-centos ~]# systemctl enable docker
8、查看版本
[root@VM-8-7-centos ~]# docker version
Client:
Version: 1.13.1
API version: 1.26
Package version: docker-1.13.1-203.git0be3e21.el7.centos.x86_64
Go version: go1.10.3
Git commit: 0be3e21/1.13.1
Built: Thu Nov 12 15:11:46 2020
OS/Arch: linux/amd64
Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Package version: docker-1.13.1-203.git0be3e21.el7.centos.x86_64
Go version: go1.10.3
Git commit: 0be3e21/1.13.1
Built: Thu Nov 12 15:11:46 2020
OS/Arch: linux/amd64
Experimental: false
区别:centos7默认安装的docker都是免费的社区版版本
2、查找Docker Hub上的fastdfs镜像:
查找fastdfs镜像
[root@VM-8-7-centos ~]# docker search fastdfs
拉取最新版本
[root@VM-8-7-centos ~]# docker pull delron/fastdfs
查看镜像
[root@VM-8-7-centos ~]# docker images
使用docker镜像构建tracker容器(跟踪服务器,起到调度的作用)
[root@VM-8-7-centos ~]# docker run -dti --network=host --name tracker -v /var/fdfs/tracker:/var/fdfs -v /etc/localtime:/etc/localtime delron/fastdfs tracker
*** 注意:TRACKER_SERVER=本机的ip地址:22122 本机ip地址不要使用127.0.0.1
使用docker镜像构建storage容器(存储服务器,提供容量和备份服务)
[root@VM-8-7-centos ~]# docker run -dti --network=host --name storage -e TRACKER_SERVER=192.168.56.1:22122 -v /var/fdfs/storage:/var/fdfs -v /etc/localtime:/etc/localtime delron/fastdfs storage
3、如果ip设置错了,请看下面的步骤
查看当前运行下的镜像进程
[root@VM-8-7-centos logs]# docker ps -a
停止storage这个镜像
[root@VM-8-7-centos logs]# docker stop [填写你的storage的CONTAINER ID]
删除storage这个镜像
[root@VM-8-7-centos logs]# docker stop [填写你的storage的CONTAINER ID]
之后再重新新建
4、fastdfs启动之后的操作
查看可以看到storage、tracker两个已经启动起来了
[root@VM-8-7-centos logs]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
21c94b7f0547 delron/fastdfs "/usr/bin/start1.s..." 8 minutes ago Up 8 minutes storage
bc258c1b3dfd delron/fastdfs "/usr/bin/start1.s..." 8 minutes ago Up 8 minutes tracker
需要重新启动下
[root@VM-8-7-centos logs]# systemctl restart docker
重新查看下状态
[root@VM-8-7-centos logs]# docker status
进入容器查看storage:
[root@VM-8-7-centos logs]# docker exec -it storage /bin/bash
进入到cd /etc/fdfs下的storage.conf
[root@VM-8-7-centos fdfs]# cd /etc/fdfs
[root@VM-8-7-centos fdfs]# pwd
/etc/fdfs
[root@VM-8-7-centos fdfs]# ll
total 88
-rw-rw-r-- 1 root root 1449 Jan 7 03:49 client.conf
-rw-r--r-- 1 root root 1461 Apr 29 2018 client.conf.sample
-rw-r--r-- 1 root root 955 Apr 29 2018 http.conf
-rw-r--r-- 1 root root 31172 Apr 29 2018 mime.types
-rw-r--r-- 1 root root 3677 Jan 7 03:49 mod_fastdfs.conf
-rw-rw-r-- 1 root root 7891 Jan 7 03:49 storage.conf
-rw-r--r-- 1 root root 7927 Apr 29 2018 storage.conf.sample
-rw-rw-r-- 1 root root 105 Apr 29 2018 storage_ids.conf
-rw-r--r-- 1 root root 105 Apr 29 2018 storage_ids.conf.sample
-rw-rw-r-- 1 root root 7307 Apr 29 2018 tracker.conf
-rw-r--r-- 1 root root 7389 Apr 29 2018 tracker.conf.sample
进入storage.conf
[root@VM-8-7-centos fdfs]# vi storage.conf
--------------------------以下是storage.conf文件--------------------------
# is this config file disabled
# false for enabled
# true for disabled
# 这个是要新添加上去的,其他的不用动
http.server_port=8888
disabled=false
# the name of the group this storage server belongs to
#
# comment or remove this item for fetching from tracker server,
# in this case, use_storage_id must set to true in tracker.conf,
# and storage_ids.conf must be configed correctly.
group_name=group1
# bind an address of this host
# empty for bind all addresses of this host
bind_addr=
# if bind an address of this host when connect to other servers
# (this storage server as a client)
# true for binding the address configed by above parameter: "bind_addr"
# false for binding any address of this host
client_bind=true
......以下省略
--------------------------以上是storage.conf文件--------------------------
5、配置nginx(进入storage配置),此步骤可以不操作
进入容器查看storage:
[root@VM-8-7-centos logs]# docker exec -it storage /bin/bash
在/usr/local/nginx/conf目录下,修改nginx.conf文件
[root@VM-8-7-centos conf]# cd /usr/local/nginx/conf
--------------------------以下是nginx.conf文件--------------------------
server {
listen 8888;
server_name localhost;
location ~/group[0-9]/ {
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
--------------------------以上是nginx.conf文件--------------------------
6、激动人心的时刻到了!!!开始测试上传文件!!!
使用web模块进行文件的上传,将文件上传至FastDFS文件系统。
将一张照片(logo.png)放置在/var/fdfs/storage目录下,进入storage容器,进入/var/fdfs目录,运行下面命令:
1、在宿主机中。
1.1、这个是宿主机,也就是你的服务器,并不是容器里的目录
[root@VM-8-7-centos etc]# cd /var/fdfs/storage
1.2、把图片放在该文件夹下,这里给了一个样例,logo.png
[root@VM-8-7-centos storage]# ll
总用量 16
drwxr-xr-x 259 root root 4096 1月 7 04:17 data
-rw-r--r-- 1 root root 5386 1月 7 03:45 logo.png
drwxr-xr-x 2 root root 4096 1月 7 04:04 logs
2、进入进入storage容器,进入/var/fdfs目录。
2.1、进入容器查看storage:
[root@VM-8-7-centos logs]# docker exec -it storage /bin/bash
2.2、进入/var/fdfs下
[root@VM-8-7-centos logs]# cd /var/fdfs
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf [你的图片名称.后缀名]
[root@VM-8-7-centos fdfs]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf logo.png
group1/M00/00/00/CgAIB1_2HZ2AIebEAAAVCnox6pU455.png
此时将该图片已上传至文件系统,并在执行该语句后返回图片存储的uri:
group1/M00/00/00/CgAIB1_2HZ2AIebEAAAVCnox6pU455.png
通过url访问:
http://ip:8888/group1/M00/00/00/CgAIB1_2HZ2AIebEAAAVCnox6pU455.png,即可查看到图片
7、开放端口
firewall-cmd --zone=public --permanent --add-port=8888/tcp
firewall-cmd --zone=public --permanent --add-port=22122/tcp
firewall-cmd --zone=public --permanent --add-port=23000/tcp
重启防火墙
systemctl restart firewalld
8.开启启动容器
docker update --restart=always tracker
docker update --restart=always storage
能够用得到的一些命令:
docker中 启动所有的容器命令
[root@VM-8-7-centos logs]# docker start $(docker ps -a | awk '{ print $1}' | tail -n +2)
docker中 关闭所有的容器命令
[root@VM-8-7-centos logs]# docker stop $(docker ps -a | awk '{ print $1}' | tail -n +2)
docker中 删除所有的容器命令
[root@VM-8-7-centos logs]# docker rm $(docker ps -a | awk '{ print $1}' | tail -n +2)
docker中 删除所有的镜像
[root@VM-8-7-centos logs]# docker rmi $(docker images | awk '{print $3}' |tail -n +2)
文章如果有错误,欢迎指正点评~
文章参考来源