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

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

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算