GoogleContainerTools / kaniko

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

build with env, and COPY files #1549

Open valador opened 3 years ago

valador commented 3 years ago

Actual behavior kaniko do not provide env in build stage? how to copy files in container?

executor:latest:

INFO[0037] Unpacking rootfs as cmd COPY ./scripts/download.sh /download.sh requires it. 
INFO[0038] ARG C_SITE_USERNAME                          
INFO[0038] ARG C_SITE_PASSWORD                          
INFO[0038] ARG C_VERSION                                
INFO[0038] ENV installer_type=server                    
error building image: error building stage: failed to get files used from context: failed to get fileinfo for /workspace/workspace/scripts/download.sh: lstat /workspace/workspace/scripts/download.sh: no such file or directory

download.sh file in /workspace/scripts/download.sh if i add COPY /workspace/scripts/download.sh /download.sh error:

error building image: error building stage: failed to get files used from context: failed to get fileinfo for /workspace/workspace/workspace/scripts/download.sh: lstat /workspace/workspace/workspace/scripts/download.sh: no such file or directory

why 2 and 3 workspace path?

executor:debug: C_SITE_USERNAME not set download.sh success copied in container with COPY ./scripts/download.sh /download.sh and run, but do not have env if i add RUN env id Dockerfile - have only installer_type=server

i try add

ENV C_SITE_USERNAME=${C_SITE_USERNAME}
ENV C_SITE_PASSWORD=${C_SITE_PASSWORD}
ENV C_VERSION=${C_VERSION}

no any effect i try delete ARG - no any effect i try delete all from Dockerfile - and only stay

RUN apk --no-cache add bash curl grep \
  && chmod +x /download.sh \
 && env

no any effect, env do not have.

If i exec in debug container and start env - env have, but no in Dockerfile.

Expected behavior env in container copy my custom files in container

Dockerfile:

FROM alpine:latest

ARG C_SITE_USERNAME
ARG C_SITE_PASSWORD
ARG C_VERSION
ENV installer_type=server

COPY ./scripts/download.sh /download.sh

WORKDIR /tmp

RUN apk --no-cache add bash curl grep \
  && chmod +x /download.sh \
  && sync; /download.sh \
  && for file in *.tar.gz; do tar -zxf "$file"; done \
  && rm -rf *.tar.gz

yml:

apiVersion: v1
kind: Pod
metadata:
  name: dev-build-test-cont
  namespace: playground
spec:
  restartPolicy: Never
  containers:
  - name: dev-test-cont
    image: gcr.io/kaniko-project/executor:debug
    #image: gcr.io/kaniko-project/executor:latest
    imagePullPolicy: Always
    # lifecycle:
    #   preStop:
    #     exec:
    #       command: ["/bin/sh","-c","cat $STACKLOG_PATH"]
    # env:
    #   - name: STACKLOG_PATH
    #     value: /workspace/kaniko.slog
    env:
      - name: C_VERSION
        valueFrom:
          configMapKeyRef:
            name: dev-config-soft-version
            key: C_VERSION
      - name: C_SITE_USERNAME
        valueFrom:
          secretKeyRef:
            name: pw-site
            key: C_SITE_USERNAME
      - name: C_SITE_PASSWORD
        valueFrom:
          secretKeyRef:
            name: pw-site
            key: C_SITE_PASSWORD
    args: ["--dockerfile=/workspace/Dockerfile",
            "--context=dir://workspace",
            "--destination=docker-registry.registry.svc.cluster.local:5000/dev-test/test-cont:1.0",
            "--cache=false"]
    volumeMounts:
      - name: build-context
        mountPath: /workspace
  volumes:
    - name: build-context
      persistentVolumeClaim:
        claimName: build-context-claim
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: context
  namespace: playground
  labels:
    type: local
spec:
  capacity:
    storage: 3Gi
  accessModes:
    - ReadOnlyMany
  storageClassName: context-storage 
  hostPath:
    path: "/home/user/project/test/src"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: build-context-claim
  namespace: playground
spec:
  accessModes:
    - ReadOnlyMany
  resources:
    requests:
      storage: 2Gi
  storageClassName: context-storage
Description Yes/No
Please check if this a new feature you are proposing
  • - [ ]
Please check if the build works in docker but not in kaniko
  • - [ ]
Please check if this error is seen when you use --cache flag
  • - [ ]
Please check if your dockerfile is a multistage dockerfile
  • - [ ]
valador commented 3 years ago

solution for debug version of executor: add in yml build args:

"--build-arg=C_SITE_USERNAME",
"--build-arg=C_SITE_PASSWORD",
"--build-arg=C_VERSION"

in dockerfile:

ARG C_SITE_USERNAME
ARG C_SITE_PASSWORD
ARG C_VERSION
ENV C_SITE_USERNAME=${C_SITE_USERNAME}
ENV C_SITE_PASSWORD=${C_SITE_PASSWORD}
ENV C_VERSION=${C_VERSION}

Stil bug with COPY in release version of executor, copy not worked as it should.