dstjh46606
2018-10-29 05:40
浏览 190

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

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

图片转代码服务由CSDN问答提供 功能建议

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

我的Go项目的Dockerfile如下:

  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”] 
   
 
 

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

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

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

  docker run -it 75d4a95ef5ec 
   
 
 

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

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

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

我的docker-compose.yml看起来像:

 版本:“ 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“ 
重新启动:始终
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongyuhui0418 2018-10-29 10:24
    已采纳

    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.

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题