gpt4 book ai didi

javascript - 如何使用 docker-container 运行网络应用程序?

转载 作者:行者123 更新时间:2023-12-01 17:14:53 26 4
gpt4 key购买 nike

我有一个简单的网络应用程序,带有 index.htmlapp.jspackage.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 下运行 Debug模式。

知道 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,请尝试:sudo systemctl enable docker && sudo systemctl start docker

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

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

关于javascript - 如何使用 docker-container 运行网络应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63494379/

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