gpt4 book ai didi

node.js - Docker-无法连接到运行镜像

转载 作者:行者123 更新时间:2023-12-02 21:27:01 28 4
gpt4 key购买 nike

在开始解释错误之前,先说我是Windows用户,并且没有使用Unix命令的丰富经验。因此,每个步骤都是使用 Docker快速入门终端(MINGW64)完成的。

几周前,我第一次听说了docker,并考虑将其用于节点/快速网站。因此,我在Synology服务器上安装了Docker软件包。

整理完网站后,我执行了以下操作:

  • 我按照此网站上的说明进行操作:
    https://docs.docker.com/windows/step_one/直到最后一步,
    一切正常(包括docker run hello-world)


  • 然后进入“Dockerizing Node.js Web应用程序” :
    https://docs.docker.com/engine/examples/nodejs_web_app/

  • 文件层次结构:

    src (C:.....\projectname)

    --assets (folder)

    --controllers (folder)

    --public (folder)

    --src (folder)

    --util (folder)

    --views (folder)

    --node_modules (folder)

    --bin (folder)

    ----www (file, no extention)

    --app.js (file)

    --Dockerfile (file, no extention)

    --package.json (file)



    至于内容:
    www :
    #!/usr/bin/env node

    /**
    * Module dependencies.
    */

    var app = require('../app');
    var debug = require('debug')('projectname:server');
    var http = require('http');

    /**
    * Get port from environment and store in Express.
    */

    var port = normalizePort(process.env.PORT || '3000');
    app.set('port', port);

    /**
    * Create HTTP server.
    */

    var server = http.createServer(app);

    /**
    * Listen on provided port, on all network interfaces.
    */

    server.listen(port);
    server.on('error', onError);
    server.on('listening', onListening);

    /**
    * Normalize a port into a number, string, or false.
    */

    function normalizePort(val) {
    var port = parseInt(val, 10);

    if (isNaN(port)) {
    // named pipe
    return val;
    }

    if (port >= 0) {
    // port number
    return port;
    }

    return false;
    }

    /**
    * Event listener for HTTP server "error" event.
    */

    function onError(error) {
    if (error.syscall !== 'listen') {
    throw error;
    }

    var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

    // handle specific listen errors with friendly messages
    switch (error.code) {
    case 'EACCES':
    console.error(bind + ' requires elevated privileges');
    process.exit(1);
    break;
    case 'EADDRINUSE':
    console.error(bind + ' is already in use');
    process.exit(1);
    break;
    default:
    throw error;
    }
    }

    /**
    * Event listener for HTTP server "listening" event.
    */

    function onListening() {
    var addr = server.address();
    var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
    debug('Listening on ' + bind);
    }

    app.js :
    var express = require('express');
    var path = require('path');
    var favicon = require('serve-favicon');
    var logger = require('morgan');
    var cookieParser = require('cookie-parser');
    var bodyParser = require('body-parser');

    var app = express();

    // view engine setup
    app.set('views', path.join(__dirname, 'views'));
    app.set('view engine', 'jade');

    app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
    app.use(logger('dev'));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(cookieParser());

    app.use(express.static(path.join(__dirname, 'public')));

    app.use(require('./controllers'));

    };

    // catch 404 and forward to error handler
    app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
    });

    // error handlers

    // development error handler
    // will print stacktrace
    if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
    message: err.message,
    error: err
    });
    });
    }

    // production error handler
    // no stacktraces leaked to user
    app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
    message: err.message,
    error: {}
    });
    });


    module.exports = app;

    package.json :
    {
    "name": "projectname",
    "version": "0.0.0",
    "private": true,
    "scripts": {
    "start": "node ./bin/www"
    },
    "dependencies": {
    "bcrypt-nodejs": "0.0.3",
    "body-parser": "~1.13.2",
    "bookshelf": "^0.9.2",
    "cookie-parser": "~1.3.5",
    "debug": "~2.2.0",
    "express": "~4.13.1",
    "express-session": "^1.13.0",
    "i18n": "^0.8.0",
    "jade": "~1.11.0",
    "knex": "^0.10.0",
    "morgan": "~1.6.1",
    "mysql": "^2.10.2",
    "passport": "^0.3.2",
    "passport-local": "^1.0.0",
    "serve-favicon": "~2.3.0"
    },
    "devDependencies": {
    "autoprefixer": "^6.3.3",
    "browserify": "^13.0.0",
    "connect-livereload": "^0.5.4",
    "grunt": "^0.4.5",
    "grunt-browserify": "^4.0.1",
    "grunt-contrib-cssmin": "^0.14.0",
    "grunt-contrib-sass": "^0.9.2",
    "grunt-contrib-uglify": "^0.11.1",
    "grunt-contrib-watch": "^0.6.1",
    "grunt-postcss": "^0.7.2"
    }
    }

    因此,如在package.json和www(由express generator命令生成)中所看到的,我必须编写 npm start来运行节点/ express服务器。

    Dockerfile :
    FROM centos:centos6

    RUN yum install -y epel-release
    RUN yum install -y nodejs npm

    COPY package.json /projectname/package.json
    RUN cd /projectname; npm install --production

    COPY . /projectname

    EXPOSE 8080

    CMD ["npm", "start"]

  • 成功构建docker build -t username/projectname .之后,我确实得到了安全警告:

    Successfully built 5ed562273b56

    SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.


  • 除此之外,不会引发任何错误,因此我运行了图像: docker run -p 49160:8080 -d username/projectname。之后,我得到一个长的哈希字符串

    de297db51ab6fb3f842abb58267c1e189d2b9de51715a619a2f5431e868dc54f



  • 仍然遵循https://docs.docker.com/engine/examples/nodejs_web_app/的原则。为了测试图像,我使用docker ps列出了容器ID ,这让我知道了这一点:

  • CONTAINER ID | IMAGE | COMMAND | CREATED | STATUS | PORTS | NAMES



    太空了!除了表格的标题外,什么都没有。但是当我使用链接中提供的代码并构建其图像时,它的确给了我一个结果(如文章中所述):

    CONTAINER ID | IMAGE | COMMAND | CREATED | STATUS | PORTS | NAMES

    26d3ac309d81 | username/centos-node-testing | "node /src/index.js" | 35 minutes ago | Up 35 minutes | 0.0.0.0:49160->8080/tcp | gigantic_ritchie



  • 可以肯定的是,我尝试使用 curl命令:curl -i 192.168.99.100:49160调用应用程序。不幸的是,这给了我一个错误:

  • curl: (7) Failed to connect to 192.168.99.100 port 49160: Connection refused



    使用 docker-machine ip命令检索IP地址。

  • 作为最后的手段,有人建议使用以下命令docker run username/projectname简单地运行应用程序。但是那给了我一个错误:

    npm ERR! Error: ENOENT, open '/package.json'

    npm ERR! If you need help, you may report this log at:

    npm ERR! http://github.com/isaacs/npm/issues

    npm ERR! or email it to:

    npm ERR!

    npm ERR! System Linux 4.1.19-boot2docker

    npm ERR! command "node" "/usr/bin/npm" "start"

    npm ERR! cwd /

    npm ERR! node -v v0.10.42

    npm ERR! npm -v 1.3.6

    npm ERR! path /package.json

    npm ERR! code ENOENT

    npm ERR! errno 34

    npm ERR!

    npm ERR! Additional logging details can be found in:

    npm ERR! /npm-debug.log

    npm ERR! not ok code 0


  • 有什么想法可能导致这种情况吗?

    最佳答案

    您的容器不存在,因为您提供的命令(上面的CMD)返回非零退出状态,并且该容器由于故障而被破坏。在您的Dockerfile中,让我们尝试类似以下的操作,该操作应确保npm start从项目根目录中运行:

    FROM centos:centos6

    RUN yum install -y epel-release
    RUN yum install -y nodejs npm

    COPY package.json /projectname/package.json

    # Set the working directory
    WORKDIR /projectname

    RUN npm install --production

    COPY . /projectname

    EXPOSE 8080

    CMD ["npm", "start"]

    同样,为了将来,如果您使用 docker run -it username/projectname /bin/bash,可能还需要对容器进行故障排除。

    关于node.js - Docker-无法连接到运行镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36261186/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com