-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool-81-Dockerfile-Linter-Optimizer.html
More file actions
495 lines (423 loc) · 18.3 KB
/
tool-81-Dockerfile-Linter-Optimizer.html
File metadata and controls
495 lines (423 loc) · 18.3 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tool 81 - Dockerfile Linter Optimizer - Developer Toolbox</title>
<link rel="stylesheet" href="assets/core.css">
<style>
.split-view {
display: grid;
grid-template-columns: 1fr;
gap: 1.5rem;
margin-top: 1rem;
margin-bottom: 2rem;
}
@media (min-width: 900px) {
.split-view { grid-template-columns: 1fr 1fr; }
}
.panel {
background: rgba(0,0,0,0.4);
border: 1px solid var(--border-color);
border-radius: var(--radius-md);
padding: 1.5rem;
display: flex;
flex-direction: column;
gap: 1rem;
}
textarea.code-editor {
flex: 1;
background: rgba(0,0,0,0.5);
border: 1px solid var(--border-color);
border-radius: var(--radius-sm);
padding: 1rem;
font-family: var(--font-mono);
font-size: 0.85rem;
color: #e0e0e0;
resize: none;
outline: none;
min-height: 350px;
}
.issue-list {
list-style: none;
padding: 0;
margin: 0;
display: flex;
flex-direction: column;
gap: 0.8rem;
max-height: 200px;
overflow-y: auto;
}
.issue-item {
background: rgba(255,85,85,0.1);
border-left: 4px solid #ff5555;
padding: 0.8rem;
border-radius: 4px;
}
.issue-item.warning {
background: rgba(255,177,66,0.1);
border-left-color: #ffb142;
}
.issue-item.info {
background: rgba(52,172,224,0.1);
border-left-color: #34ace0;
}
.issue-title {
font-weight: bold;
font-size: 0.9rem;
color: #fff;
margin-bottom: 0.2rem;
}
.issue-desc {
font-size: 0.8rem;
color: #ccc;
}
.layer-viz {
display: flex;
flex-direction: column-reverse; /* Bottom up */
gap: 2px;
margin-top: 1rem;
padding: 1rem;
background: rgba(255,255,255,0.02);
border: 1px solid #333;
border-radius: 4px;
max-height: 250px;
overflow-y: auto;
}
.layer-box {
padding: 0.5rem;
background: #222;
border: 1px solid #444;
font-family: var(--font-mono);
font-size: 0.75rem;
color: #aaa;
border-left: 4px solid #33d9b2;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.header-actions {
display: flex;
justify-content: space-between;
align-items: center;
}
/* 3D Canvas wrapper */
#canvas-container {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
opacity: 0.3;
pointer-events: none;
}
</style>
</head>
<body class="theme-dark">
<div id="canvas-container"></div>
<main class="main-content" style="padding-top:2rem;">
<div class="container" style="max-width: 1300px;">
<div class="tool-header">
<div>
<a href="index.html" class="back-link">← Back to Toolbox</a>
<h1>81. Dockerfile Linter Optimizer</h1>
</div>
<div>
<select id="theme-select" aria-label="Select Theme">
<option value="dark">Dark Theme</option>
<option value="light">Light Theme</option>
<option value="solarized">Solarized</option>
<option value="neon">Neon Glow</option>
<option value="highcontrast">High Contrast</option>
</select>
</div>
</div>
<div class="split-view">
<!-- Input Panel -->
<div class="panel">
<div class="header-actions">
<h3>Original Dockerfile</h3>
<button class="btn btn-sm" id="btn-lint">Lint & Optimize</button>
</div>
<textarea id="dock-input" class="code-editor" spellcheck="false">FROM node:latest
WORKDIR /app
ADD package.json .
RUN npm install
ADD . .
RUN npm run build
EXPOSE 3000
CMD ["npm", "start"]</textarea>
<div>
<h4 style="margin-bottom:0.8rem;">Layer Visualisation</h4>
<div class="layer-viz" id="layer-viz">
<!-- Visual layers injected here -->
</div>
<div style="font-size: 0.75rem; color:#888; margin-top:0.5rem; text-align:center;">
Number of layers: <span id="layer-count">8</span> (fewer layers = smaller image size)
</div>
</div>
</div>
<!-- Output Panel -->
<div class="panel">
<div class="header-actions">
<h3>Optimized Version</h3>
<button class="btn btn-sm" id="btn-copy">Copy</button>
</div>
<textarea id="dock-output" class="code-editor" readonly></textarea>
<div style="margin-top:0.5rem;">
<h4 style="margin-bottom:0.8rem;">Issue Matrix</h4>
<ul class="issue-list" id="issue-list">
<li class="issue-item info">
<div class="issue-title">Ready to lint</div>
<div class="issue-desc">Click 'Lint & Optimize' to analyze your Dockerfile.</div>
</li>
</ul>
</div>
</div>
</div>
</div>
</main>
<footer class="global-footer">
<div class="container">
Made with ❤️ by <a href="https://github.com/Aliriyaj007" target="_blank">Aliriyaj007</a>
</div>
</footer>
<script src="assets/core.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r134/three.min.js"></script>
<script>
const inpDock = document.getElementById('dock-input');
const outDock = document.getElementById('dock-output');
const issueList = document.getElementById('issue-list');
const layerViz = document.getElementById('layer-viz');
const layerCount = document.getElementById('layer-count');
const btnLint = document.getElementById('btn-lint');
const btnCopy = document.getElementById('btn-copy');
function analyzeDockerfile() {
const raw = inpDock.value;
const lines = raw.split('\n');
let optimizedLines = [];
let issues = [];
let layers = [];
// Linter state
let currentRunBlock = [];
// regexes
const regexFromLatest = /^FROM\s+([^:]+)(:latest)?(\s+AS\s+.*)?$/i;
const regexAdd = /^ADD\s+(.*)$/i;
const regexRun = /^RUN\s+(.*)$/i;
// First pass: extract layers for visualization
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
// Track layers that add size: RUN, COPY, ADD
if (line.match(/^(FROM|COPY|ADD|RUN)/i)) {
layers.push(line);
}
if (!line || line.startsWith('#')) {
optimizedLines.push(lines[i]); // Keep comments and empty lines
continue;
}
// 1. FROM :latest warning
let matchFrom = line.match(regexFromLatest);
if (matchFrom && (matchFrom[2] === ':latest' || !matchFrom[2])) {
issues.push({
type: 'warning',
title: 'Use specific image tags',
desc: `Line ${i+1}: Using ':latest' is unpredictable. Use a specific version tag (e.g., node:18-alpine).`
});
// Optimization: add -alpine tag as suggestion
let base = matchFrom[1];
let asPart = matchFrom[3] || '';
optimizedLines.push(`FROM ${base}:alpine${asPart} # [Optimized] Added alpine tag`);
continue;
}
// 2. ADD vs COPY
let matchAdd = line.match(regexAdd);
if (matchAdd && !matchAdd[1].includes('http') && !matchAdd[1].endsWith('.tar.gz')) {
issues.push({
type: 'error',
title: 'Prefer COPY over ADD',
desc: `Line ${i+1}: ADD is used but COPY is preferred for local files since it's more transparent.`
});
optimizedLines.push(`COPY ${matchAdd[1]}`);
continue;
}
// 3. Collect RUN commands to chain them
let matchRun = line.match(regexRun);
if (matchRun && !line.endsWith('\\')) {
currentRunBlock.push(matchRun[1]);
// Don't push immediately, we might chain
// Lookahead: is the next line also a RUN command?
// To be safe, we only chain if they are adjacent
let nextIsRun = false;
for(let j = i+1; j < lines.length; j++){
if(lines[j].trim() === '') continue;
if(lines[j].trim().startsWith('RUN')) nextIsRun = true;
break;
}
if (!nextIsRun) {
// Flush the chain
if (currentRunBlock.length > 1) {
issues.push({
type: 'info',
title: 'Chained RUN commands',
desc: `Combined multiple RUN commands into a single layer to reduce image size.`
});
optimizedLines.push(`RUN ${currentRunBlock.join(' && \\ \n ')}`);
} else {
optimizedLines.push(`RUN ${currentRunBlock[0]}`);
}
// Check for apt-get clean if apt-get update is present
const runCode = currentRunBlock.join(' ');
if (runCode.includes('apt-get update') && !runCode.includes('rm -rf /var/lib/apt/lists')) {
issues.push({
type: 'warning',
title: 'Clean up package caches',
desc: `Found apt-get update but no cleanup. Added cleanup to reduce layer size.`
});
// Append cleanup to the last optimized RUN line
let lastRun = optimizedLines.pop();
optimizedLines.push(lastRun + ' && \\ \n rm -rf /var/lib/apt/lists/*');
}
currentRunBlock = [];
}
continue;
}
// Default: just copy the line
optimizedLines.push(lines[i]);
}
// Render output
outDock.value = optimizedLines.join('\n');
// Render layers visual
layerCount.textContent = layers.length;
layerViz.innerHTML = layers.map(l => `<div class="layer-box">${l}</div>`).join('');
// Render issues
if (issues.length === 0) {
issueList.innerHTML = `
<li class="issue-item info">
<div class="issue-title">All Good!</div>
<div class="issue-desc">No obvious optimizations found.</div>
</li>`;
} else {
issueList.innerHTML = issues.map(iss => `
<li class="issue-item ${iss.type}">
<div class="issue-title">${iss.title}</div>
<div class="issue-desc">${iss.desc}</div>
</li>
`).join('');
}
// Trigger 3D compression
triggerCompression(layers.length, optimizedLines.filter(l => l.match(/^(FROM|COPY|ADD|RUN)/i)).length);
}
btnLint.addEventListener('click', analyzeDockerfile);
btnCopy.addEventListener('click', () => {
navigator.clipboard.writeText(outDock.value);
btnCopy.textContent = 'Copied!';
setTimeout(() => { btnCopy.textContent = 'Copy'; }, 2000);
});
// 3D Scene: Stack of layers simulating compression
let scene, camera, renderer, stackGrp;
let layerMeshes = [];
function init3D() {
const container = document.getElementById('canvas-container');
if(!container) return;
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(50, window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.z = 25;
camera.position.x = 10;
camera.position.y = 8;
camera.lookAt(0, 0, 0);
renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
stackGrp = new THREE.Group();
scene.add(stackGrp);
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
const dirLight = new THREE.DirectionalLight(0xffffff, 0.8);
dirLight.position.set(-5, 10, 5);
scene.add(dirLight);
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Initial visual (default 8 layers)
buildStack(8);
animate3D();
}
function buildStack(count) {
// clear
while(stackGrp.children.length > 0){
stackGrp.remove(stackGrp.children[0]);
}
layerMeshes = [];
const matArr = [
new THREE.MeshPhongMaterial({ color: 0x34ace0, transparent: true, opacity: 0.8 }),
new THREE.MeshPhongMaterial({ color: 0x7ee787, transparent: true, opacity: 0.8 }),
new THREE.MeshPhongMaterial({ color: 0xffb142, transparent: true, opacity: 0.8 }),
new THREE.MeshPhongMaterial({ color: 0xff5555, transparent: true, opacity: 0.8 })
];
const gap = 1.0;
const startY = -(count * gap) / 2;
for(let i=0; i<count; i++) {
const mat = matArr[i % matArr.length];
const mesh = new THREE.Mesh(new THREE.BoxGeometry(8, 0.4, 8), mat);
// Start position
mesh.position.y = startY + (i * gap);
// Store target Y for compression animation
mesh.userData = {
originalY: mesh.position.y,
targetY: mesh.position.y
};
stackGrp.add(mesh);
layerMeshes.push(mesh);
}
}
let isCompressed = false;
function triggerCompression(originalCount, optimizedCount) {
// Rebuild stack to original count
buildStack(originalCount);
// Wait a beat, then compress down to optimized count visual
setTimeout(() => {
// Calculate new compact positions
const ratio = optimizedCount / Math.max(1, originalCount);
const newGap = 1.0 * ratio; // tighter gap
const startY = -(layerMeshes.length * newGap) / 2;
layerMeshes.forEach((mesh, i) => {
mesh.userData.targetY = startY + (i * newGap);
// Also make them slightly smaller to simulate pruning
mesh.scale.x = 0.9;
mesh.scale.z = 0.9;
});
isCompressed = true;
}, 500);
}
function animate3D() {
requestAnimationFrame(animate3D);
stackGrp.rotation.y += 0.005;
// lerp positions
layerMeshes.forEach(mesh => {
mesh.position.y += (mesh.userData.targetY - mesh.position.y) * 0.1;
// Pulse slightly when not compressed
if (!isCompressed) {
mesh.scale.x = 1.0 + Math.sin(Date.now()*0.002 + mesh.position.y)*0.02;
mesh.scale.z = 1.0 + Math.sin(Date.now()*0.002 + mesh.position.y)*0.02;
}
});
renderer.render(scene, camera);
}
document.addEventListener('DOMContentLoaded', () => {
init3D();
analyzeDockerfile(); // initial run
// Reset compression state on input
inpDock.addEventListener('input', () => {
isCompressed = false;
layerMeshes.forEach(m => {
m.userData.targetY = m.userData.originalY;
m.scale.set(1,1,1);
});
});
});
</script>
</body>
</html>