dongtou5557 2019-03-11 05:10
浏览 209
已采纳

如何在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

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义