kaniko icon indicating copy to clipboard operation
kaniko copied to clipboard

Unable to build image in monorepo.

Open ojasgo opened this issue 1 year ago • 2 comments

I want to build a Docker image using Kaniko. Currently I am using docker buildx and it works perfectly. I have following dir structure.

main 
├── Makefile
├── README.md
├── svc
├── OtherImportantFiles
├── RequiredForBuildingThisDockerImage
├── svc2
├── svc3
├── svc4

This is the Dockerfile I have.

FROM alpine as packages

WORKDIR /src/
RUN --mount=type=bind,target=/docker-context \
        cd /docker-context/; \
        find . -name "go.*" -mindepth 0 -maxdepth 4 -exec cp --parents "{}" /src/ \;

# ------------------------------------------------------------------------------------------
FROM golang:1.20-alpine as builder

ENV GOPROXY=https://proxy.golang.org

RUN mkdir -p /src
WORKDIR /src

# Copy the Go Modules manifests
COPY --from=packages /src/ .

# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN cd /src/svc; go mod download

ADD . .

RUN cd /src/svc; go mod tidy; go build -o /bin/app

# ------------------------------------------------------------------------------------------
FROM alpine
# RUN apk add --no-cache bash
RUN apk add --no-cache ca-certificates

WORKDIR /bin/

COPY --from=builder /bin/app .

CMD ["app"]

I am using following command to build the docker image.

docker run --rm \
-v ./:/kaniko/src/:rw \
gcr.io/kaniko-project/executor:debug \
--context dir:///kaniko/src/ \
--dockerfile svc/Dockerfile \
--no-push
--verbosity=trace

I am getting the following error message.

INFO[0012] Running: [/bin/sh -c cd /src/svc; go mod download] 
/bin/sh: cd: line 0: can't cd to /src/svc: No such file or directory
go: no modules specified (see 'go help mod download')
error building image: error building stage: failed to execute command: waiting for process to exit: exit status 1
zsh: command not found: --verbosity=trace

I think either I am passing the context incorrectly, or either monorepo or multi stage build is breaking Kaniko. Can you please suggest what might be going on here ?

ojasgo avatar Mar 11 '24 12:03 ojasgo

@ojasgo thanks for the question, would you mind elaborating your use case a bit more - is this meant to build an image at your local registry? May I ask what is this -v ./:/kaniko/src/:rw \ aiming to achieve?

JeromeJu avatar Mar 21 '24 15:03 JeromeJu

Hi @JeromeJu -v ./:/kaniko/src/:rw is mounting the current directory inside the kaniko container at the location /kaniko/src/. This way kaniko has access to everythign it needs to build the image.

ojasgo avatar Mar 26 '24 10:03 ojasgo