【问题标题】:How to build a go executable that doesn't link to musl libc如何构建不链接到 musl libc 的 go 可执行文件
【发布时间】:2019-02-04 21:46:27
【问题描述】:

所以:

官方的 Go 构建容器基于 Alpine。

Alpine 使用 musl 作为 libc 而不是 glibc。

我需要在一个可以在 Ubuntu 上运行的容器中构建一个 Go 可执行文件,该容器使用 glibc。

我该怎么做

  1. 使官方 GoLang 构建容器使用 glibc 或
  2. 在基于 Ubuntu 的容器上构建我的 GoLang 项目

我无法使用Disable CGO 解决方案,因为我的 Go 代码是 FUSE 驱动程序,需要 CGO

【问题讨论】:

  • hub.docker.com/r/frolvlad/alpine-glibc获取这个镜像作为基础镜像,在golang的官方dockerfile(github.com/docker-library/golang/blob/master/…)中运行同样的步骤来创建你的builder镜像,编译你的代码,获利。
  • 嗯,这是一个好的开始,但现在我必须配置该图像以准备构建我的 Go 代码...
  • “我必须配置该映像以准备构建我的 Go 代码”是的,您应该尝试该配置。
  • 其实golang:latest是基于Debian Stretch的。基本上,您需要做的就是在构建阶段的 FROM 指令中省略 alpine 修饰符。
  • 另一种选择是在 Ubuntu 上安装 musl-dev(并将其添加为应用程序的依赖项)

标签: docker go alpine


【解决方案1】:

从 Ubuntu 基础映像开始,并在其中设置 golang。在发现依赖项时添加依赖项。如果你不是很关心 go 的版本,那么 Ubuntu 的官方包就可以了,你不必下载官方的 go 版本。

这适用于我们(linux / mac 用户):

Dockerfile

FROM ubuntu:20.04 as base

ARG BUILDPLATFORM
ARG TARGETPLATFORM
RUN echo "Building for $TARGETPLATFORM on $BUILDPLATFORM"

# Enable super fast apt caches for use with buildkit --mount=type=cache
RUN rm -f /etc/apt/apt.conf.d/docker-clean; \
    echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache

# Install dependencies for building
# --mount docs: https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/syntax.md
RUN --mount=type=cache,sharing=locked,target=/var/cache/apt \
    --mount=type=cache,sharing=locked,target=/var/lib/apt \
    apt-get update && \
    DEBIAN_FRONTEND=noninteractive apt-get install -y \
    awscli \
    curl \
    gcc \
    git \
    liblxc-dev \
    liblxc1 \
    lxc-utils \
    make \
    parallel \
    pkg-config \
    upx-ucl \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

ARG GOVERSION=1.15
ARG TARGETARCH
ADD https://dl.google.com/go/go${GOVERSION}.linux-${TARGETARCH}.tar.gz /tmp/golang.tar.gz
RUN cd /opt && tar zxf /tmp/golang.tar.gz && rm /tmp/golang.tar.gz

# Set timezone to UTC
RUN echo 'UTC' > /etc/timezone && \
    dpkg-reconfigure -f noninteractive tzdata

# Set up go environment variables
ENV GOPATH=/opt/gopath
ENV GOCACHE=/opt/gocache
ENV GOROOT=/opt/go
ENV PATH="$GOROOT/bin/:$PATH"

# Install go-bindata, it is used when building drone
# go get installs to: $GOPATH/src and $GOPATH/bin
# We then move the binary to $GOROOT so we can change GOPATH in our builds
RUN go get -u github.com/go-bindata/go-bindata/... && \
    mv $GOPATH/bin/go-bindata $GOROOT/bin/go-bindata

# Install https://staticcheck.io for statistical analysis of go code
RUN go get honnef.co/go/tools/cmd/staticcheck && \
    mv $GOPATH/bin/staticcheck $GOROOT/bin/staticcheck

# Install https://github.com/kyoh86/richgo for pretty color output
RUN go get -u github.com/kyoh86/richgo && \
    mv $GOPATH/bin/richgo $GOROOT/bin/richgo

# Set up working dir for this project
WORKDIR /workspace

#########
# TESTS #
#########

# Install dependencies for running tests
RUN --mount=type=cache,sharing=locked,target=/var/cache/apt \
    --mount=type=cache,sharing=locked,target=/var/lib/apt \
    apt-get update && \
    DEBIAN_FRONTEND=noninteractive apt-get install -y \
    pigz \
    bind9-host \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# Setup git for tests
RUN git config --global user.email "test@project.com" && \
    git config --global user.name "Test Project"


FROM base as ci
ENV GOPATH=/workspace/.cache/gopath
ENV GOCACHE=/workspace/.cache/gocache

RUN mkdir -p /root/.parallel; touch /root/.parallel/will-cite


#############
# COPY CODE #
#############
#COPY go.mod go.sum /workspace/
#WORKDIR /workspace
#RUN go mod download
#COPY ./ /workspace/


# A note on folders
# /opt/go is where go itself is installed
# /opt/gopath is where `go get` installs packages
# /opt/gopath is also what `go mod` uses for package cache
# /builds is where the current project folder should be mounted

运行它:

docker.sh

docker build -q --target base -t ubuntu-go . ||
docker build    --target base -t ubuntu-go .

docker run -i -t --rm --init --env-file=.env \
  --volume /src/myproject:/workspace:delegated \
  --volume /src/myproject/.docker/gopath:/opt/gopath:delegated \
  --volume /src/myproject/.docker/gocache:/opt/gocache:delegated \
  ubuntu-go "$@"

例如

$ bash -x docker.sh bash -c 'seq 1 4 ; whoami'
+ docker build -q --target base -t ubuntu-go .
sha256:03eaf19625efd7f5760d14ea0d741d4454a9f280cd70c5c600bea63bbca70984
+ docker run -i -t --rm --init --env-file=.env \
  --volume /src/myproject:/workspace:delegated \
  --volume /src/myproject/.docker/gopath:/opt/gopath:delegated \
  --volume /src/myproject/.docker/gocache:/opt/gocache:delegated \
   ubuntu-go bash -c 'seq 1 4 ; whoami'
1
2
3
4
root

我们的 ldd 部门如下所示:

 linux-vdso.so.1 (…)
 libpthread.so.0 => /lib64/libpthread.so.0 (…)
 liblxc.so.1 => /usr/lib64/liblxc.so.1 (…)
 libutil.so.1 => /lib64/libutil.so.1 (…)
 libdl.so.2 => /lib64/libdl.so.2 (…)
 libc.so.6 => /lib64/libc.so.6 (…)
 /lib64/ld-linux-x86-64.so.2 (…)
 libcrypto.so.1.1 => /usr/lib64/libcrypto.so.1.1 (…)
 libseccomp.so.2 => /usr/lib64/libseccomp.so.2 (…)
 libcap.so.2 => /lib64/libcap.so.2 (…)
 libgcc_s.so.1 => /usr/lib/gcc/x86_64-pc-linux-gnu/9.4.0/libgcc_s.so.1 (…)

【讨论】:

    猜你喜欢
    • 2017-03-06
    • 1970-01-01
    • 2012-12-08
    • 2011-02-13
    • 2017-06-03
    • 2016-01-11
    • 2019-08-16
    • 2015-06-24
    • 2021-03-17
    相关资源
    最近更新 更多