Skip to content

Commit

Permalink
fix: incorrect DOCKER_COPY_IMAGES for COPY --from=<index> (#479)
Browse files Browse the repository at this point in the history
It was not parsing whenever `--from` provided a stage index, not its
alias. In docker you can pass either:

```dockerfile
FROM alpine AS one
FROM alpine AS two
COPY --from=one /foo /bar
COPY --from=0 /foo /bar
```
  • Loading branch information
miki725 authored Feb 3, 2025
1 parent a5b3792 commit 06b9fa0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## On the `main` branch

### Fixes

- Incorrect base image for `DOCKER_COPY_IMAGES` when using stage index
(e.g. `COPY --from=<index>`).
([#479](https://github.com/crashappsec/chalk/pull/479))

## 0.5.2

**Jan 28, 2025**
Expand Down
4 changes: 3 additions & 1 deletion src/docker/dockerfile.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
## Dockerfile parsing

import std/[algorithm, sequtils, unicode]
import ".."/config
import ".."/[config, util]
import "."/[ids]

# RUN and COPY accept << and <<-
Expand Down Expand Up @@ -1068,6 +1068,8 @@ proc formatCopyImage(ctx: DockerInvocation, copy: CopyInfo): ChalkDict =
let image =
if copy.frm in ctx.dfSectionAliases:
ctx.getBaseDockerSection(ctx.dfSectionAliases[copy.frm]).image
elif isUInt(copy.frm) and parseInt(copy.frm) < len(ctx.dfSections):
ctx.getBaseDockerSection(ctx.dfSections[parseInt(copy.frm)]).image
else:
parseImage(copy.frm, defaultTag = "")
result = ChalkDict()
Expand Down
7 changes: 7 additions & 0 deletions src/util.nim
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,13 @@ proc isInt*(i: string): bool =
except:
return false

proc isUInt*(i: string): bool =
try:
discard parseUInt(i)
return true
except:
return false

proc splitBy*(s: string, sep: string, default: string = ""): (string, string) =
let parts = s.split(sep, maxsplit = 1)
if len(parts) == 2:
Expand Down
12 changes: 12 additions & 0 deletions tests/functional/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ def test_base_images(chalk: Chalk, random_hex: str, tmp_data_dir: Path):
FROM one as five
COPY --from=nginx:1.27.0@sha256:97b83c73d3165f2deb95e02459a6e905f092260cd991f4c4eae2f192ddb99cbe /usr/sbin/nginx /nginx
COPY --from=one /bin/sh /sh
COPY --from=0 /bin/ls /ls
FROM scratch as six
Expand Down Expand Up @@ -796,6 +797,17 @@ def test_base_images(chalk: Chalk, random_hex: str, tmp_data_dir: Path):
"src": ["/bin/sh"],
"dest": "/sh",
},
{
"from": "0",
"uri": re.compile("alpine@sha256:"),
"repo": "alpine",
"registry": "registry-1.docker.io",
"name": "library/alpine",
"tag": MISSING,
"digest": ANY,
"src": ["/bin/ls"],
"dest": "/ls",
},
],
"": [
{
Expand Down

0 comments on commit 06b9fa0

Please sign in to comment.