简体   繁体   English

将容器中的文件移动到已安装卷的文件夹 docker

[英]moving a file in a container to a folder that has a mounted volume docker

am trying to run a golang application on docker. But when i try to move created file in the container to the folder the created volume is mounted on,i get an error:rename /mygo/newt /mygo/store/newt: invalid cross-device link我正在尝试在 docker 上运行 golang 应用程序。但是当我尝试将容器中创建的文件移动到创建的卷所在的文件夹时,我收到错误消息:重命名/mygo/newt /mygo/store/newt:无效交叉-设备链接

my golang code我的高朗代码

package main

import (
    "bufio"
    "fmt"
    "os"
    "path/filepath"
    "strings"
)

func main() {

    for {

        fmt.Println("do you want to create a file,y for yes, n for no")
        var ans string
        fmt.Scanln(&ans)
        if ans == "y" {
            var userFile string
            fmt.Println("enter name of file")
            fmt.Scanln(&userFile)
            myfile, err := os.Create(userFile)
            if err != nil {
                fmt.Printf("error creating file::%v\n", err)
                return
            }
            fmt.Println("enter text to write in file")
            reader := bufio.NewReader(os.Stdin)
            input, err := reader.ReadString('\t')
            if err != nil {
                fmt.Println("an error occured while reading::", err)
                return
            }
            input = strings.TrimSuffix(input, "\t")
            num, err := myfile.WriteString(input)
            if err != nil {
                fmt.Println("error while writing to file", err)
            }
            fmt.Printf("%v characters entered \n", num)
            defer myfile.Close()
            fmt.Println("created a file", userFile)
            fmt.Println("===========")
            fmt.Println("moving file to default folder")
            pwd, err_pwd := os.Getwd()
            if err_pwd != nil {
                fmt.Printf("could not get current working directory::%v\n", err_pwd)
            }

            userFilePath := pwd + "/" + userFile
            fmt.Println(pwd)
            destinationFilePath := pwd + "/store/" + userFile
            //destinationFilePath := pwd + "/default/" + userFile
            err_moving := os.Rename(userFilePath, destinationFilePath)
            if err_moving != nil {
                fmt.Printf("Error occured while moving file::%v\n", err_moving)
                return
            }
            fmt.Println("file moved")
            continue
        }
        pwd, err_pwd := os.Getwd()
        if err_pwd != nil {
            fmt.Printf("could not get current working directory::%v\n", err_pwd)
        }
        fmt.Println("enter full path to move to default location")
        var userFilePath string
        fmt.Scanln(&userFilePath)
        userFileName := filepath.Base(userFilePath)
        destinationFilePath := pwd + "/" + userFileName
        err_move := os.Rename(userFilePath, destinationFilePath)
        if err_move != nil {
            fmt.Printf("error occured while moving file:::%v", err_move)
            return
        }
        fmt.Println("file moved")
        continue
    }
}

dockerfile dockerfile

FROM golang 

WORKDIR /mygo

COPY . .
RUN go build -o app
CMD ["./app"]

running the container运行容器

the program exits after the error错误后程序退出

There are two ways to "rename" a file in Linux.在 Linux 中有两种方法可以“重命名”一个文件。

  1. Move the directory entry to a new place, but keep the file contents unchanged.将目录条目移动到新位置,但保持文件内容不变。

    This has the advantage of being fast.这具有速度快的优点。 It has the disadvantage that it doesn't work when moving a file from one filesystem to another.它的缺点是在将文件从一个文件系统移动到另一个文件系统时不起作用。

  2. Create a new file, copy the data to the new file, and delete the old file.创建一个新文件,将数据复制到新文件,删除旧文件。

    However, it will work if the source and destination are on two different filesystems.但是,如果源和目标位于两个不同的文件系统上,它将起作用。

Method #1 will not work in this case.方法#1 在这种情况下不起作用。 You need method #2.你需要方法#2。

More resources:更多资源:

  • This golang-dev discussion explains why this happens.这个golang-dev 讨论解释了为什么会发生这种情况。
  • This question talks about the same problem, but in the context of C++. This question谈论同样的问题,但是在C++的上下文中。
  • Go uses the renameat() syscall internally. Go 在内部使用renameat()系统调用。 This manual page explains how it works.手册页解释了它是如何工作的。 The specific error you're encountering is the EXDEV error: "oldpath and newpath are not on the same mounted filesystem. (Linux permits a filesystem to be mounted at multiple points, but rename() does not work across different mount points, even if the same filesystem is mounted on both.)"您遇到的具体错误是 EXDEV 错误:“oldpath 和 newpath 不在同一个挂载的文件系统上。(Linux 允许在多个点挂载文件系统,但 rename() 不能跨不同的挂载点工作,即使两者上都安装了相同的文件系统。)”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM