skip to Main Content

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

3

Answers


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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. use RUN go get <package_repository>

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search