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

WeChat applet implements a simple calculator

WeChat applet's simple calculator is for your...

Solve the problem of insufficient docker disk space

After the server where Docker is located has been...

Some improvements in MySQL 8.0.24 Release Note

Table of contents 1. Connection Management 2. Imp...

Solution to input cursor misalignment in Chrome, Firefox, and IE

Detailed explanation of the misplacement of the in...

MySql index detailed introduction and correct use method

MySql index detailed introduction and correct use...

Detailed explanation of webpage screenshot function in Vue

Recently, there is a requirement for uploading pi...

Why is UTF-8 not recommended in MySQL?

I recently encountered a bug where I was trying t...

How to limit the number of records in a table in MySQL

Table of contents 1. Trigger Solution 2. Partitio...

Centering the Form in HTML

I once encountered an assignment where I was give...

5 Ways to Send Emails in Linux Command Line (Recommended)

When you need to create an email in a shell scrip...

Install two MySQL5.6.35 databases under win10

Record the installation of two MySQL5.6.35 databa...