docker volumes File Mapping Mode

  • 2021-10-25 00:10:32
  • OfStack

Background

When making blockchain log module, if the container runs, it is necessary to map the log file to the host machine for easy viewing. The following is an introduction to my implementation.

Realization

volumes parameters through docker-compose configuration file

Sample configuration file:


 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 under the current directory of the host. The two directories share data.

When the container is created, the relevant parameters are configured in the code

Add when you create a container in the code:


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
}

Among them, volumes, Mounts, Hostconfig. Binds parameters need to be filled in according to their own mapping relationship.

In this way and through:

1. docker-compose configuration file startup

2, or docker-v parameter command line startup

Achieve one effect.

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

The docker container does not hold any data

Use external volume storage for important data (data persistence)

Containers can mount real machine directories or shared storage as volumes

Mapping of host volumes


[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

Mapping using shared storage

Thoughts:

One host is used as the host of nfs, and the corresponding folder is created and shared with two hosts of docker. The two hosts of docker map the shared folder to the container, so that the corresponding container can share the contents of the host of nfs. You can use this form for the corresponding page folders of servers such as http, so as to realize multiple containers running 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

Related articles: