GoogleContainerTools / kaniko

Build Container Images In Kubernetes
Apache License 2.0
14.85k stars 1.44k forks source link

panic: runtime error: index out of range [0] with length 0 #1271

Open slayer opened 4 years ago

slayer commented 4 years ago

Need to check slice size https://github.com/GoogleContainerTools/kaniko/blob/master/pkg/executor/build.go#L516

 $ /kaniko/executor --dockerfile docker/Dockerfile-release --cache=true --context . --destination "${DOCKER_IMAGE?}-release"
 INFO[0000] Using dockerignore file: /builds/xxx/yyy/.dockerignore 
 panic: runtime error: index out of range [0] with length 0
 goroutine 1 [running]:
 github.com/GoogleContainerTools/kaniko/pkg/executor.CalculateDependencies(0xc0008fe090, 0x1, 0x1, 0x3133c00, 0xc0008a1530, 0x0, 0x1, 0xc0008fe090)
    /go/src/github.com/GoogleContainerTools/kaniko/pkg/executor/build.go:516 +0xfd4
 github.com/GoogleContainerTools/kaniko/pkg/executor.DoBuild(0x3133c00, 0x1, 0x0, 0x0, 0x7)
    /go/src/github.com/GoogleContainerTools/kaniko/pkg/executor/build.go:593 +0x241
 github.com/GoogleContainerTools/kaniko/cmd/executor/cmd.glob..func2(0x311c000, 0xc000146c40, 0x0, 0x7)
    /go/src/github.com/GoogleContainerTools/kaniko/cmd/executor/cmd/root.go:107 +0x12b
 github.com/spf13/cobra.(*Command).execute(0x311c000, 0xc00003a090, 0x7, 0x7, 0x311c000, 0xc00003a090)
    /go/src/github.com/GoogleContainerTools/kaniko/vendor/github.com/spf13/cobra/command.go:830 +0x29d
 github.com/spf13/cobra.(*Command).ExecuteC(0x311c000, 0x0, 0x1a8f8a0, 0xc000042178)
    /go/src/github.com/GoogleContainerTools/kaniko/vendor/github.com/spf13/cobra/command.go:914 +0x2fb
 github.com/spf13/cobra.(*Command).Execute(...)
    /go/src/github.com/GoogleContainerTools/kaniko/vendor/github.com/spf13/cobra/command.go:864
 main.main()
    /go/src/github.com/GoogleContainerTools/kaniko/cmd/executor/main.go:26 +0x2d
tejal29 commented 4 years ago

hmm this should not typically happen. Can you share your dockerfile.

slayer commented 4 years ago

Yes, actually it was happen when FROM is not defined properly

Dockerfile

ARG IMAGE
FROM ${IMAGE}

and additional --build-arg IMAGE=${DOCKER_IMAGE} solves the problem but IMO error message can be more helpful

Tazminia commented 4 years ago

@tejal29 should I take a look at this ?

runzexia commented 4 years ago

I also encountered the same problem. my dockerfile is

FROM debian as debian
RUN \
  export DEBIAN_FRONTEND=noninteractive &&\
  apt-get update &&\
  apt-get install -qq bash-static

FROM golang:1.13-alpine as builder
WORKDIR /workspace
COPY / /workspace/
RUN CGO_ENABLED=0 go install .

FROM alpine
ENTRYPOINT ["/khronos/khronos"]
EXPOSE 8080
COPY --from=debian /bin/bash-static /khronos/bash
COPY --from=builder /go/bin/khronos /khronos/
tejal29 commented 4 years ago

@tejal29 should I take a look at this ?

Please do @Tazminia . Would appreciate any help here.

iclass-brian commented 4 years ago

I also encountered the same problem. my dockerfile is

FROM debian as debian
RUN \
  export DEBIAN_FRONTEND=noninteractive &&\
  apt-get update &&\
  apt-get install -qq bash-static

FROM golang:1.13-alpine as builder
WORKDIR /workspace
COPY / /workspace/
RUN CGO_ENABLED=0 go install .

FROM alpine
ENTRYPOINT ["/khronos/khronos"]
EXPOSE 8080
COPY --from=debian /bin/bash-static /khronos/bash
COPY --from=builder /go/bin/khronos /khronos/

maybe the following line is the reason?

FROM debian as debian

In my case, it works after I change FROM composer as composer to FROM composer to composerStage

gstolz commented 3 years ago

I'm having the same problem. Using ARG reference in multi-stage seems to be the problem:

Dockerfile example:

FROM golang:1.15 as build ... ARG BASE_IMAGE=ghcr.io/ironpeakservices/iron-scratch/iron-scratch:1.0.1 FROM ${BASE_IMAGE} ...

Log: INFO[0001] Retrieving image golang:1.15 panic: runtime error: index out of range [1] with length 1 goroutine 1 [running]: github.com/GoogleContainerTools/kaniko/pkg/executor.CalculateDependencies(0xc000690900, 0x2, 0x2, 0x334ef00, 0xc0007dec60, 0x0, 0x0, 0x0) /go/src/github.com/GoogleContainerTools/kaniko/pkg/executor/build.go:506 +0xfd4 github.com/GoogleContainerTools/kaniko/pkg/executor.DoBuild(0x334ef00, 0x1, 0x0, 0x0, 0xc00059a280) /go/src/github.com/GoogleContainerTools/kaniko/pkg/executor/build.go:584 +0x268 github.com/GoogleContainerTools/kaniko/cmd/executor/cmd.glob..func2(0x3336bc0, 0xc000156000, 0x1, 0xe) /go/src/github.com/GoogleContainerTools/kaniko/cmd/executor/cmd/root.go:109 +0x11c github.com/spf13/cobra.(*Command).execute(0x3336bc0, 0xc00004c100, 0xe, 0xe, 0x3336bc0, 0xc00004c100) /go/src/github.com/GoogleContainerTools/kaniko/vendor/github.com/spf13/cobra/command.go:846 +0x29d github.com/spf13/cobra.(*Command).ExecuteC(0x3336bc0, 0x0, 0x1b78c20, 0xc00005a2f8) /go/src/github.com/GoogleContainerTools/kaniko/vendor/github.com/spf13/cobra/command.go:950 +0x349 github.com/spf13/cobra.(*Command).Execute(...) /go/src/github.com/GoogleContainerTools/kaniko/vendor/github.com/spf13/cobra/command.go:887 main.main() /go/src/github.com/GoogleContainerTools/kaniko/cmd/executor/main.go:26 +0x2d

It would be very helpful if this could be fixed, since this seems to be the easiest solution to setup hardened production images as well as comprehensive debugging images with just one Dockerfile!

gstolz commented 3 years ago

Sorry to say this, but in my case it was not a problem of Kaniko! I just did not properly specify the ARG in Dockerfile! In multi-layer Dockerfile the scope needs to be taken care of as described in the docs and a Stackoverflow article: https://stackoverflow.com/questions/53681522/multi-stage-dockerfile-arg-before-from-not-substituted https://docs.docker.com/engine/reference/builder/#arg https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact

dankeder commented 3 years ago

I encountered the same problem as well. In my case the problem was the FROM statement:

FROM nginx as nginx

A workaround that helped in my case was to use a tagged image:

FROM nginx:latest as nginx
neumantm commented 3 years ago

I have the same problem. I'm using FROM composer as composer The workaround mentioned by @dankeder worked, but I think this should be fixed.

vijal-patel commented 2 years ago

is this still an issue? I'm happy to work on it

dimaulupov commented 1 year ago

It is still happening. If you have multi-stage build and have this:

FROM golang as golang

kaniko fails with this error.

But this line:

FROM golang as builder

works fine

ashish1099 commented 12 months ago

I'm still seeing this issue, even without multistage build. This is my Dockerfile, and I have tried the latest 1.18.0 and 1.9.1 (debug) images

FROM example.com/jammy-generic-build:1.0.0
MAINTAINER Ashish Jaiswal <ashish@enableit.dk>

WORKDIR /tmp

RUN apt-get update \
    && \
    DEBIAN_FRONTEND=noninteractive \
    apt-get install -y \
    pandoc \
    texlive-xetex \
    && \
    apt-get autoremove -y \
    && \
    apt-get autoclean -y \

ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

RUN rm -fr /var/lib/apt/lists/*
bouaouda-achraf commented 1 month ago

Reproductible with : --build-arg "XX=YY" instead of : --build-arg XX=YY