Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Fixes default Dockerfile user/workdir behavior (#1033)
Mish Ushakov8be34f8344e4
diff --git a/.changeset/true-hands-type.md b/.changeset/true-hands-type.mdnew file mode 100644index 000000000..116e39da8--- /dev/null+++ b/.changeset/true-hands-type.md@@ -0,0 +1,6 @@+---+'@e2b/python-sdk': minor+'e2b': minor+---++keep Docker WORKDIR and USER if specifieddiff --git a/packages/cli/tests/commands/template/fixtures/complex-python/expected/python-async/template.py b/packages/cli/tests/commands/template/fixtures/complex-python/expected/python-async/template.pyindex 984723c2c..d38ee700b 100644--- a/packages/cli/tests/commands/template/fixtures/complex-python/expected/python-async/template.py+++ b/packages/cli/tests/commands/template/fixtures/complex-python/expected/python-async/template.py@@ -16,7 +16,5 @@ template = (.run_cmd("pip install --upgrade pip && pip install -r requirements.txt").copy("app.py", ".").set_user("appuser")- .set_user("user")- .set_workdir("/home/user").set_start_cmd("sudo gunicorn --bind 0.0.0.0:8000 app:application", "sleep 20"))\ No newline at end of filediff --git a/packages/cli/tests/commands/template/fixtures/complex-python/expected/python-sync/template.py b/packages/cli/tests/commands/template/fixtures/complex-python/expected/python-sync/template.pyindex 07390c2e4..9c73b9ca2 100644--- a/packages/cli/tests/commands/template/fixtures/complex-python/expected/python-sync/template.py+++ b/packages/cli/tests/commands/template/fixtures/complex-python/expected/python-sync/template.py@@ -16,7 +16,5 @@ template = (.run_cmd("pip install --upgrade pip && pip install -r requirements.txt").copy("app.py", ".").set_user("appuser")- .set_user("user")- .set_workdir("/home/user").set_start_cmd("sudo gunicorn --bind 0.0.0.0:8000 app:application", "sleep 20"))\ No newline at end of filediff --git a/packages/cli/tests/commands/template/fixtures/complex-python/expected/typescript/template.ts b/packages/cli/tests/commands/template/fixtures/complex-python/expected/typescript/template.tsindex 0da2a6e8a..885afe531 100644--- a/packages/cli/tests/commands/template/fixtures/complex-python/expected/typescript/template.ts+++ b/packages/cli/tests/commands/template/fixtures/complex-python/expected/typescript/template.ts@@ -15,6 +15,4 @@ export const template = Template().runCmd('pip install --upgrade pip && pip install -r requirements.txt').copy('app.py', '.').setUser('appuser')- .setUser('user')- .setWorkdir('/home/user')- .setStartCmd('sudo gunicorn --bind 0.0.0.0:8000 app:application', 'sleep 20')\ No newline at end of file+ .setStartCmd('sudo gunicorn --bind 0.0.0.0:8000 app:application', 'sleep 20')diff --git a/packages/cli/tests/commands/template/fixtures/custom-commands/expected/python-async/template.py b/packages/cli/tests/commands/template/fixtures/custom-commands/expected/python-async/template.pyindex 4f8f4df7c..03a4e22e5 100644--- a/packages/cli/tests/commands/template/fixtures/custom-commands/expected/python-async/template.py+++ b/packages/cli/tests/commands/template/fixtures/custom-commands/expected/python-async/template.py@@ -8,6 +8,5 @@ template = (.set_workdir("/app").copy("server.js", ".").set_user("user")- .set_workdir("/home/user").set_start_cmd("sudo node server.js", "curl -f http://localhost:3000/health || exit 1"))\ No newline at end of filediff --git a/packages/cli/tests/commands/template/fixtures/custom-commands/expected/python-sync/template.py b/packages/cli/tests/commands/template/fixtures/custom-commands/expected/python-sync/template.pyindex 29c882226..3d155f65e 100644--- a/packages/cli/tests/commands/template/fixtures/custom-commands/expected/python-sync/template.py+++ b/packages/cli/tests/commands/template/fixtures/custom-commands/expected/python-sync/template.py@@ -8,6 +8,5 @@ template = (.set_workdir("/app").copy("server.js", ".").set_user("user")- .set_workdir("/home/user").set_start_cmd("sudo node server.js", "curl -f http://localhost:3000/health || exit 1"))\ No newline at end of filediff --git a/packages/cli/tests/commands/template/fixtures/custom-commands/expected/typescript/template.ts b/packages/cli/tests/commands/template/fixtures/custom-commands/expected/typescript/template.tsindex b742d2237..dcf84f4bb 100644--- a/packages/cli/tests/commands/template/fixtures/custom-commands/expected/typescript/template.ts+++ b/packages/cli/tests/commands/template/fixtures/custom-commands/expected/typescript/template.ts@@ -7,5 +7,4 @@ export const template = Template().setWorkdir('/app').copy('server.js', '.').setUser('user')- .setWorkdir('/home/user').setStartCmd('sudo node server.js', 'curl -f http://localhost:3000/health || exit 1')\ No newline at end of filediff --git a/packages/cli/tests/commands/template/fixtures/multiple-env/expected/python-async/template.py b/packages/cli/tests/commands/template/fixtures/multiple-env/expected/python-async/template.pyindex 32f5a2c86..345b52559 100644--- a/packages/cli/tests/commands/template/fixtures/multiple-env/expected/python-async/template.py+++ b/packages/cli/tests/commands/template/fixtures/multiple-env/expected/python-async/template.py@@ -21,5 +21,4 @@ template = (}).set_workdir("/app").set_user("user")- .set_workdir("/home/user"))\ No newline at end of filediff --git a/packages/cli/tests/commands/template/fixtures/multiple-env/expected/python-sync/template.py b/packages/cli/tests/commands/template/fixtures/multiple-env/expected/python-sync/template.pyindex 24c442061..01eecfa4c 100644--- a/packages/cli/tests/commands/template/fixtures/multiple-env/expected/python-sync/template.py+++ b/packages/cli/tests/commands/template/fixtures/multiple-env/expected/python-sync/template.py@@ -21,5 +21,4 @@ template = (}).set_workdir("/app").set_user("user")- .set_workdir("/home/user"))\ No newline at end of filediff --git a/packages/cli/tests/commands/template/fixtures/multiple-env/expected/typescript/template.ts b/packages/cli/tests/commands/template/fixtures/multiple-env/expected/typescript/template.tsindex 5d32a2df0..8c30525c2 100644--- a/packages/cli/tests/commands/template/fixtures/multiple-env/expected/typescript/template.ts+++ b/packages/cli/tests/commands/template/fixtures/multiple-env/expected/typescript/template.ts@@ -19,5 +19,4 @@ export const template = Template()'SINGLE_VAR': 'single_value',}).setWorkdir('/app')- .setUser('user')- .setWorkdir('/home/user')\ No newline at end of file+ .setUser('user')\ No newline at end of filediff --git a/packages/cli/tests/commands/template/fixtures/start-cmd/expected/python-async/template.py b/packages/cli/tests/commands/template/fixtures/start-cmd/expected/python-async/template.pyindex bc9ccdab6..4b14d3da7 100644--- a/packages/cli/tests/commands/template/fixtures/start-cmd/expected/python-async/template.py+++ b/packages/cli/tests/commands/template/fixtures/start-cmd/expected/python-async/template.py@@ -12,6 +12,5 @@ template = ("PYTHONUNBUFFERED": "1",}).set_user("user")- .set_workdir("/home/user").set_start_cmd("sudo node server.js", "sleep 20"))\ No newline at end of filediff --git a/packages/cli/tests/commands/template/fixtures/start-cmd/expected/python-sync/template.py b/packages/cli/tests/commands/template/fixtures/start-cmd/expected/python-sync/template.pyindex bffafc019..aa2d9ac1f 100644--- a/packages/cli/tests/commands/template/fixtures/start-cmd/expected/python-sync/template.py+++ b/packages/cli/tests/commands/template/fixtures/start-cmd/expected/python-sync/template.py@@ -12,6 +12,5 @@ template = ("PYTHONUNBUFFERED": "1",}).set_user("user")- .set_workdir("/home/user").set_start_cmd("sudo node server.js", "sleep 20"))\ No newline at end of filediff --git a/packages/cli/tests/commands/template/fixtures/start-cmd/expected/typescript/template.ts b/packages/cli/tests/commands/template/fixtures/start-cmd/expected/typescript/template.tsindex 9baa5da3a..386041e86 100644--- a/packages/cli/tests/commands/template/fixtures/start-cmd/expected/typescript/template.ts+++ b/packages/cli/tests/commands/template/fixtures/start-cmd/expected/typescript/template.ts@@ -11,5 +11,4 @@ export const template = Template()'PYTHONUNBUFFERED': '1',}).setUser('user')- .setWorkdir('/home/user').setStartCmd('sudo node server.js', 'sleep 20')\ No newline at end of filediff --git a/packages/js-sdk/src/template/dockerfileParser.ts b/packages/js-sdk/src/template/dockerfileParser.tsindex 8a94692cc..15f66fc55 100644--- a/packages/js-sdk/src/template/dockerfileParser.ts+++ b/packages/js-sdk/src/template/dockerfileParser.ts@@ -86,6 +86,8 @@ export function parseDockerfile(const fromInstruction = fromInstructions[0]const argumentsData = fromInstruction.getArguments()let baseImage = 'e2bdev/base' // default fallback+ let userChanged = false+ let workdirChanged = falseif (argumentsData && argumentsData.length > 0) {baseImage = argumentsData[0].getValue()}@@ -114,10 +116,12 @@ export function parseDockerfile(case 'WORKDIR':handleWorkdirInstruction(instruction, templateBuilder)+ workdirChanged = truebreakcase 'USER':handleUserInstruction(instruction, templateBuilder)+ userChanged = truebreakcase 'ENV':@@ -145,8 +149,12 @@ export function parseDockerfile(}// Set the user and workdir to the E2B defaults- templateBuilder.setUser('user')- templateBuilder.setWorkdir('/home/user')+ if (!userChanged) {+ templateBuilder.setUser('user')+ }+ if (!workdirChanged) {+ templateBuilder.setWorkdir('/home/user')+ }return {baseImage,diff --git a/packages/js-sdk/tests/template/methods/fromMethods.test.ts b/packages/js-sdk/tests/template/methods/fromMethods.test.tsindex 63161a1cc..48e2024eb 100644--- a/packages/js-sdk/tests/template/methods/fromMethods.test.ts+++ b/packages/js-sdk/tests/template/methods/fromMethods.test.ts@@ -1,8 +1,9 @@import { buildTemplateTest } from '../../setup'import { Template } from '../../../src'+import { InstructionType } from '../../../src/template/types'import path from 'node:path'import fs from 'node:fs'-import { afterAll, beforeAll } from 'vitest'+import { afterAll, beforeAll, assert } from 'vitest'const fileContextPath = path.join(__dirname, 'dockerfile-context')@@ -93,3 +94,55 @@ RUN npm install`const template = Template({ fileContextPath }).fromDockerfile(dockerfile)await buildTemplate(template, { skipCache: true })})++buildTemplateTest('fromDockerfile with default user and workdir', () => {+ const dockerfile = 'FROM node:24'+ const template = Template({ fileContextPath }).fromDockerfile(dockerfile)++ assert.equal(+ // @ts-expect-error - instructions is not a property of TemplateBuilder+ template.instructions[template.instructions.length - 2].type,+ InstructionType.USER+ )+ assert.equal(+ // @ts-expect-error - instructions is not a property of TemplateBuilder+ template.instructions[template.instructions.length - 2].args[0],+ 'user'+ )+ assert.equal(+ // @ts-expect-error - instructions is not a property of TemplateBuilder+ template.instructions[template.instructions.length - 1].type,+ InstructionType.WORKDIR+ )+ assert.equal(+ // @ts-expect-error - instructions is not a property of TemplateBuilder+ template.instructions[template.instructions.length - 1].args[0],+ '/home/user'+ )+})++buildTemplateTest('fromDockerfile with custom user and workdir', () => {+ const dockerfile = 'FROM node:24\nUSER mish\nWORKDIR /home/mish'+ const template = Template({ fileContextPath }).fromDockerfile(dockerfile)++ assert.equal(+ // @ts-expect-error - instructions is not a property of TemplateBuilder+ template.instructions[template.instructions.length - 2].type,+ InstructionType.USER+ )+ assert.equal(+ // @ts-expect-error - instructions is not a property of TemplateBuilder+ template.instructions[template.instructions.length - 2].args[0],+ 'mish'+ )+ assert.equal(+ // @ts-expect-error - instructions is not a property of TemplateBuilder+ template.instructions[template.instructions.length - 1].type,+ InstructionType.WORKDIR+ )+ assert.equal(+ // @ts-expect-error - instructions is not a property of TemplateBuilder+ template.instructions[template.instructions.length - 1].args[0],+ '/home/mish'+ )+})diff --git a/packages/python-sdk/e2b/template/dockerfile_parser.py b/packages/python-sdk/e2b/template/dockerfile_parser.pyindex 112bbc515..d93fef30b 100644--- a/packages/python-sdk/e2b/template/dockerfile_parser.py+++ b/packages/python-sdk/e2b/template/dockerfile_parser.py@@ -101,6 +101,9 @@ def parse_dockerfile(if " as " in base_image.lower():base_image = base_image.split(" as ")[0].strip()+ user_changed = False+ workdir_changed = False+# Set the user and workdir to the Docker defaultstemplate_builder.set_user("root")template_builder.set_workdir("/")@@ -119,8 +122,10 @@ def parse_dockerfile(_handle_copy_instruction(value, template_builder)elif instruction == "WORKDIR":_handle_workdir_instruction(value, template_builder)+ workdir_changed = Trueelif instruction == "USER":_handle_user_instruction(value, template_builder)+ user_changed = Trueelif instruction in ["ENV", "ARG"]:_handle_env_instruction(value, instruction, template_builder)elif instruction in ["CMD", "ENTRYPOINT"]:@@ -130,8 +135,10 @@ def parse_dockerfile(continue# Set the user and workdir to the E2B defaults- template_builder.set_user("user")- template_builder.set_workdir("/home/user")+ if not user_changed:+ template_builder.set_user("user")+ if not workdir_changed:+ template_builder.set_workdir("/home/user")return base_imagediff --git a/packages/python-sdk/tests/async/template_async/methods/test_from_methods.py b/packages/python-sdk/tests/async/template_async/methods/test_from_methods.pyindex 98cbf4fa7..90b8d077a 100644--- a/packages/python-sdk/tests/async/template_async/methods/test_from_methods.py+++ b/packages/python-sdk/tests/async/template_async/methods/test_from_methods.py@@ -6,6 +6,7 @@ import jsonimport pytestfrom e2b import AsyncTemplate+from e2b.template.types import InstructionType@pytest.fixture(scope="module")@@ -111,3 +112,31 @@ RUN npm install"""file_context_path=setup_dockerfile_context).from_dockerfile(dockerfile)await async_build(template, skip_cache=True)++[email protected]_debug()+async def test_from_dockerfile_with_default_user_and_workdir(setup_dockerfile_context):+ dockerfile = "FROM node:24"++ template = AsyncTemplate(+ file_context_path=setup_dockerfile_context+ ).from_dockerfile(dockerfile)++ assert template._template._instructions[-2]["type"] == InstructionType.USER+ assert template._template._instructions[-2]["args"][0] == "user"+ assert template._template._instructions[-1]["type"] == InstructionType.WORKDIR+ assert template._template._instructions[-1]["args"][0] == "/home/user"++[email protected]_debug()+async def test_from_dockerfile_with_custom_user_and_workdir(setup_dockerfile_context):+ dockerfile = "FROM node:24\nUSER mish\nWORKDIR /home/mish"++ template = AsyncTemplate(+ file_context_path=setup_dockerfile_context+ ).from_dockerfile(dockerfile)++ assert template._template._instructions[-2]["type"] == InstructionType.USER+ 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"diff --git a/packages/python-sdk/tests/sync/template_sync/methods/test_from_methods.py b/packages/python-sdk/tests/sync/template_sync/methods/test_from_methods.pyindex 4ef27de48..2a03b98fe 100644--- a/packages/python-sdk/tests/sync/template_sync/methods/test_from_methods.py+++ b/packages/python-sdk/tests/sync/template_sync/methods/test_from_methods.py@@ -6,6 +6,7 @@ import jsonimport pytestfrom e2b import Template+from e2b.template.types import InstructionType@pytest.fixture(scope="module")@@ -112,3 +113,31 @@ RUN npm install"""dockerfile)build(template, skip_cache=True)++[email protected]_debug()+def test_from_dockerfile_with_default_user_and_workdir(setup_dockerfile_context):+ dockerfile = "FROM node:24"++ template = Template(file_context_path=setup_dockerfile_context).from_dockerfile(+ dockerfile+ )++ assert template._template._instructions[-2]["type"] == InstructionType.USER+ assert template._template._instructions[-2]["args"][0] == "user"+ assert template._template._instructions[-1]["type"] == InstructionType.WORKDIR+ assert template._template._instructions[-1]["args"][0] == "/home/user"++[email protected]_debug()+def test_from_dockerfile_with_custom_user_and_workdir(setup_dockerfile_context):+ dockerfile = "FROM node:24\nUSER mish\nWORKDIR /home/mish"++ template = Template(file_context_path=setup_dockerfile_context).from_dockerfile(+ dockerfile+ )++ assert template._template._instructions[-2]["type"] == InstructionType.USER+ 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"