dstjh46606 2018-10-29 05:40
浏览 198
已采纳

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
  • 写回答

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条)

报告相同问题?

悬赏问题

  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题
  • ¥15 使用dify通过OpenAI 的API keys添加OpenAI模型时报了“Connection Error”错误
  • ¥40 想找个软件,但我不知道怎么找到它,我甚至不知道有没有
  • ¥15 怎么把60秒的视频时长改成显示0秒?且视频内容没有任何变化的正常播放?目的是为了解决一些平台对视频时长的要求,最好有自动处理的工具!