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

Detailed example of using if statement in mysql stored procedure

This article uses an example to illustrate the us...

Detailed explanation of the this pointing problem in JavaScript

Summarize Global environment ➡️ window Normal fun...

Summary of several APIs or tips in HTML5 that cannot be missed

In previous blog posts, I have been focusing on so...

How to configure nginx+php+mysql in docker

First, understand a method: Entering a Docker con...

Analyze the usage and principles of Vue's provide and inject

First, let's talk about why we use provide/in...

A brief analysis of the function calling process under the ARM architecture

Table of contents 1. Background knowledge 1. Intr...

Detailed explanation of custom swiper component in JavaScript

Table of contents Effect display Component Settin...

Docker case analysis: Building a MySQL database service

Table of contents 1 Create configuration and data...

Several ways to remove the dotted box that appears when clicking a link

Here are a few ways to remove it: Add the link dir...

MySQL 8.0.21 installation and configuration method graphic tutorial

Record the installation and configuration method ...

Use JS to operate files (FileReader reads --node's fs)

Table of contents JS reads file FileReader docume...

Simple implementation of mini-vue rendering

Table of contents Preface Target first step: Step...

Complete steps to use mock.js in Vue project

Using mock.js in Vue project Development tool sel...