File size: 8,118 Bytes
4b07aaf
 
 
 
 
 
85b7ac8
a7caaff
85b7ac8
 
 
 
 
 
 
 
 
 
4b07aaf
85b7ac8
4b07aaf
85b7ac8
 
 
 
 
 
4b07aaf
 
 
 
 
 
 
 
 
 
 
 
85b7ac8
 
 
 
 
4b07aaf
 
85b7ac8
 
 
 
 
 
4b07aaf
85b7ac8
 
4b07aaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85b7ac8
4b07aaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""Task 2: Dockerfile Runtime Errors — MEDIUM.

Agent fixes Dockerfiles that build successfully but fail at container
runtime: missing WORKDIR, CMD/ENTRYPOINT conflicts, permission issues,
and missing environment variables.
"""



from server.models import TaskDifficulty
from server.tasks.base import BaseTask


class DockerfileRuntimeTask(BaseTask):
    NAME = "Dockerfile Runtime Errors"
    DESCRIPTION = "Fix runtime/container execution issues in Dockerfiles"
    DIFFICULTY = TaskDifficulty.MEDIUM
    AVAILABLE_SECRETS = []

    SCENARIOS = [
        # Scenario 1: Missing WORKDIR — node module resolution fails at runtime
        {
            "id": "missing_workdir",
            "files": [
                {
                    "path": "Dockerfile",
                    "type": "dockerfile",
                    "content": (
                        "FROM node:18-alpine\n"
                        "COPY package*.json ./\n"
                        "RUN npm ci\n"
                        "COPY . .\n"
                        'CMD ["npm", "start"]'
                    ),
                },
                {
                    "path": "package.json",
                    "type": "other",
                    "content": '{"name": "app", "scripts": {"start": "node index.js"}}',
                },
            ],
            "error": {
                "phase": "docker_run",
                "message": "Error: Cannot find module '/package.json'",
                "exit_code": 1,
                "failed_step": "npm start",
            },
            "expected_fixes": [
                {
                    "file": "Dockerfile",
                    "type": "contains",
                    "expected": "WORKDIR /app",
                    "hint": "Set a working directory before COPY/RUN so files land in /app, not /",
                }
            ],
        },

        # Scenario 2: CMD and ENTRYPOINT both defined as full exec forms — conflict
        {
            "id": "cmd_entrypoint_conflict",
            "files": [
                {
                    "path": "Dockerfile",
                    "type": "dockerfile",
                    "content": (
                        "FROM python:3.11-slim\n"
                        "WORKDIR /app\n"
                        "COPY . .\n"
                        "RUN pip install -r requirements.txt\n"
                        'ENTRYPOINT ["python", "server.py"]\n'
                        'CMD ["python", "server.py"]'
                    ),
                },
                {
                    "path": "requirements.txt",
                    "type": "requirements",
                    "content": "flask==2.3.0",
                },
            ],
            "error": {
                "phase": "docker_run",
                "message": (
                    "container exits immediately; process started twice — "
                    "ENTRYPOINT and CMD both specify the full command"
                ),
                "exit_code": 1,
                "failed_step": "container start",
            },
            "expected_fixes": [
                {
                    "file": "Dockerfile",
                    "type": "not_contains",
                    "expected": 'CMD ["python", "server.py"]',
                    "hint": (
                        "When using ENTRYPOINT as a full command, CMD should provide "
                        "default arguments only, or be removed entirely"
                    ),
                }
            ],
        },

        # Scenario 3: Entrypoint script not executable
        {
            "id": "entrypoint_not_executable",
            "files": [
                {
                    "path": "Dockerfile",
                    "type": "dockerfile",
                    "content": (
                        "FROM python:3.11-slim\n"
                        "WORKDIR /app\n"
                        "COPY . .\n"
                        "RUN pip install -r requirements.txt\n"
                        'ENTRYPOINT ["./start.sh"]'
                    ),
                },
                {
                    "path": "requirements.txt",
                    "type": "requirements",
                    "content": "flask==2.3.0",
                },
                {
                    "path": "start.sh",
                    "type": "other",
                    "content": "#!/bin/bash\npython app.py",
                },
            ],
            "error": {
                "phase": "docker_run",
                "message": "exec ./start.sh: permission denied",
                "exit_code": 126,
                "failed_step": "ENTRYPOINT ./start.sh",
            },
            "expected_fixes": [
                {
                    "file": "Dockerfile",
                    "type": "contains",
                    "expected": "RUN chmod +x ./start.sh",
                    "hint": "The entrypoint script must be made executable with chmod +x before the ENTRYPOINT instruction",
                }
            ],
        },

        # Scenario 4: App crashes because a required ENV variable is missing
        {
            "id": "missing_required_env",
            "files": [
                {
                    "path": "Dockerfile",
                    "type": "dockerfile",
                    "content": (
                        "FROM python:3.11-slim\n"
                        "WORKDIR /app\n"
                        "COPY . .\n"
                        "RUN pip install -r requirements.txt\n"
                        "EXPOSE 8080\n"
                        'CMD ["python", "app.py"]'
                    ),
                },
                {
                    "path": "requirements.txt",
                    "type": "requirements",
                    "content": "flask==2.3.0\ngunicorn==21.2.0",
                },
            ],
            "error": {
                "phase": "docker_run",
                "message": (
                    "KeyError: 'DATABASE_URL'\n"
                    "Application requires DATABASE_URL environment variable to be set"
                ),
                "exit_code": 1,
                "failed_step": "python app.py",
            },
            "expected_fixes": [
                {
                    "file": "Dockerfile",
                    "type": "contains",
                    "expected": "ENV DATABASE_URL",
                    "hint": "Add an ENV instruction to set DATABASE_URL (use a default or placeholder value)",
                }
            ],
        },

        # Scenario 5: Non-root user can't bind to privileged port
        {
            "id": "non_root_privileged_port",
            "files": [
                {
                    "path": "Dockerfile",
                    "type": "dockerfile",
                    "content": (
                        "FROM python:3.11-slim\n"
                        "WORKDIR /app\n"
                        "COPY . .\n"
                        "RUN pip install -r requirements.txt\n"
                        "RUN useradd --create-home appuser\n"
                        "USER appuser\n"
                        "EXPOSE 80\n"
                        'CMD ["python", "app.py"]'
                    ),
                },
                {
                    "path": "requirements.txt",
                    "type": "requirements",
                    "content": "flask==2.3.0",
                },
            ],
            "error": {
                "phase": "docker_run",
                "message": (
                    "PermissionError: [Errno 13] Permission denied — "
                    "non-root user cannot bind to port 80"
                ),
                "exit_code": 1,
                "failed_step": "python app.py",
            },
            "expected_fixes": [
                {
                    "file": "Dockerfile",
                    "type": "contains",
                    "expected": "EXPOSE 8080",
                    "hint": "Non-root users cannot bind to ports below 1024 — use a higher port like 8080",
                }
            ],
        },
    ]