Golang Docker Engine SDK 不会安装在 Docker 中

问题描述 投票:0回答:1

我正在尝试使用Golang Docker Engine SDK,但安装它根本不起作用。我尝试了各种方法,但似乎没有任何效果。

当我尝试构建图像时,出现以下错误:

6.676 github.com/docker/docker/client imports
6.676   go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp imports
6.676   go.opentelemetry.io/otel/attribute imports
6.676   cmp: package cmp is not in GOROOT (/usr/lib/go-1.18/src/cmp)
6.676 github.com/docker/docker/client imports
6.676   go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp imports
6.676   go.opentelemetry.io/otel/attribute imports
6.676   slices: package slices is not in GOROOT (/usr/lib/go-1.18/src/slices)
6.676 go: downloading golang.org/x/net v0.24.0
------
Dockerfile:17
--------------------
  15 |     COPY go.sum ./
  16 |
  17 | >>> RUN go get -u github.com/docker/docker/client
  18 |
  19 |     COPY . .
--------------------
ERROR: failed to solve: process "/bin/sh -c go get -u github.com/docker/docker/client" did not complete successfully: exit code: 1

我像这样导入sdk:

import (
    "fmt"
    "log"
    "os"
    "os/exec"
    "strconv"

    "github.com/docker/docker/client"
)

这是我的 Dockerfile 的一部分:

FROM ubuntu:latest
WORKDIR /app

# Prep apt installs
ARG DEBIAN_FRONTEND=noninteractive
RUN dpkg --add-architecture i386
# Update apt
RUN apt-get update
RUN apt-get install -y wine32 xvfb winbind golang-go
# Download wine + fake monitor


COPY go.mod ./
COPY go.sum ./

RUN go get -u github.com/docker/docker/client

COPY . .

RUN go mod download

RUN go build -buildvcs=false

WORKDIR /app/
docker go
1个回答
0
投票

cmp
包是 Go 1.21 中的新功能。您的错误消息表明您使用的是 Go 1.18,它太旧了。

我会使用 Docker Hub

golang
图像 来编译您的应用程序,并使用已知版本的编译器,而不是依赖某些版本的 Ubuntu 将提供的工具集。例如,

FROM golang:1.22 AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -buildvcs=false -o app

FROM ubuntu
# RUN apt-get update \
#  && DEBIAN_FRONTEND=noninteractive \
#     apt-get install --no-install-recommends --assume-yes
#       ...
COPY --from=build /app/app /usr/local/bin
CMD ["app"]
© www.soinside.com 2019 - 2024. All rights reserved.