【问题标题】:how can I run a web-app with a docker-container?如何使用 docker-container 运行 Web 应用程序?
【发布时间】:2020-08-19 20:16:39
【问题描述】:

我有一个简单的网络应用程序,其中包含 index.html、app.js 和 package.json 文件。
现在,我想通过 docker-container 运行它。在我的本地机器上,我可以使用npm install 然后npm start 运行该应用程序。
当我尝试通过docker-compose up 运行它时,我收到以下错误消息:

Couldn't connect to Docker daemon at http+docker://localunixsocket - is it running?
If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable

我的 Dockerfile 如下所示:

FROM node:8.11
WORKDIR /usr/src/app
ARG NODE_ENV
ENV NODE_ENV $NODE_ENV
COPY package.json /usr/src/app/
RUN npm install
COPY . /usr/src/app
    
# replace this with your application's default port
EXPOSE 8000
CMD [ "npm", "run", "dev" ]

和 docker-compose.yml 看起来像这样:

version: "2" 
services:
  web:
    build: .
    command: nodemon -L --inspect=0.0.0.0:5858
    volumes:
      - .:/usr/src/app
    ports:
      - "8000:8000"
      - "5858:5858"

实际上,应用程序应该在 localhost:8000 或 localhost:5858 下运行调试模式。

知道 Dockerfile 或 docker-compose.yml 有什么问题吗?我已经尝试了here 中描述的修复方法,但是这两个建议都对我不起作用,所以肯定有其他问题。

在此先感谢您的问候。
PS:如果您需要更多代码,请随时告诉我,我将其添加到问题中。

更新:package.json 如下所示:

{
  "name": "custom-meta-model",
  "version": "0.0.0",
  "description": "An bpmn-js modeler extended with a custom meta-model",
  "main": "app/index.js",
  "scripts": {
    "all": "grunt",
    "dev": "grunt auto-build"
  },
  "keywords": [
    "bpmnjs-example"
  ],
  "author": {
    "name": "Nico Rehwaldt",
    "url": "https://github.com/nikku"
  },
  "contributors": [
    {
      "name": "bpmn.io contributors",
      "url": "https://github.com/bpmn-io"
    }
  ],
  "license": "MIT",
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-preset-env": "^1.7.0",
    "babelify": "^8.0.0",
    "grunt": "^1.2.0",
    "grunt-browserify": "^5.3.0",
    "grunt-contrib-watch": "^1.1.0",
    "grunt-contrib-connect": "^2.1.0",
    "grunt-contrib-copy": "^1.0.0",
    "load-grunt-tasks": "^5.1.0",
    "stringify": "^5.2.0"
  },
  "dependencies": {
    "bpmn-js": "^7.2.0",
    "diagram-js": "^6.6.1",
    "jquery": "^3.5.1"
  }
}

更新 2:现在看起来好多了,我更正了 Dockerfile 中的 CMD-Command。现在输出告诉我没有找到'grunt'。具体:

Step 9/9 : CMD [ "npm", "run", "dev" ]
 ---> Running in f51692a86908
Removing intermediate container f51692a86908
 ---> 53e88bbb46c4
Successfully built 53e88bbb46c4
Successfully tagged overlayexample2_web:latest
Recreating overlayexample2_web_1
Attaching to overlayexample2_web_1
web_1  | 
web_1  | > custom-meta-model@0.0.0 dev /usr/src/app
web_1  | > grunt auto-build
web_1  | 
web_1  | sh: 1: grunt: not found
web_1  | npm ERR! code ELIFECYCLE
web_1  | npm ERR! syscall spawn
web_1  | npm ERR! file sh
web_1  | npm ERR! errno ENOENT
web_1  | npm ERR! custom-meta-model@0.0.0 dev: `grunt auto-build`
web_1  | npm ERR! spawn ENOENT
web_1  | npm ERR! 
web_1  | npm ERR! Failed at the custom-meta-model@0.0.0 dev script.
web_1  | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
web_1  | npm WARN Local package.json exists, but node_modules missing, did you mean to install?
web_1  | 
web_1  | npm ERR! A complete log of this run can be found in:
web_1  | npm ERR!     /root/.npm/_logs/2020-08-21T17_03_50_937Z-debug.log
overlayexample2_web_1 exited with code 1

我该如何解决?

也许 Gruntfile.js 需要修改,目前看起来是这样的:

module.exports = function(grunt) {

  require('load-grunt-tasks')(grunt);
  grunt.loadNpmTasks('grunt-docker');

  grunt.initConfig({

    browserify: {
      options: {
        transform: [
          [ 'stringify', {
            extensions: [ '.bpmn' ]
          } ],
          [ 'babelify', {
            global: true
          } ]
        ]
      },
      watch: {
        options: {
          watch: true
        },
        files: {
          'dist/index.js': [ 'app/**/*.js' ]
        }
      },
      app: {
        files: {
          'dist/index.js': [ 'app/**/*.js' ]
        }
      }
    },
    copy: {
      diagram_js: {
        files: [ {
          src: require.resolve('diagram-js/assets/diagram-js.css'),
          dest: 'dist/css/diagram-js.css'
        } ]
      },
      app: {
        files: [
          {
            expand: true,
            cwd: 'app',
            src: ['**/*.*', '!**/*.js'],
            dest: 'dist'
          }
        ]
      }
    },
    watch: {
      options: {
        livereload: false
      },

      samples: {
        files: [ 'app/**/*.*' ],
        tasks: [ 'copy:app' ]
      },
    },

    connect: {
      livereload: {
        options: {
          port: 8000,
          livereload: true,
          hostname: '*',
          open: false,
          base: [
            'dist'
          ]
        }
      }
    }
  });

  // tasks

  grunt.registerTask('build', [ 'browserify:app', 'copy' ]);

  grunt.registerTask('auto-build', [
    'copy',
    'browserify:watch',
    'connect:livereload',
    'watch'
  ]);

  grunt.registerTask('default', [ 'build' ]);
};

【问题讨论】:

  • 你在哪个系统上?
  • 你是如何安装 Docker 的?
  • Linux Debian amd64 架构

标签: javascript node.js docker docker-compose dockerfile


【解决方案1】:

您的 Docker 服务没有运行,您需要手动启动 Docker Desktop。

【讨论】:

    【解决方案2】:

    您必须在系统中启用并启动 Docker 守护程序。

    如果你在 Linux 上,试试看:sudo systemctl enable docker && sudo systemctl start docker

    如果systemctl 未被识别为命令,则应使用:service docker start。

    第一次运行需要systemctl start,因为enable 只会在重启后自动启动守护进程。启用后,它会在开机时自动启动。

    【讨论】:

    • 我刚刚尝试了您的建议,但仍然出现相同的错误消息:(您确定我的配置正确吗?这是我第一次尝试使用 docker,所以我绝对是初学者
    • 您的配置似乎是正确的,除了 docker 和 docker-compose 在 dockerfile 或 docker-compose.yml 中有特定的语法或配置错误消息。尝试使用sudo usermod $USER -a -G docker 将您的用户添加到docker 组并重新启动,然后重试。
    【解决方案3】:

    Dockerfile 和 docker-compose.file 没有问题。

    尝试使用 sudo 运行:

    sudo docker-compose up
    

    【讨论】:

    • 我现在可以运行 docker-compose,但我应该提到我正在使用 Gruntfile.js 来启动 Web 服务器。所以我只是用“grunt”替换了 docker-compose 中的“web”,但这不起作用。你知道如何配置 docker-compose 以使用 Gruntfile.js 吗?
    • 您必须更新 package.json 文件,而不是 web。找到scripts部分,并更新:"start": "grunt"
    • 在 package.json 中我为此使用了“dev”脚本,我还更正了卷目录和 Dockerfile 中的 CMD。但它仍然不起作用。我不必在 docker-compose 中设置 grunt 吗?我刚刚更新了问题,所以你现在也可以看到 package.json
    • 你可以在脚本标签下添加"start": "grunt"
    • 输出告诉我,未找到 grunt(只是更新了问题),我是否必须复制 Gruntfile 或其他内容?
    猜你喜欢
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 2023-03-05
    • 2017-02-24
    • 2023-03-27
    • 1970-01-01
    相关资源
    最近更新 更多