从Docker文件中提供一个Node项目

huangapple go评论100阅读模式
英文:

Serving up a node project from within a docker file

问题

我对Node.js和前端世界还不太熟悉,各种框架让我感到困惑。

我有一个可以在本地运行的Node项目。我使用uppy前端框架和tus.io来处理后端上传,已经实现了一个简单的概念验证。我使用yarn start命令使用vite在本地提供所有内容。

我想从Docker文件中运行它,以便我可以托管它并向同事展示。有人可以帮助我让它运行起来吗?不需要太复杂。

我尝试过以下方法:

  • 使用标准的HTTP服务器实现这里,但出现了关于.js文件不是cjs的错误,然后在浏览器中出现了Uncaught TypeError: The specifier “@uppy/core” was a bare specifier, but was not remapped to anything的错误。
  • 尝试从Docker文件中使用vite,但出现了FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory的错误。
  • 使用webpack,但是出现了各种cjs错误。

我的index.html文件:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Node.js + Uppy Example</title>
</head>
<body>
    <noscript>The app requires JavaScript to be enabled.</noscript>
    <button id="modalVisit">Upload To Visit</button>
    <script src="./upload_to_visit.js" type="module"></script>
    <noscript>The app requires JavaScript to be enabled.</noscript>
    <button id="modalDataset">Upload To Dataset</button>
    <script src="./upload_to_dataset.js" type="module"></script>
</body>
</html>

我的upload_to_visit.js文件:

import Uppy from '@uppy/core'
import Dashboard from '@uppy/dashboard';
import Tus from '@uppy/tus'
import GoldenRetriever from '@uppy/golden-retriever';

import '@uppy/core/dist/style.css';
import '@uppy/drag-drop/dist/style.min.css';
import '@uppy/dashboard/dist/style.min.css';

const uppy = new Uppy({
    debug: true,
    autoProceed: false,
})

uppy.use(Dashboard, {
    inline: false,
    trigger:"#modalDataset",
    target: 'body',
    note:"to upload to a dataset",
    proudlyDisplayPoweredByUppy: false,
    showProgressDetails: true,
    fileManagerSelectionType:"files",
})

uppy.use(Tus, { endpoint: 'http://127.0.0.1:3000/files/datasets/' });
uppy.use(GoldenRetriever);

(我知道URL需要更改以适应Docker网络)

我的package.json文件:

{
  "name": "node_uppy",
  "version": "0.0.0",
  "type": "module",
  "dependencies": {
    "@uppy/core": "^3.3.1",
    "@uppy/drag-drop": "^3.0.2",
    "@uppy/dashboard": "",
    "@uppy/tus": "^3.1.2",
    "@uppy/golden-retriever": "^3.1.0",
    "formidable": "^3.5.0"
  },
  "devDependencies": {
    "npm-run-all": "^4.1.3",
    "vite": "^4.0.0"
  },
  "private": true,
  "scripts": {
    "build": "npm-run-all --parallel",
    "start": "npm-run-all --parallel start:client",
    "start:client": "vite"
  }
}
英文:

I'm pretty new to the node.js/frontend world and am getting bamboozled by the various frameworks.

I have a node project that I can run locally. I've got a simple proof of concept up and running using the uppy front end framework, supported by tus.io to handle backend uploads. I use vite to serve everything up locally using yarn start.

I'm looking to run it from within a docker file so I can host it and show some colleagues. Can anyone help to get this running, it doesn't have to be pretty.

Things I've tried:

  • using a standard HTTP server implementation here, got errors about .js files not cjs then getting Uncaught TypeError: The specifier “@uppy/core” was a bare specifier, but was not remapped to anything in the browser
  • trying to use vite from a docker file but that bombed out with FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
  • Using webpack, but that was an all round fail with mostly cjs errors.

my index.html:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
    &lt;title&gt;Node.js + Uppy Example&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;noscript&gt;The app requires JavaScript to be enabled.&lt;/noscript&gt;
    &lt;button id=&quot;modalVisit&quot;&gt;Upload To Visit&lt;/button&gt;
    &lt;script src=&quot;./upload_to_visit.js&quot; type=&quot;module&quot;&gt;&lt;/script&gt;
    &lt;noscript&gt;The app requires JavaScript to be enabled.&lt;/noscript&gt;
    &lt;button id=&quot;modalDataset&quot;&gt;Upload To Dataset&lt;/button&gt;
    &lt;script src=&quot;./upload_to_dataset.js&quot; type=&quot;module&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

my upload_to_visit.js:

import Uppy from &#39;@uppy/core&#39;
import Dashboard from &#39;@uppy/dashboard&#39;;
import Tus from &#39;@uppy/tus&#39;
import GoldenRetriever from &#39;@uppy/golden-retriever&#39;;

import &#39;@uppy/core/dist/style.css&#39;
import &#39;@uppy/drag-drop/dist/style.min.css&#39;;
import &#39;@uppy/dashboard/dist/style.min.css&#39;;

const uppy = new Uppy({
    debug: true,
    autoProceed: false,
})

uppy.use(Dashboard, {
    inline: false,
    trigger:&quot;#modalDataset&quot;,
    target: &#39;body&#39;,
    note:&quot;to upload to a dataset&quot;,
    proudlyDisplayPoweredByUppy: false,
    showProgressDetails: true,
    fileManagerSelectionType:&quot;files&quot;,
})

uppy.use(Tus, { endpoint: &#39;http://127.0.0.1:3000/files/datasets/&#39; });
uppy.use(GoldenRetriever);

(I'm aware the the URL will need to change to accommodate the docker network)

my package.json:

{
  &quot;name&quot;: &quot;node_uppy&quot;,
  &quot;version&quot;: &quot;0.0.0&quot;,
  &quot;type&quot;: &quot;module&quot;,
  &quot;dependencies&quot;: {
    &quot;@uppy/core&quot;: &quot;^3.3.1&quot;,
    &quot;@uppy/drag-drop&quot;: &quot;^3.0.2&quot;,
    &quot;@uppy/dashboard&quot;: &quot;&quot;,
    &quot;@uppy/tus&quot;: &quot;^3.1.2&quot;,
    &quot;@uppy/golden-retriever&quot;: &quot;^3.1.0&quot;,
    &quot;formidable&quot;: &quot;^3.5.0&quot;
  },
  &quot;devDependencies&quot;: {
    &quot;npm-run-all&quot;: &quot;^4.1.3&quot;,
    &quot;vite&quot;: &quot;^4.0.0&quot;
  },
  &quot;private&quot;: true,
  &quot;scripts&quot;: {
    &quot;build&quot;: &quot;npm-run-all --parallel&quot;,
    &quot;start&quot;: &quot;npm-run-all --parallel start:client&quot;,
    &quot;start:client&quot;: &quot;vite&quot;
  }
}

答案1

得分: 1

将以下内容放入 Dockerfile 中:

FROM node:lts-alpine AS build
WORKDIR /app
COPY . .
RUN yarn install
RUN vite build
FROM nginx:stable-alpine
COPY --from=build /app/dist /usr/share/nginx/html

然后运行 docker build -t app .。这应该足够让你有一个良好的起点。

英文:

Put the following in a Dockerfile:

FROM node:lts-alpine AS build
WORKDIR /app
COPY . .
RUN yarn install
RUN vite build
FROM nginx:stable-alpine
COPY --from=build /app/dist /usr/share/nginx/html

Then run docker build -t app .. That should be enough to give you a head-start.

huangapple
  • 本文由 发表于 2023年8月8日 23:54:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定