Docker volumes file mapping method

Docker volumes file mapping method

background

When working on the blockchain log module, if the container is running, the log file needs to be mapped to the host machine for easy viewing. Here is how I implement it.

accomplish

Through the docker-compose configuration file volumes parameter

Configuration file example:

 volumes:
   - /var/run/:/host/var/run/
   - ./channel-artifacts:/var/hyperledger/configs
   - ./fabric_logs:/tmp/fabric_logs/

Map the /tmp/fabric_logs directory in the container to the ./fabric_logs directory in the current directory of the host. These two directories will share data.

When creating a container, configure relevant parameters in the code

When creating a container in the code, add:

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
}

The volumes, Mounts, and Hostconfig.Binds parameters need to be filled in according to your own mapping relationships.

This way and through:

1. Docker-compose configuration file startup

2. Or start with the docker -v parameter command line

Achieve the same effect.

Supplement: Two ways of docker folder mapping --- host volume mapping and shared folder mapping

Docker containers do not store any data

Please use external volume storage for important data (data persistence)

Containers can mount real machine directories or shared storage as volumes

Host volume mapping

[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
[root@docker1 ~]# cd /var/data/
[root@docker1 data]# ls
f1
[root@docker1 data]# touch zhy

Mapping using shared storage

Ideas:

Use one host as the nfs host, create corresponding folders, and share them with the two docker hosts. The two docker hosts map the shared folders to the containers so that the corresponding containers can share the contents of the nfs host. The corresponding page folders of http and other servers can be used in this form, so that multiple containers can run one business.

nfs host configuration [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 host configuration

[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 host configuration

[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

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Introduction to container data volumes in Docker
  • Two ways to manage volumes in Docker
  • Docker volume deletion operation
  • How to implement Docker volume mounting
  • Docker Data Storage Volumes Detailed Explanation
  • Docker volume usage details and examples
  • Docker writes data to the data volume

<<:  Detailed explanation and practical exercises of Mysql tuning Explain tool (recommended)

>>:  Do designers need to learn to code?

Recommend

Example of how to create a database name with special characters in MySQL

Preface This article explains how to create a dat...

svg+css or js to create tick animation effect

Previously, my boss asked me to make a program th...

JavaScript function call classic example code

Table of contents JavaScript function call classi...

Problems with using multiple single quotes and triple quotes in MySQL concat

When dynamically concatenating strings, we often ...

Summary of seven sorting algorithms implemented in JavaScript (recommended!)

Table of contents Preface Bubble Sort Basic Algor...

The whole process of node.js using express to automatically build the project

1. Install the express library and generator Open...

Get the IP and host name of all hosts on Zabbix

zabbix Zabbix ([`zæbiks]) is an enterprise-level ...

How to write DROP TABLE in different databases

How to write DROP TABLE in different databases 1....

MySQL uses inet_aton and inet_ntoa to process IP address data

This article will introduce how to save IP addres...

Detailed explanation of Vue3 sandbox mechanism

Table of contents Preface Browser compiled versio...

Detailed explanation of the execution process of JavaScript engine V8

Table of contents 1. V8 Source 2. V8 Service Targ...

JS achieves five-star praise effect

Use JS to implement object-oriented methods to ac...

Detailed tutorial on installing and using Kong API Gateway with Docker

1 Introduction Kong is not a simple product. The ...

How to reset the root password of Mysql in Windows if you forget it

My machine environment: Windows 2008 R2 MySQL 5.6...