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 如何让子窗口鼠标滚动独立,不要传递消息给主窗口
  • ¥15 如何能达到用ping0.cc检测成这样?如图
  • ¥15 关于#DMA固件#的问题,请各位专家解答!
  • ¥15 matlab生成的x1图不趋于稳定,之后的图像是稳定的水平线
  • ¥15 请问华为OD岗位的内部职业发展通道都有哪些,以及各个级别晋升的要求
  • ¥20 微信小程序 canvas 问题
  • ¥15 系统 24h2 专业工作站版,浏览文件夹的图库,视频,图片之类的怎样删除?
  • ¥15 怎么把512还原为520格式
  • ¥15 MATLAB的动态模态分解出现错误,以CFX非定常模拟结果为快照
  • ¥15 求高通平台Softsim调试经验