dongtou5557
2019-03-11 05:10
浏览 179

如何在docker compose的帮助下正确设置golang应用程序的第三方库?

I am trying to deploy my Golang application with the help of docker compose.

In CentOS server the hierarchy of folders:

docker_app
 - src
   - app
     - docker-compose.yml
     - main.go

This Golang application use several third-party libraries:

  • Gorilla Mux;

  • Gorilla Handlers;

  • pq;

  • godotenv;

  • GORM;

  • goracle.

docker-compose.yml:

version: '2'

services:
  app:
    image: golang:1.11-alpine
    volumes:
      - .:/go/src/app
    working_dir: /go/src/app
    command: go run main.go

When I try to run docker-compose up project it raise error:

Attaching to app_app_1
app_1  | main.go:4:2: cannot find package "github.com/gorilla/handlers" in any of:
app_1  |      /usr/local/go/src/github.com/gorilla/handlers (from $GOROOT)
app_1  |      /go/src/github.com/gorilla/handlers (from $GOPATH)

As you can see I need to set up third-party libraries. How to make it correctly? Also how to set up the name of the future docker image and contaiener with the help of docker compose?

Is it possible to create go.mod file on Windows 10?

When I run $Env:GOOS = "linux"; $Env:GOARCH = "amd64"; go build command in Powershell it raise error:

enter image description here

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

我正尝试在docker compose的帮助下部署我的 Golang 应用程序。

在CentOS服务器中文件夹的层次结构:

  docker_app 
-src 
-app 
-docker-compose.yml 
  -main.go 
   
 
 

此Golang应用程序使用多个第三方库:

  • 大猩猩Mux;

  • 大猩猩处理程序;

  • pq;

  • godotenv;

  • GORM;

  • goracle

    docker-compose.yml

     版本:'2'
     \  nservices:
    应用程序:
    图像:golang:1.11-alpine 
    卷:
    -。:/ go / src / app 
     working_dir:/ go / src / app 
    命令:运行main.go \  n   
     
     

    当我尝试运行 docker-compose up 项目时,它会引发错误:

     附加到app_app_1 
    app_1 |  main.go:4:2:在以下任何位置都找不到软件包“ github.com/gorilla/handlers”:
    app_1 |  /usr/local/go/src/github.com/gorilla/handlers(来自$ GOROOT)
    app_1 |  /go/src/github.com/gorilla/handlers(来自$ GOPATH)
       
     
     

    如您所见,我需要设置第三方库。 如何正确制作? 还要如何在docker compose的帮助下设置未来的docker映像名称和contaiener?

    是否可以在<上创建 go.mod 文件 code> Windows 10

    当我运行 $ Env:GOOS =“ linux”; $ Env:GOARCH =“ amd64”; 在Powershell中执行构建命令会引发错误:

    ”在此处输入图片描述“

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanou9739 2019-03-11 05:39
    已采纳

    Is it possible to create go.mod file on Windows 10?

    Yes, as long as you have a Go 1.11/1.12

    But to use it with a docker-compose, you can follow "Go Docker dev environment with Go Modules and live code reloading" from Miłosz Smółka

    He uses one Dockerfile for compilation:

    FROM golang:1.11.2-stretch
    RUN go get github.com/cespare/reflex
    COPY reflex.conf /
    ENTRYPOINT ["reflex", "-c", "/reflex.conf"]
    

    Then a docker-compose for execution, mounting the compiled executable, *and the Go module cache:

    version: '3'
    services:
      publisher:
        build: .
        volumes:
          - ./publisher:/app
          - $GOPATH/pkg/mod/cache:/go/pkg/mod/cache
        working_dir: /app
        env_file:
          - .env
        ports:
          - 5000:5000
    

    Regarding the cross-compilation issue with go-goracle/goracle, issue 59 details:

    goracle needs CGO to compile in the Oracle OCI libs.
    So either a cross-compiling C toolchain is required (and special env vars set accordingly), or a native env (that's easier, IMHO).

    Meaning: don't try to cross-compile it from Windows, do it in a Dockerfile using directly the right OS (through a Linux VM, which is the case with Windows 10 HyperV environment).

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题