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

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

报告相同问题?