简体   繁体   English

无法在 Docker 上运行 React 应用程序

[英]Fail to run react application on Docker

problem问题

I'm making simple React application by webpack without create-react-app .我正在通过webpack制作简单的 React 应用程序,而没有create-react-app
Now I try to run this React application on Docker by commanding docker-compose up , but this is failed.现在我尝试通过命令docker-compose upDocker上运行这个 React 应用程序,但这失败了。
I want to know how to fix to run on Docker by docker-compose up .我想知道如何通过docker-compose up修复以在 Docker 上运行。

~/hospital master*
❯ docker-compose up
Starting hospital_client_1 ... done
Attaching to hospital_client_1
client_1  | 
client_1  | > client@1.0.0 start /app
client_1  | > webpack-dev-server --mode=development "0.0.0.0" "3000"
client_1  | 
client_1  | /app/node_modules/webpack-cli/bin/utils/convert-argv.js:547
client_1  |                             const i = content.indexOf("=");
client_1  |                                               ^
client_1  | 
client_1  | TypeError: content.indexOf is not a function
client_1  |     at /app/node_modules/webpack-cli/bin/utils/convert-argv.js:547:23
client_1  |     at Array.forEach (<anonymous>)
client_1  |     at processOptions (/app/node_modules/webpack-cli/bin/utils/convert-argv.js:546:11)
client_1  |     at processConfiguredOptions (/app/node_modules/webpack-cli/bin/utils/convert-argv.js:170:4)
client_1  |     at module.exports (/app/node_modules/webpack-cli/bin/utils/convert-argv.js:131:10)
client_1  |     at Object.<anonymous> (/app/node_modules/webpack-dev-server/bin/webpack-dev-server.js:84:40)
client_1  |     at Module._compile (internal/modules/cjs/loader.js:777:30)
client_1  |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:788:10)
client_1  |     at Module.load (internal/modules/cjs/loader.js:643:32)
client_1  |     at Function.Module._load (internal/modules/cjs/loader.js:556:12)
client_1  | npm ERR! code ELIFECYCLE
client_1  | npm ERR! errno 1
client_1  | npm ERR! client@1.0.0 start: `webpack-dev-server --mode=development "0.0.0.0" "3000"`
client_1  | npm ERR! Exit status 1
client_1  | npm ERR! 
client_1  | npm ERR! Failed at the client@1.0.0 start script.
client_1  | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
client_1  | 
client_1  | npm ERR! A complete log of this run can be found in:
client_1  | npm ERR!     /root/.npm/_logs/2019-12-02T13_52_45_192Z-debug.log
hospital_client_1 exited with code 1

what I tried我试过的

This React application successes to run in local environment(npm start).这个 React 应用程序成功运行在本地环境中(npm start)。

some codes一些代码

hospital
  ├ client
  │  ├ src
  │  │  ├ components
  │  │  │  └ Demo.tsx
  │  │  ├ App.tsx
  │  │  ├ index.html
  │  │  └ index.tsx
  │  ├ Dockerfile
  │  ├ package.json
  │  └ webpack.config.js
  └ docker-compose.yml

Dockerfile文件

FROM node:alpine
WORKDIR '/app'
COPY ./package.json ./
RUN npm install
COPY . .
EXPOSE 3000
RUN npm start

docker-compose.yml docker-compose.yml

version: '3'
services:
  client:
    command: 'npm start --host 0.0.0.0 --port 3000'
    build:
      dockerfile: Dockerfile
      context: ./client
    ports:
      - '3000:3000'
    volumes:
      - /app/node_modules
      - ./client:/app

webpack.config.js webpack.config.js

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  mode: 'development',

  entry: './src/index.tsx',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
  },

  devServer: {
    port: '3000',
    hot: true,
    open: true,
  },
  module: {
    rules: [
      {
        test: /\.js(x?)$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
      },
      {
        test: /\.ts(x?)$/,
        loader: 'ts-loader',
        exclude: /node_modules/,
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html',
    }),
  ],

  resolve: {
    extensions: ['.js', '.ts', '.tsx'],
  },
};

package.json包.json

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "webpack-dev-server",
    "start": "webpack-dev-server --mode=development"
  },

The entire code is here:整个代码在这里:
https://github.com/jpskgc/hospital https://github.com/jpskgc/hospital

Update更新

I fixed Dockerfile and docker-compose.yml in following way.我通过以下方式修复了DockerfileDockerfile docker-compose.yml
It called another error.它调用了另一个错误。

FROM node:alpine
WORKDIR '/app'
// ADD 
RUN apk add --no-cache bash
COPY ./package.json ./
RUN npm install
COPY . .
EXPOSE 3000
RUN npm start
version: '3'
services:
  client:
    // change command
    command: bash -c "npm start --host 0.0.0.0 --port 3000"
    build:
      dockerfile: Dockerfile
      context: ./client
    ports:
      - '3000:3000'
    volumes:
      - /app/node_modules
      - ./client:/app
❯ docker-compose up
Creating network "hospital_default" with the default driver
Creating hospital_client_1 ... done
Attaching to hospital_client_1
client_1  | internal/modules/cjs/loader.js:628
client_1  |     throw err;
client_1  |     ^
client_1  | 
client_1  | Error: Cannot find module '/app/bash'
client_1  |     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:625:15)
client_1  |     at Function.Module._load (internal/modules/cjs/loader.js:527:27)
client_1  |     at Function.Module.runMain (internal/modules/cjs/loader.js:840:10)
client_1  |     at internal/main/run_main_module.js:17:11 {
client_1  |   code: 'MODULE_NOT_FOUND',
client_1  |   requireStack: []
client_1  | }

looks like something is wrong with your docker-compose start up command because it fails on reading arguments:看起来您的 docker-compose 启动命令有问题,因为它在读取参数时失败:

/app/node_modules/webpack-cli/bin/utils/convert-argv.js:547
client_1  |                             const i = content.indexOf("=");

for that purposes i usually do like this:为此,我通常这样做:

command: bash -c "(some_command_here && some_second_command_here && npm start --host 0.0.0.0 --port 3000"

but since you are starting from FROM node:alpine you have to install bash first.但是由于您是从FROM node:alpine开始的,因此您必须先安装bash so something like this should work for your Dockerfile:所以这样的事情应该适用于你的 Dockerfile:

RUN apk update
RUN apk upgrade
RUN apk add bash

The problem is that you are passing --host 0.0.0.0 --port 3000 to npm start instead of to webpack-dev-server .问题是您将--host 0.0.0.0 --port 3000传递给npm start而不是webpack-dev-server You can move those arguments to package.json .您可以将这些参数移动到package.json

The Dockerfile needs some adjustments too. Dockerfile需要一些调整。

Dockerfile文件

FROM node:alpine

RUN mkdir -p /app
COPY . /app
WORKDIR /app
RUN npm install

EXPOSE 3000/tcp
CMD ["npm", "start"]

docker-compose.yml docker-compose.yml

version: '3'
services:
  client:
    build:
      dockerfile: Dockerfile
      context: ./client
    ports:
      - '3000:3000'
    volumes:
      - ./client:/app

package.json包.json

{
  "name": "client",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "webpack-dev-server",
    "build": "webpack --mode development",
    "start": "webpack-dev-server --mode development --host 0.0.0.0 --port 3000"
  },
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM