duanmei1930
2018-01-08 04:12 阅读 204

是否可以通过docker golang:alpine image构建静态的sqlite Go应用程序?

I'm trying to build my app which contains go-sqlite3 by docker.

main.go

package main

import (
        "database/sql"

        _ "github.com/mattn/go-sqlite3"
)

func main() {
        sql.Open("sqlite3", "test.db")
}

Dockerfile

FROM golang:alpine
RUN apk add --no-cache git
RUN apk add --no-cache sqlite-libs sqlite-dev
RUN apk add --no-cache build-base
WORKDIR /go/src/app
COPY *.go ./
RUN go-wrapper download
RUN go-wrapper install

I use this command to copy the compiled program out to my host OS (Arch Linux).

docker build -t mygo .
docker run --rm -v $$PWD:/usr/src/app mygo /bin/cp /go/bin/app /usr/src/app

The problem is the compiled program is missing dynamic library

$  ldd app
   ...
   libc.musl-x86_64.so.1 => not found

What I expected is like

$  ldd app
not a dynamic executable
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    doujie4050 doujie4050 2018-01-08 06:16

    refer to how to create a statically linked golang executable with go 1.5+

    Change the Dockerfile's go-wrapper install line into

    RUN go-wrapper install -ldflags "-linkmode external -extldflags -static"   
    
    点赞 评论 复制链接分享
  • dongtou2097 dongtou2097 2018-01-08 12:46

    If I correctly understood your problem it depends on how you build the Golang executable.

    You should build your package with CGO_ENABLED set to 0. In order to do that, before compiling, simply run export CGO_ENABLED=0 on your terminal than build your Golang binary.

    If you are interested you can find more info about CGO here

    点赞 评论 复制链接分享

相关推荐