duanlan7239
2017-09-22 04:42
浏览 733

如何将标准输出从Docker容器重定向到主机

I'm trying to call docker's mysqldump from host system for save mysql dump from golang. It works correctly with host mysqldump, but don't work with docker's mysqldump.

func writeDb(dbName string)
{
   var mysqldumpPath string = "/usr/bin/mysqldump"
 //var mysqldumpPath string = "/Applications/MAMP/Library/bin/mysqldump"

 //cmd := exec.Command(mysqldumpPath, fmt.Sprintf("-u%s", USER), fmt.Sprintf("-p%s", PASSWORD) , dbName)
   cmd := exec.Command("docker", "exec", "some-mysql", mysqldumpPath, fmt.Sprintf("%s", USER), fmt.Sprintf("-p%s", PASSWORD) , dbName, ">", fmt.Sprintf("%s.sql", dbName))

   stdout, err := cmd.StdoutPipe()
   if err != nil
   {
     log.Fatal(err)
   }

   if err := cmd.Start(); err != nil
   {
     log.Fatal(err)
   }

   bytes, err := ioutil.ReadAll(stdout)
   if err != nil
   {
      log.Fatal(err)
   }
   err = ioutil.WriteFile("./backup/" + dbName +".sql", bytes, 0644)
   if err != nil
   {
     panic(err)
   }

}

I got only that for non-empty database empty mysql dump

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

相关推荐 更多相似问题