背景
在构建区块链日志模块时,通常需要将容器内的日志文件映射至宿主机,以便于后续查看与分析。以下详述具体的实现路径。
实现
利用 docker-compose 配置文件中的 volumes 参数进行设置。
配置文件示例如下:
volumes:
- /var/run/:/host/var/run/
- ./channel-artifacts:/var/hyperledger/configs
- ./fabric_logs:/tmp/fabric_logs/
该配置将容器内 /tmp/fabric_logs 目录映射至宿主机当前路径下的 ./fabric_logs 目录,两者之间实现数据共享。
在创建容器的代码逻辑中配置相关参数。
代码中创建容器时需添加相应配置:
func (vm *DockerVM) createContainer(ctxt context.Context, client dockerClient,
imageID string, containerID string, args []string,
env []string, attachStdout bool) error {
volumes := make(map[string]struct{})
var mounts []docker.Mount
var source string
var destination string
var fabricCfgPath = os.Getenv("FABRIC_CFG_PATH")
var configName string
_, err := os.Stat(fabricCfgPath)
if err == nil {
configName = strings.ToLower(Peer_Prefix)
config := viper.New()
config.SetConfigName(configName)
config.AddConfigPath(fabricCfgPath)
config.ReadInConfig()
config.SetEnvPrefix("CORE")
config.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
config.SetEnvKeyReplacer(replacer)
config.SetConfigType("yaml")
destination = config.GetString("logging.logpath")
//fmt.Println(destination)
}
if destination == "" {
destination = "/tmp/fabric_logs/"
}
source = "/tmp/chaincode_logs/" + containerID
volumes[destination] = struct{}{}
mount := docker.Mount{
Name: "bind",
Source: source,
Destination: destination,
Mode: "rw",
RW: true,
Driver: "rprivate",
}
mounts = append(mounts, mount)
config := docker.Config{Cmd: args, Image: imageID, Env: env, Volumes: volumes, Mounts: mounts, AttachStdout: attachStdout, AttachStderr: attachStdout}
hostConfig := getDockerHostConfig()
hostConfig.Binds = []string{
source + ":" + destination + ":rw",
}
copts := docker.CreateContainerOptions{Name: containerID, Config: &config, HostConfig: hostConfig}
dockerLogger.Debugf("Create container: %s", containerID)
_, err = client.CreateContainer(copts)
if err != nil {
return err
}
dockerLogger.Debugf("Created container: %s", imageID)
return nil
}
其中 volumes、Mounts 以及 Hostconfig.Binds 参数需根据实际的映射关系进行填写。
此方法的效果等同于:
1、使用 docker-compose 配置文件启动
2、或者使用 docker -v 参数命令行启动
均可达到相同的数据映射效果。
补充说明:Docker 文件夹映射主要包含两种模式——主机卷映射和共享文件夹映射。
Docker 容器本身不保持任何数据。
重要数据请使用外部卷存储以实现数据持久化。
容器可以挂载真实机目录或共享存储为卷。
主机卷的映射
[root@docker1 ~]# mkdir /var/data
[root@docker1 ~]# docker run -it -v /var/data:/abc myos
[root@f1fb58b85671 /]# cd /abc/
[root@f1fb58b85671 abc]# touch f1
[root@f1fb58b85671 abc]# ls
f1 zhy
[root@docker1 ~]# cd /var/data/
[root@docker1 data]# ls
f1
[root@docker1 data]# touch zhy
使用共享存储的映射
思路:
将一台主机作为 NFS 服务器,创建相应文件夹并共享给两台 Docker 主机。这两台 Docker 主机将该共享文件夹映射到容器中,使得对应容器能够访问 NFS 主机的内容。这种方式适用于 HTTP 等服务器的页面文件夹,从而实现多个容器运行同一个业务。
NFS 主机配置【192.168.6.77】
[root@nfs ~]# yum -y install nfs-utils
[root@nfs ~]# vim /etc/exports
/public *(rw)
[root@nfs ~]# systemctl restart nfs-server
Failed to restart nfs-serve.service: Unit not found
[root@nfs ~]# mkdir /public
[root@nfs ~]# cd /public/
[root@nfs public]# touch nfs.txt
[root@nfs public]# ls
nfs.txt
docker1 主机配置
[root@docker1 ~]# vim /etc/fstab
192.168.6.77:/public /mnt/nfs nfs defaults,_netdev 0 0
[root@docker1 ~]# mkdir /mnt/nfs
[root@docker1 ~]# systemctl restart nfs-server
[root@docker1 ~]# mount -a
[root@docker1 ~]# df -h
192.168.6.77:/public 17G 3.2G 14G 19% /mnt/nfs
[root@docker1 ~]# docker run -it -v /mnt/nfs/:/zhuhaiyan 192.168.6.153:5000/myos
[root@c7c376e3755a /]# cd /zhuhaiyan
[root@c7c376e3755a zhuhaiyan]# ls
nfs.txt
docker2 主机配置
[root@docker2 ~]# vim /etc/fstab
192.168.6.77:/public /mnt/nfs nfs defaults,_netdev 0 0
[root@docker2 ~]# mkdir /mnt/nfs
[root@docker2 ~]# systemctl restart nfs-server
[root@docker2 ~]# mount -a
[root@docker2 ~]# df -h
192.168.6.77:/public 17G 3.2G 14G 19% /mnt/nfs
[root@docker2 ~]# docker run -it -v /mnt/nfs/:/zhuhaiyan 192.168.6.153:5000/myos
[root@cdd805771d07 /]# cd /zhuhaiyan/
[root@cdd805771d07 zhuhaiyan]# ls
nfs.txt
以上为个人经验,希望能给大家一个参考,也希望大家多多支持本站。如有错误或未考虑完全的地方,望不吝赐教。