Go应用程序失败并在使用docker-compose运行时退出,但可与docker run命令配合使用

我正在删除服务器上运行所有这些操作,该服务器是运行Ubuntu 16.04的VM
。 5 x64 </ strong>。</ p>

我的Go项目的Dockerfile如下:</ p>

  FROM golang:latest 

ADD。 $ GOPATH / src / example.com / myapp
WORKDIR $ GOPATH / src / example.com / myapp
RUN go build

#EXPOSE 80

#ENTRYPOINT $ GOPATH / src / example.com / myapp / myapp
ENTRYPOINT ./myapp
#CMD [“ ./myapp”]
</ code> </ pre>

当我使用 docker-docker-compose up -d运行Docker容器时 </ code>,Go应用程序退出,我在docker日志中看到了这一点:</ p>


myapp_1 | / bin / sh:1:./myapp:执行格式错误docker_myapp_1
以代码2退出</ p>
</ blockquote>

如果我使用 docker images < / code>并运行以下图像:</ p>

  docker run -it 75d4a95ef5ec 
</ code> </ pre>

我可以看到我的 golang应用程序可以正常运行:</ p>


Viper环境是:开发HTTP服务器侦听地址:
“:3005” </ p>
</ blockquote> \ n

当我搜索此错误时,有人建议使用一些特殊的标志进行编译,但是我在同一Ubuntu主机上运行此容器,因此我很困惑为什么不能使用docker。</ p> \ n

我的docker-compose.yml看起来像:</ p>

 版本:“ 3” 

services:
openresty:
构建:./openresty

端口:
-“ 80:80”
-“ 443:443”
depends_on:
-myapp
env_file:
-'.env'
重新启动:始终

myapp:

构建:../ myapp
卷:
-/home/deploy/apps/myapp:/go/src/example.com/myapp
端口:
-“ 3005:3005”
depends_on:
-db
-redis
-memcached
env_file:
-'.env'

redis:
图像:redis:alpine

端口:
-“ 6379:6379”
卷:
-“ / home / deploy / v / redis:/ data”
重新启动:始终

memcached:
图片:memcached
端口:
-“ 11211:11211”
重新启动:始终

db:
图片:postgres:9.4
卷:
-“ / home / deploy / v / pgdata:/ var / lib / postgresql / data“
重新启动:始终
</ code> </ pre>
</ div>

展开原文

原文

I am running all of these operations on a remove server that is a VM running Ubuntu 16.04.5 x64.

My Go project's Dockerfile looks like:

FROM golang:latest

ADD . $GOPATH/src/example.com/myapp
WORKDIR $GOPATH/src/example.com/myapp
RUN go build

#EXPOSE 80

#ENTRYPOINT $GOPATH/src/example.com/myapp/myapp
ENTRYPOINT ./myapp
#CMD ["./myapp"]

When I run the docker container using docker-compose up -d, the Go application exits and I see this in the docker logs:

myapp_1 | /bin/sh: 1: ./myapp: Exec format error docker_myapp_1 exited with code 2

If I locate the image using docker images and run the image like:

 docker run -it 75d4a95ef5ec 

I can see that my golang applications runs just fine:

viper environment is: development HTTP server listening on address: ":3005"

When I googled for this error some people suggested compiling with some special flags but I am running this container on the same Ubuntu host so I am really confused why this isn't working using docker.

My docker-compose.yml looks like:

version: "3"

services:
  openresty:
    build: ./openresty
    ports:
     - "80:80"
     - "443:443"
    depends_on:
      - myapp
    env_file:
     - '.env'
    restart: always

  myapp:
    build: ../myapp
    volumes:
     - /home/deploy/apps/myapp:/go/src/example.com/myapp
    ports:
      - "3005:3005"
    depends_on:
      - db
      - redis
      - memcached
    env_file:
      - '.env'

  redis:
    image: redis:alpine
    ports:
    - "6379:6379"
    volumes:
     - "/home/deploy/v/redis:/data"
    restart: always

  memcached:
    image: memcached
    ports:
      - "11211:11211"
    restart: always

  db:
    image: postgres:9.4
    volumes:
      - "/home/deploy/v/pgdata:/var/lib/postgresql/data"
    restart: always

2个回答



您的 docker-compose.yml </ code>文件说:</ p>

 < 代码>卷:
-/home/deploy/apps/myapp:/go/src/example.com/myapp
</ pre>

,这表示主机系统的源目录 已安装并隐藏Dockerfile构建的所有内容。 ./ myapp </ code>是 myapp </ code>可执行文件的主机的</ em>副本,如果有所不同(可能是MacOS或Windows主机),则会导致 此错误。</ p>

这是一种常见的解释型语言设置,开发人员希望在不运行正常的test-build-deploy序列的情况下运行其应用程序,但是对于 诸如Go之类的编译语言,您别无选择。 我会完全删除该块。</ p>
</ div>

展开原文

原文

Your docker-compose.yml file says:

volumes:
 - /home/deploy/apps/myapp:/go/src/example.com/myapp

which means your host system's source directory is mounted over, and hides, everything that the Dockerfile builds. ./myapp is the host's copy of the myapp executable and if something is different (maybe you have a MacOS or Windows host) that will cause this error.

This is a popular setup for interpreted languages where developers want to run their application without running a normal test-build-deploy sequence, but it doesn't really make sense for a compiled language like Go where you don't have a choice. I'd delete this block entirely.

duanchao4445
duanchao4445 谢谢大卫! 我直到凌晨2点才尝试解决这个问题。 唤醒并在您的帮助下在5分钟内使其工作。
一年多之前 回复



Go容器由于以下原因而停止运行:</ p>

  WORKDIR $ GOPATH / src  /example.com/myapp
RUN进行构建

#EXPOSE 80

#ENTRYPOINT $ GOPATH / src / example.com / myapp / myapp
ENTRYPOINT ./myapp

</ pre>

您正在将目录切换到构建应用程序的 $ GOPATH / src / example.com / myapp </ em>,但是,进入点</ em>指向 错误的位置。 </ p>

要解决此问题,您可以将应用复制到根目录并保留相同的 ENTRYPOINT </ em>命令,或者将应用复制到其他位置并传递完整 路径,例如:</ p>

  ENTRYPOINT / my / go / app / location 
</ code> </ pre>
</ div>

展开原文

原文

The Go container stops running because of this:

WORKDIR $GOPATH/src/example.com/myapp
RUN go build

#EXPOSE 80

#ENTRYPOINT $GOPATH/src/example.com/myapp/myapp
ENTRYPOINT ./myapp

You are switching directories to $GOPATH/src/example.com/myapp where you build your app, however, your entry point is pointing to the wrong location.

To solve this, you either copy the app into the root directory and keep the same ENTRYPOINT command or you copy the application to a different location and pass the full path such as:

ENTRYPOINT /my/go/app/location

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问