如何在 docker 文件中安装 golang 包?

How to install a golang package in a docker file?

我是 docker 的新手,我想为我的 django 应用程序设置一个 docker-compose。在我的应用程序的后端,我也有 golang 包和 运行 在带有子进程库的 djang 中。
但是,当我想使用 go install github.com/x/y@latest 安装包,然后将其二进制文件复制到项目目录时,出现错误:package github.com/x/y@latest: cannot use path@version syntax in GOPATH mode
我在互联网上搜索了很多但没有找到解决我的问题的方法。你能告诉我哪里错了吗?

这是我的 Dockerfile:

FROM golang:1.18.1-bullseye as go-build
       
# Install go package
RUN go install github.com/hakluke/hakrawler@latest \
 && cp $GOPATH/bin/hakrawler /usr/local/bin/
 
# Install main image for backend
FROM python:3.8.11-bullseye

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Install Dist packages
RUN apt-get update \
    && apt-get -y install --no-install-recommends software-properties-common libpq5 python3-dev musl-dev git netcat-traditional golang \
    && rm -rf /var/lib/apt/lists/

# Set work directory
WORKDIR /usr/src/redteam_toolkit/

# Install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt

# Copy project, and then the go package
COPY . .
COPY --from=go-build /usr/local/bin/hakrawler /usr/src/redteam_toolkit/toolkit/scripts/webapp/

docker-compose.yml:

version: '3.3'

services:
  webapp:
    build: .
    command: python manage.py runserver 0.0.0.0:4334
    container_name: toolkit_webapp
    volumes:
      - .:/usr/src/redteam_toolkit/
    ports:
      - 4334:4334
    env_file:
      - ./.env
    depends_on:
      - db

  db:
    image: postgres:13.4-bullseye
    container_name: database
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=redteam_toolkit_db

volumes:
  postgres_data:

/usr/src/redteam_toolkit/toolkit/scripts/webapp/ 目录中的 get.py 文件,只是 运行 go 包,并列出此目录中的文件:

import os
import subprocess

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(f"Current path is: {BASE_DIR}")

def go(target_url):
    run_go_package = subprocess.getoutput(
        f"echo {target_url} | {BASE_DIR}/webapp/hakrawler -t 15 -u"
    )
    list_files = subprocess.getoutput(f"ls {BASE_DIR}/webapp/")
    
    print(run_go_package)
    print(list_files)

go("https://example.org")

然后我只是 运行:

$ docker-compose up -d --build

$ docker-compose exec webapp python toolkit/scripts/webapp/get.py


输出为:

Current path is: /usr/src/redteam_toolkit/toolkit/scripts
/bin/sh: 1: /usr/src/redteam_toolkit/toolkit/scripts/webap/hakrawler: not found
__init__.py
__pycache__
scr.py
gather.py

GOPATH 模式不适用于 Golang 模块,在您的 Dockerfile 文件中,添加:
RUN unset GOPATH

这看起来非常适合 multi-stage 构建:

FROM golang:1.18.0 as go-build
       
# Install packages
RUN go install github.com/x/y@latest \
 && cp $GOPATH/bin/pacakge /usr/local/bin/
 
FROM python:3.8.11-bullseye as release
...
COPY --from=go-build /usr/local/bin/package /usr/src/toolkit/toolkit/scripts/webapp/
...

您的 compose 文件也需要更新,它用卷安装掩盖了整个 /usr/src/redteam_toolkit 文件夹。删除该卷安装以查看图像的内容。

使用RUN go get <package_repository>