How to install a golang package in a docker file?

huangapple go评论83阅读模式
英文:

How to install a golang package in a docker file?

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我是docker的新手,我想为我的Django应用程序设置一个docker-compose。在我的应用程序的后端中,我还有一些使用golang编写的包,并且使用subprocess库在Django中运行它们。

但是,当我想使用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

# 安装go包
RUN go install github.com/hakluke/hakrawler@latest \
 && cp $GOPATH/bin/hakrawler /usr/local/bin/

# 安装后端的主要镜像
FROM python:3.8.11-bullseye

# 设置环境变量
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# 安装Dist包
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/

# 设置工作目录
WORKDIR /usr/src/redteam_toolkit/

# 安装依赖
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt

# 复制项目,然后复制go包
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:

get.py文件位于/usr/src/redteam_toolkit/toolkit/scripts/webapp/目录下,用于仅运行go包并列出此目录中的文件:

import os
import subprocess

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(f"当前路径为:{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

输出结果为:

当前路径为:/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
英文:

I'm new in docker and I want to setting-up a docker-compose for my django app. in the backend of my app, I have golang packages too and run that in djang with subprocess library.
But, when I want to install a package using go install github.com/x/y@latest and then copy its binary to the project directory, it gives me the error: package github.com/x/y@latest: cannot use path@version syntax in GOPATH mode
I searched a lot in the internet but didn't find a solution to solve my problem. Could you please tell me where I'm wrong?

here is my 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:

the get.py file inside /usr/src/redteam_toolkit/toolkit/scripts/webapp/ directory, to just run the go package, and list files in this dir:

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")

and then I just run:

$ docker-compose up -d --build

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

The output is:

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

答案1

得分: 4

这看起来是一个非常适合多阶段构建的候选方案:

FROM golang:1.18.0 as go-build
       
# 安装包
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文件夹。删除该卷挂载以查看镜像的内容。

英文:

This looks like a really good candidate for a multi-stage build:

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/
...

Your compose file also needs to be updated, it is masking the entire /usr/src/redteam_toolkit folder with the volume mount. Delete that volume mount to see the content of the image.

答案2

得分: 0

GOPATH 模式在 Golang 模块中不起作用,在你的 Dockerfile 文件中添加以下内容:
RUN unset GOPATH

英文:

GOPATH mode does not work with Golang modules, in your Dockerfile file, add:
RUN unset GOPATH

答案3

得分: -1

使用 RUN go get <package_repository>

英文:

use RUN go get &lt;package_repository&gt;

huangapple
  • 本文由 发表于 2022年4月27日 03:34:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/72019587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定