Skip to content

Commit 3470510

Browse files
committed
fix: 修改create bug
1 parent ff93efd commit 3470510

File tree

4 files changed

+129
-114
lines changed

4 files changed

+129
-114
lines changed

bin/www

Lines changed: 60 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,88 +4,82 @@
44
* Module dependencies.
55
*/
66

7-
var app = require('../app');
8-
var io = app.io;
9-
var http = require('http');
7+
const app = require('../app');
8+
const io = app.io;
9+
const http = require('http');
1010

1111
/**
12-
* Get port from environment and store in Express.
12+
* Normalize a port into a number, string, or false.
1313
*/
14+
const normalizePort = val => {
15+
const port = parseInt(val, 10);
1416

15-
var port = normalizePort(process.env.PORT || '3000');
16-
app.set('port', port);
17+
if (isNaN(port)) {
18+
// named pipe
19+
return val;
20+
}
1721

18-
/**
19-
* Create HTTP server.
20-
*/
22+
if (port >= 0) {
23+
// port number
24+
return port;
25+
}
2126

22-
var server = http.createServer(app);
27+
return false;
28+
};
2329

2430
/**
25-
* Listen on provided port, on all network interfaces.
31+
* Event listener for HTTP server "error" event.
2632
*/
27-
28-
server.listen(port);
29-
server.on('error', onError);
30-
server.on('listening', onListening);
31-
io.attach(server);
33+
const onError = error => {
34+
if (error.syscall !== 'listen') {
35+
throw error;
36+
}
37+
38+
const bind = typeof port === 'string'
39+
? 'Pipe ' + port
40+
: 'Port ' + port;
41+
42+
// handle specific listen errors with friendly messages
43+
switch (error.code) {
44+
case 'EACCES':
45+
console.log(bind + ' requires elevated privileges');
46+
process.exit(1);
47+
break;
48+
case 'EADDRINUSE':
49+
console.log(bind + ' is already in use');
50+
process.exit(1);
51+
break;
52+
default:
53+
throw error;
54+
}
55+
};
3256

3357
/**
34-
* Normalize a port into a number, string, or false.
58+
* Event listener for HTTP server "listening" event.
3559
*/
36-
37-
function normalizePort(val) {
38-
var port = parseInt(val, 10);
39-
40-
if (isNaN(port)) {
41-
// named pipe
42-
return val;
43-
}
44-
45-
if (port >= 0) {
46-
// port number
47-
return port;
48-
}
49-
50-
return false;
51-
}
60+
const onListening = () => {
61+
const addr = server.address();
62+
const bind = typeof addr === 'string'
63+
? 'pipe ' + addr
64+
: 'port ' + addr.port;
65+
console.log('Listening on ' + bind);
66+
};
5267

5368
/**
54-
* Event listener for HTTP server "error" event.
69+
* Get port from environment and store in Express.
5570
*/
56-
57-
function onError(error) {
58-
if (error.syscall !== 'listen') {
59-
throw error;
60-
}
61-
62-
var bind = typeof port === 'string'
63-
? 'Pipe ' + port
64-
: 'Port ' + port;
65-
66-
// handle specific listen errors with friendly messages
67-
switch (error.code) {
68-
case 'EACCES':
69-
console.log(bind + ' requires elevated privileges');
70-
process.exit(1);
71-
break;
72-
case 'EADDRINUSE':
73-
console.log(bind + ' is already in use');
74-
process.exit(1);
75-
break;
76-
default:
77-
throw error;
78-
}
79-
}
71+
const port = normalizePort(process.env.PORT || '3000');
72+
app.set('port', port);
8073

8174
/**
82-
* Event listener for HTTP server "listening" event.
75+
* Create HTTP server.
8376
*/
77+
const server = http.createServer(app);
8478

85-
function onListening() {
86-
var addr = server.address();
87-
var bind = typeof addr === 'string'
88-
? 'pipe ' + addr
89-
: 'port ' + addr.port;
90-
console.log('Listening on ' + bind);
91-
}
79+
/**
80+
* Listen on provided port, on all network interfaces.
81+
*/
82+
server.listen(port);
83+
server.on('error', onError);
84+
server.on('listening', onListening);
85+
io.attach(server);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
"dependencies": {
99
"body-parser": "1.20.3",
10-
"dockerode": "^4.0.3",
10+
"dockerode": "^4.0.4",
1111
"ejs": "3.1.10",
1212
"express": "^4.16.4",
1313
"express-session": "^1.17.1",

pnpm-lock.yaml

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

routes/containers.js

Lines changed: 59 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -47,79 +47,100 @@ const returnContainersRouter = (io) => {
4747
});
4848

4949
router.post('/create', (req, res, next) => {
50+
console.log('Container create request:', {
51+
image: req.body.containerImage,
52+
name: req.body.containerName,
53+
cmd: req.body.containerCmd,
54+
ports: `${req.body.containerPortSource}:${req.body.containerPortDistination}`,
55+
volumes: `${req.body.containerVolumeSource}:${req.body.containerVolumeDistination}`,
56+
isAlways: req.body.isAlways
57+
});
58+
59+
if (!req.body.containerImage || typeof req.body.containerImage !== 'string') {
60+
return res.status(400).send('Invalid image name');
61+
}
62+
63+
// Handle comma-separated image names
64+
const imageName = req.body.containerImage.split(',')[0].trim();
65+
5066
let options = {
51-
Image: req.body.containerImage,
67+
Image: imageName,
5268
AttachStdin: false,
5369
AttachStdout: true,
5470
AttachStderr: true,
5571
Tty: false,
5672
HostConfig: {
57-
PortBindings: {}
73+
PortBindings: {},
74+
Binds: [],
75+
RestartPolicy: {
76+
Name: req.body.isAlways === 'on' ? 'always' : 'no'
77+
}
5878
}
5979
};
6080

61-
// name
6281
if (req.body.containerName !== '') {
63-
options = {
64-
...options,
65-
name: req.body.containerName
66-
};
82+
options.name = req.body.containerName;
6783
}
6884

69-
// volume
7085
if (req.body.containerVolumeSource !== '' &&
7186
req.body.containerVolumeDistination !== '') {
7287
const src = req.body.containerVolumeSource;
7388
const dis = req.body.containerVolumeDistination;
74-
options['Volumes'] = {dis: {}};
75-
options.HostConfig = {
76-
'Binds': [src + ':' + dis],
77-
'RestartPolicy': {
78-
'Name': req.body.isAlways === 'on' ? 'always' : '',
79-
'MaximumRetryCount': 5
80-
}
81-
};
89+
90+
if (!options.Volumes) {
91+
options.Volumes = {};
92+
}
93+
options.Volumes[dis] = {};
94+
options.HostConfig.Binds.push(`${src}:${dis}`);
8295
}
8396

84-
// port
8597
if (req.body.containerPortSource !== '' &&
8698
req.body.containerPortDistination !== '') {
87-
const src = req.body.containerPortSource + '/tcp';
99+
const src = `${req.body.containerPortSource}/tcp`;
88100
const dis = req.body.containerPortDistination;
89-
options['ExposedPorts'] = {dis: {}};
90-
options.HostConfig.PortBindings = {
91-
src: [{HostPort: dis}]
92-
};
101+
102+
if (!options.ExposedPorts) {
103+
options.ExposedPorts = {};
104+
}
105+
options.ExposedPorts[src] = {};
106+
options.HostConfig.PortBindings[src] = [{HostPort: dis}];
93107
}
94108

95-
if (req.body.containerCmd != '') {
109+
if (req.body.containerCmd !== '') {
110+
// Use /bin/sh instead of /bin/bash
96111
options.Cmd = ['/bin/sh', '-c', req.body.containerCmd];
97-
// console.log(options)
98-
docker.createContainer(options, (err, container) => {
99-
if (err) throw err;
100-
container.start((err, data) => {
112+
113+
docker.createContainer(options)
114+
.then(container => container.start())
115+
.then(container => {
101116
res.redirect('/containers/logs/' + container.id);
117+
})
118+
.catch(err => {
119+
console.error('Docker错误:', err);
120+
next(err);
102121
});
103-
});
104122
} else {
105123
const runOpt = {
106-
Image: req.body.containerImage,
124+
...options,
107125
AttachStdin: true,
108126
AttachStdout: true,
109127
AttachStderr: true,
110128
Tty: true,
111-
//Cmd: ['/bin/sh'],
112129
OpenStdin: false,
113-
StdinOnce: false,
114-
...options
130+
StdinOnce: false
131+
// Don't set Cmd to avoid shell issues
115132
};
116-
docker.createContainer(runOpt).then(function (container) {
117-
return container.start();
118-
}).then((container) => {
119-
res.redirect('/containers');
120-
});
121-
}
122133

134+
docker.createContainer(runOpt)
135+
.then(container => container.start())
136+
.then(container => {
137+
res.redirect('/containers');
138+
})
139+
.catch(err => {
140+
console.error('Docker错误:', err);
141+
next(err);
142+
});
143+
}
123144
});
124145

125146
router.get('/console/:id', (req, res, next) => {

0 commit comments

Comments
 (0)