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

jQuery implements simple button color change

In HTML and CSS, we want to set the color of a bu...

Detailed explanation of using Vue.prototype in Vue

Table of contents 1. Basic Example 2. Set the sco...

Use of docker system command set

Table of contents docker system df docker system ...

MySQL master-slave data is inconsistent, prompt: Slave_SQL_Running: No solution

This article uses an example to describe the solu...

Solve the problem of inconsistency between mysql time and system time in docker

Recently, when I installed MySQL in Docker, I fou...

CentOS IP connection network implementation process diagram

1. Log in to the system and enter the directory: ...

Introduction to using Unicode characters in web pages (&#,\u, etc.)

The earliest computers could only use ASCII chara...

Implementation of element shuttle frame performance optimization

Table of contents background Solution New Questio...

Ubuntu opens port 22

Scenario You need to use the xshell tool to conne...

Installation and use of mysql on Ubuntu (general version)

Regardless of which version of Ubuntu, installing...

vue_drf implements SMS verification code

Table of contents 1. Demand 1. Demand 2. SDK para...

Programs to query port usage and clear port usage in Windows operating system

In Windows operating system, the program to query...

Example code for mixing float and margin in CSS

In my recent studies, I found some layout exercis...

Tutorial diagram of using Jenkins for automated deployment under Windows

Today we will talk about how to use Jenkins+power...