docker 中的错误-未定义撰写服务器
Error in docker-compose server is not defined
我正在尝试 运行 一个非常简单的 docker-compose.yml,但是在客户端容器。
| /app/index.js:6
node-app_1 | host: redis-server,
node-app_1 | ^
代码如下:
index.js
const express = require('express');
const redis = require('redis');
const app = express();
const client = redis.createClient({
host: redis-server,
port: 6379
});
client.set('visits', 0);
app.get('/', (req,res) => {
client.get('visits', (err,visits) => {
res.send('Number of visit is: ' + visits);
client.set('visits', parseInt(visits) + 1);
})
})
app.listen(8081, () =>{
console.log('Listening on port 8081');
})
docker-compose.yml
version: '3'
services:
redis-server:
image: 'redis'
node-app:
build: .
ports:
- '4001:8081'
docker-compose up
的输出
redis-server_1 | 1:C 07 Nov 2020 16:52:30.899 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis-server_1 | 1:C 07 Nov 2020 16:52:30.900 # Redis version=6.0.9, bits=64, commit=00000000, modified=0, pid=1, just started
redis-server_1 | 1:C 07 Nov 2020 16:52:30.900 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis-server_1 | 1:M 07 Nov 2020 16:52:30.901 * Running mode=standalone, port=6379.
redis-server_1 | 1:M 07 Nov 2020 16:52:30.902 # Server initialized
redis-server_1 | 1:M 07 Nov 2020 16:52:30.902 * Loading RDB produced by version 6.0.9
redis-server_1 | 1:M 07 Nov 2020 16:52:30.902 * RDB age 180 seconds
redis-server_1 | 1:M 07 Nov 2020 16:52:30.902 * RDB memory usage when created 0.77 Mb
redis-server_1 | 1:M 07 Nov 2020 16:52:30.903 * DB loaded from disk: 0.000 seconds
redis-server_1 | 1:M 07 Nov 2020 16:52:30.903 * Ready to accept connections
node-app_1 |
node-app_1 | > start
node-app_1 | > node index.js
node-app_1 |
node-app_1 | /app/index.js:6
node-app_1 | host: redis-server,
node-app_1 | ^
node-app_1 |
node-app_1 | ReferenceError: server is not defined
node-app_1 | at Object.<anonymous> (/app/index.js:6:17)
node-app_1 | at Module._compile (node:internal/modules/cjs/loader:1083:30)
node-app_1 | at Object.Module._extensions..js (node:internal/modules/cjs/loader:1112:10)
node-app_1 | at Module.load (node:internal/modules/cjs/loader:948:32)
node-app_1 | at Function.Module._load (node:internal/modules/cjs/loader:789:14)
node-app_1 | at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:72:12)
node-app_1 | at node:internal/main/run_main_module:17:47
node-app_1 | npm ERR! code 1
node-app_1 | npm ERR! path /app
node-app_1 | npm ERR! command failed
node-app_1 | npm ERR! command sh -c node index.js
node-app_1 |
node-app_1 | npm ERR! A complete log of this run can be found in:
node-app_1 | npm ERR! /root/.npm/_logs/2020-11-07T16_52_31_324Z-debug.log
visits_node-app_1 exited with code 1
它没有找到“redis-server”,但根据 udemy 课程,它应该找到它,因为这就是它在 composer 中的识别方式。
有什么问题?
"redis-server" 应该是一个字符串,用引号表示:
const client = redis.createClient({
host: 'redis-server',
port: 6379
});
我正在尝试 运行 一个非常简单的 docker-compose.yml,但是在客户端容器。
| /app/index.js:6
node-app_1 | host: redis-server,
node-app_1 | ^
代码如下:
index.js
const express = require('express');
const redis = require('redis');
const app = express();
const client = redis.createClient({
host: redis-server,
port: 6379
});
client.set('visits', 0);
app.get('/', (req,res) => {
client.get('visits', (err,visits) => {
res.send('Number of visit is: ' + visits);
client.set('visits', parseInt(visits) + 1);
})
})
app.listen(8081, () =>{
console.log('Listening on port 8081');
})
docker-compose.yml
version: '3'
services:
redis-server:
image: 'redis'
node-app:
build: .
ports:
- '4001:8081'
docker-compose up
redis-server_1 | 1:C 07 Nov 2020 16:52:30.899 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis-server_1 | 1:C 07 Nov 2020 16:52:30.900 # Redis version=6.0.9, bits=64, commit=00000000, modified=0, pid=1, just started
redis-server_1 | 1:C 07 Nov 2020 16:52:30.900 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis-server_1 | 1:M 07 Nov 2020 16:52:30.901 * Running mode=standalone, port=6379.
redis-server_1 | 1:M 07 Nov 2020 16:52:30.902 # Server initialized
redis-server_1 | 1:M 07 Nov 2020 16:52:30.902 * Loading RDB produced by version 6.0.9
redis-server_1 | 1:M 07 Nov 2020 16:52:30.902 * RDB age 180 seconds
redis-server_1 | 1:M 07 Nov 2020 16:52:30.902 * RDB memory usage when created 0.77 Mb
redis-server_1 | 1:M 07 Nov 2020 16:52:30.903 * DB loaded from disk: 0.000 seconds
redis-server_1 | 1:M 07 Nov 2020 16:52:30.903 * Ready to accept connections
node-app_1 |
node-app_1 | > start
node-app_1 | > node index.js
node-app_1 |
node-app_1 | /app/index.js:6
node-app_1 | host: redis-server,
node-app_1 | ^
node-app_1 |
node-app_1 | ReferenceError: server is not defined
node-app_1 | at Object.<anonymous> (/app/index.js:6:17)
node-app_1 | at Module._compile (node:internal/modules/cjs/loader:1083:30)
node-app_1 | at Object.Module._extensions..js (node:internal/modules/cjs/loader:1112:10)
node-app_1 | at Module.load (node:internal/modules/cjs/loader:948:32)
node-app_1 | at Function.Module._load (node:internal/modules/cjs/loader:789:14)
node-app_1 | at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:72:12)
node-app_1 | at node:internal/main/run_main_module:17:47
node-app_1 | npm ERR! code 1
node-app_1 | npm ERR! path /app
node-app_1 | npm ERR! command failed
node-app_1 | npm ERR! command sh -c node index.js
node-app_1 |
node-app_1 | npm ERR! A complete log of this run can be found in:
node-app_1 | npm ERR! /root/.npm/_logs/2020-11-07T16_52_31_324Z-debug.log
visits_node-app_1 exited with code 1
它没有找到“redis-server”,但根据 udemy 课程,它应该找到它,因为这就是它在 composer 中的识别方式。
有什么问题?
"redis-server" 应该是一个字符串,用引号表示:
const client = redis.createClient({
host: 'redis-server',
port: 6379
});