`--target` builds unnecessary stages if they come first in the Dockerfile
Actual behavior
Given this Dockerfile:
FROM node:16.14.0-buster-slim AS client
RUN npm --version
FROM python:3.10-slim-buster AS api-base
RUN pip --version
It builds the client and api-base targets:
INFO[0000] Resolved base name node:16.14.0-buster-slim to client
INFO[0000] Resolved base name python:3.10-slim-buster to api-base
INFO[0000] Using dockerignore file: /workspace/.dockerignore
INFO[0000] Retrieving image manifest node:16.14.0-buster-slim
INFO[0000] Retrieving image node:16.14.0-buster-slim from registry index.docker.io
INFO[0001] Retrieving image manifest python:3.10-slim-buster
INFO[0001] Retrieving image python:3.10-slim-buster from registry index.docker.io
INFO[0002] Built cross stage deps: map[]
INFO[0002] Retrieving image manifest node:16.14.0-buster-slim
INFO[0002] Returning cached image manifest
INFO[0002] Executing 0 build triggers
INFO[0002] Building stage 'node:16.14.0-buster-slim' [idx: '0', base-idx: '-1']
INFO[0002] Unpacking rootfs as cmd RUN npm --version requires it.
INFO[0005] RUN npm --version
INFO[0005] Initializing snapshotter ...
INFO[0005] Taking snapshot of full filesystem...
INFO[0005] Cmd: /bin/sh
INFO[0005] Args: [-c npm --version]
INFO[0005] Running: [/bin/sh -c npm --version]
8.3.1
INFO[0005] Taking snapshot of full filesystem...
INFO[0005] Deleting filesystem...
INFO[0005] Retrieving image manifest python:3.10-slim-buster
INFO[0005] Returning cached image manifest
INFO[0005] Executing 0 build triggers
INFO[0005] Building stage 'python:3.10-slim-buster' [idx: '1', base-idx: '-1']
INFO[0005] Unpacking rootfs as cmd RUN pip --version requires it.
INFO[0008] RUN pip --version
INFO[0008] Initializing snapshotter ...
INFO[0008] Taking snapshot of full filesystem...
INFO[0008] Cmd: /bin/sh
INFO[0008] Args: [-c pip --version]
INFO[0008] Running: [/bin/sh -c pip --version]
pip 22.2.2 from /usr/local/lib/python3.10/site-packages/pip (python 3.10)
INFO[0009] Taking snapshot of full filesystem...
INFO[0009] Skipping push to container registry due to --no-push flag
Expected behavior
I would expect running kaniko with --target api-base to not build the client target like in does when using docker build.
To Reproduce Steps to reproduce the behavior:
- Create the Dockerfile as above.
docker run -v "$(pwd)":/workspace gcr.io/kaniko-project/executor:latest --dockerfile Dockerfile --destination myimage --no-push --target api-base --context dir:///workspace/ --cache=false
Additional Information
- Kaniko Image (fully qualified with digest): gcr.io/kaniko-project/executor:latest == gcr.io/kaniko-project/executor:12db9715709e
Triage Notes for the Maintainers
| 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 |
|
Thata a pitty, we should probably try to implement a proper DAG and detect if the first stage is even needed. I guess sofar this i snot implemented, right @imjasonh ?
I think for the stages it should be implemented already as its straight forward, maybe the more technical feature about parallelisation is more complex as it completes the DAG with all docker statements etc.
isn't this the reason we have --skip-unused-stages?
@salzig I just faced this "issue" today and it seems counterintuitive for me that --skip-unused-stages is not the default. IMO it should be the default and a --build-unused-stages flag or equivalent should be provided for people wanting unused stages.
In Podman and Docker, the default is to not build unused stages, that's why I thought it would be the default for kaniko. I understand kaniko does not necessarily follows Docker/Podman conventions but I think this one is more intuitive.
+1