在Docker容器(Ubuntu)中运行在Windows上创建的Go应用程序。

huangapple go评论92阅读模式
英文:

Run Go app created in windows in Docker container (Ubuntu)

问题

我创建了一个非常简单的应用程序,用于输出一些文本。

我的开发环境是Windows + Docker for Windows。我创建了一个Ubuntu容器,并在Windows上为Ubuntu构建了我的应用程序。

set GOARCH=amd64
set GOOS=linux
go build -o "myapp"

然后,我将'myapp'文件复制到共享文件夹中,并在容器的共享文件夹中看到它。然后我尝试在容器内运行它,但出现了错误。

为什么我不能运行它?是构建错误还是我以错误的方式执行它?

在Windows中,当我运行我的exe文件时,我希望在Ubuntu中得到类似的输出。

更新:
根据建议,我尝试运行./myapp,但出现了"Exec format error"的错误。

英文:

I created very simple application that put to output some text.

My development environment is Windows + Docker for Windows. I created ubuntu container and after build my application in Windows for ubuntu

set GOARCH=amd64
set GOOS=linux
go build -o "myapp"

Then I copy 'myapp' file to shared folder and see it in shared folder of my container.
After I try to run it inside container but there is an error

在Docker容器(Ubuntu)中运行在Windows上创建的Go应用程序。

Why can't I run it? Was it built incorrectly or do I try to execute it in some wrong way?

In windows I have this output when I run my exe file and I want something the same in Ubuntu.

在Docker容器(Ubuntu)中运行在Windows上创建的Go应用程序。

UPDATE
As suggested I try ./myapp and there is Exec format error

在Docker容器(Ubuntu)中运行在Windows上创建的Go应用程序。

答案1

得分: 1

在Ubuntu和其他Linux系统上,只需输入myapp,系统会尝试在PATH环境变量中查找myapp。如果要在当前目录中明确查找可执行文件,需要使用以下命令:

./myapp

或者使用完整路径:

/__shared/myapp

更新:为了使此方法生效,您的文件需要具有可执行权限。您可以使用ls -l命令进行验证:

ls -l
-rwxrwxr-x  1 user user    0 Feb  9 09:45 myapp

如果在第一列中看不到至少一个x,则需要运行以下命令:

chmod +x myapp

有关此主题的更多信息,请参阅理解Linux文件权限

更新:错误信息cannot execute binary file: Exec format error可能是由以下原因引起的:

  • 系统架构与文件不匹配 --> 确保您的Ubuntu机器是64位版本,并比较uname -afile myapp的输出。
  • 根据此线程的说法,在虚拟机的共享文件夹中运行文件(使用Windows主机)可能会导致问题 --> 尝试将myapp复制到共享文件夹之外。
英文:

On ubuntu and other linux systems, just myapp will try to find myapp from the PATH environment variable. To look specifically for an executable inside the current directory, you need to use:

./myapp

Or use the complete path:

/__shared/myapp

UPDATE: for this to work, your file need to be executable. You can verify it using ls -l:

ls -l
-rwxrwxr-x  1 user user    0 Feb  9 09:45 myapp

If you don't see at least one 'x' in the first column, you need to run:

chmod +x myapp

See understanding-linux-file-permissions for more information on the subject.

UPDATE: the error cannot execute binary file: Exec format error' could come from:

  • a mismatch between the system architecture and the file --> ensure your ubuntu machine is a 64-bit version and compare the output of uname -a and file myapp
  • according to this thread, running a file inside the shared folder of the VM (with windows host) can cause trouble --> try to copy myapp outside the shared folder

huangapple
  • 本文由 发表于 2017年2月9日 16:12:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/42131301.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定