Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Parse chown on Docker COPY (#1071)

Mish Ushakovdc54fe020080
diff --git a/.changeset/tiny-squids-check.md b/.changeset/tiny-squids-check.md
new file mode 100644
index 000000000..c6f454979
--- /dev/null
+++ b/.changeset/tiny-squids-check.md
@@ -0,0 +1,6 @@
+---
+'@e2b/python-sdk': patch
+'e2b': patch
+---
+
+parse --chown on COPY via fromDockerfile
diff --git a/packages/js-sdk/src/template/dockerfileParser.ts b/packages/js-sdk/src/template/dockerfileParser.ts
index 15f66fc55..97d54a572 100644
--- a/packages/js-sdk/src/template/dockerfileParser.ts
+++ b/packages/js-sdk/src/template/dockerfileParser.ts
@@ -3,6 +3,7 @@ import {
Argument,
DockerfileParser,
Instruction as DockerfileInstruction,
+ ModifiableInstruction,
} from 'dockerfile-ast'
import fs from 'node:fs'
import { ReadyCmd, waitForTimeout } from './readycmd'
@@ -111,7 +112,10 @@ export function parseDockerfile(
case 'COPY':
case 'ADD':
- handleCopyInstruction(instruction, templateBuilder)
+ handleCopyInstruction(
+ instruction as ModifiableInstruction,
+ templateBuilder
+ )
break
case 'WORKDIR':
@@ -175,14 +179,22 @@ function handleRunInstruction(
}
function handleCopyInstruction(
- instruction: DockerfileInstruction,
+ instruction: ModifiableInstruction,
templateBuilder: DockerfileParserInterface
): void {
const argumentsData = instruction.getArguments()
if (argumentsData && argumentsData.length >= 2) {
const src = argumentsData[0].getValue()
const dest = argumentsData[argumentsData.length - 1].getValue()
- templateBuilder.copy(src, dest)
+
+ let user: string | undefined
+ const flags = instruction.getFlags()
+ const chownFlag = flags.find((flag) => flag.getName() === 'chown')
+ if (chownFlag) {
+ user = chownFlag.getValue() ?? undefined
+ }
+
+ templateBuilder.copy(src, dest, { user })
}
}
diff --git a/packages/js-sdk/tests/template/methods/fromDockerfile.test.ts b/packages/js-sdk/tests/template/methods/fromDockerfile.test.ts
index 29fd68327..70e34f742 100644
--- a/packages/js-sdk/tests/template/methods/fromDockerfile.test.ts
+++ b/packages/js-sdk/tests/template/methods/fromDockerfile.test.ts
@@ -130,3 +130,27 @@ buildTemplateTest('fromDockerfile with custom user and workdir', () => {
'/home/mish'
)
})
+
+buildTemplateTest('fromDockerfile with COPY --chown', () => {
+ const dockerfile = `FROM node:24
+COPY --chown=myuser:mygroup app.js /app/
+COPY --chown=anotheruser config.json /config/`
+
+ const template = Template().fromDockerfile(dockerfile)
+
+ // First COPY instruction (after initial USER root and WORKDIR /)
+ // @ts-expect-error - instructions is not a property of TemplateBuilder
+ const copyInstruction1 = template.instructions[2]
+ assert.equal(copyInstruction1.type, InstructionType.COPY)
+ assert.equal(copyInstruction1.args[0], 'app.js')
+ assert.equal(copyInstruction1.args[1], '/app/')
+ assert.equal(copyInstruction1.args[2], 'myuser:mygroup') // user from --chown
+
+ // Second COPY instruction
+ // @ts-expect-error - instructions is not a property of TemplateBuilder
+ const copyInstruction2 = template.instructions[3]
+ assert.equal(copyInstruction2.type, InstructionType.COPY)
+ assert.equal(copyInstruction2.args[0], 'config.json')
+ assert.equal(copyInstruction2.args[1], '/config/')
+ assert.equal(copyInstruction2.args[2], 'anotheruser') // user from --chown (without group)
+})
diff --git a/packages/python-sdk/e2b/template/dockerfile_parser.py b/packages/python-sdk/e2b/template/dockerfile_parser.py
index e04f9d5bb..c03b16447 100644
--- a/packages/python-sdk/e2b/template/dockerfile_parser.py
+++ b/packages/python-sdk/e2b/template/dockerfile_parser.py
@@ -191,10 +191,20 @@ def _handle_copy_instruction(
if current_part:
parts.append(current_part)
- if len(parts) >= 2:
- src = parts[0]
- dest = parts[-1] # Last part is destination
- template_builder.copy(src, dest)
+ # Extract --chown flag and separate from paths
+ user = None
+ non_flag_parts = []
+ for part in parts:
+ if part.startswith("--chown="):
+ user = part[8:] # Extract value after "--chown="
+ elif not part.startswith("--"):
+ non_flag_parts.append(part)
+
+ if len(non_flag_parts) >= 2:
+ src = non_flag_parts[0]
+ dest = non_flag_parts[-1] # Last part is destination
+
+ template_builder.copy(src, dest, user=user)
def _handle_workdir_instruction(
diff --git a/packages/python-sdk/tests/async/template_async/methods/test_from_dockerfile.py b/packages/python-sdk/tests/async/template_async/methods/test_from_dockerfile.py
index 597704e92..426ed010c 100644
--- a/packages/python-sdk/tests/async/template_async/methods/test_from_dockerfile.py
+++ b/packages/python-sdk/tests/async/template_async/methods/test_from_dockerfile.py
@@ -64,3 +64,30 @@ async def test_from_dockerfile_with_custom_user_and_workdir():
assert template._template._instructions[-2]["args"][0] == "mish"
assert template._template._instructions[-1]["type"] == InstructionType.WORKDIR
assert template._template._instructions[-1]["args"][0] == "/home/mish"
+
+
+async def test_from_dockerfile_with_copy_chown():
+ dockerfile = """FROM node:24
+COPY --chown=myuser:mygroup app.js /app/
+COPY --chown=anotheruser config.json /config/"""
+
+ template = AsyncTemplate().from_dockerfile(dockerfile)
+
+ instructions = template._template._instructions
+
+ # First COPY instruction (after initial USER root and WORKDIR /)
+ copy_instruction1 = instructions[2]
+ assert copy_instruction1["type"] == InstructionType.COPY
+ assert copy_instruction1["args"][0] == "app.js"
+ assert copy_instruction1["args"][1] == "/app/"
+ assert copy_instruction1["args"][2] == "myuser:mygroup" # user from --chown
+
+ # Second COPY instruction
+ copy_instruction2 = instructions[3]
+ assert copy_instruction2["type"] == InstructionType.COPY
+ assert copy_instruction2["args"][0] == "config.json"
+ assert copy_instruction2["args"][1] == "/config/"
+ assert (
+ copy_instruction2["args"][2] == "anotheruser"
+ ) # user from --chown (without group)
diff --git a/packages/python-sdk/tests/sync/template_sync/methods/test_from_dockerfile.py b/packages/python-sdk/tests/sync/template_sync/methods/test_from_dockerfile.py
index 5cb68d302..b8ff1966e 100644
--- a/packages/python-sdk/tests/sync/template_sync/methods/test_from_dockerfile.py
+++ b/packages/python-sdk/tests/sync/template_sync/methods/test_from_dockerfile.py
@@ -64,3 +64,30 @@ def test_from_dockerfile_with_custom_user_and_workdir():
assert template._template._instructions[-2]["args"][0] == "mish"
assert template._template._instructions[-1]["type"] == InstructionType.WORKDIR
assert template._template._instructions[-1]["args"][0] == "/home/mish"
+
+
+def test_from_dockerfile_with_copy_chown():
+ dockerfile = """FROM node:24
+COPY --chown=myuser:mygroup app.js /app/
+COPY --chown=anotheruser config.json /config/"""
+
+ template = Template().from_dockerfile(dockerfile)
+
+ instructions = template._template._instructions
+
+ # First COPY instruction (after initial USER root and WORKDIR /)
+ copy_instruction1 = instructions[2]
+ assert copy_instruction1["type"] == InstructionType.COPY
+ assert copy_instruction1["args"][0] == "app.js"
+ assert copy_instruction1["args"][1] == "/app/"
+ assert copy_instruction1["args"][2] == "myuser:mygroup" # user from --chown
+
+ # Second COPY instruction
+ copy_instruction2 = instructions[3]
+ assert copy_instruction2["type"] == InstructionType.COPY
+ assert copy_instruction2["args"][0] == "config.json"
+ assert copy_instruction2["args"][1] == "/config/"
+ assert (
+ copy_instruction2["args"][2] == "anotheruser"
+ ) # user from --chown (without group)