我是docker的新手,我想在docker内部构建一个带有typescript的react应用,为此我需要使用语义UI反应。我按照此处的说明通过应用程序进行构建:https://mherman.org/blog/dockerizing-a-react-app/
我使用npm i -g semantic-ui-react
添加了语义ui,这是我的App.tsx:
import React from 'react';
import './App.css';
import {Button} from "semantic-ui-react";
class App extends React.PureComponent {
render() {
return (<div>
This is text
<Button>
ok
</Button>
</div>)
}
}
export default App;
当我打开http://localhost:3001时出现此错误:
./src/App.tsx
Module not found: Can't resolve 'semantic-ui-react' in '/app/src'
我尝试使用npm构建docker映像,然后更改为yarn认为可以解决。到目前为止我使用过的构建命令:
docker-compose build --no-cache && docker-compose up -d --force-recreate
docker-compose build --no-cache
docker-compose up -d --force-recreate --build
我的Dockerfile
# base image
FROM node:12.2.0-alpine
# set working directory
WORKDIR /app
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install and cache app dependencies
COPY package.json /app/package.json
RUN cat package.json
RUN yarn install
# start app
CMD ["yarn", "start"]
我的docker-compose.yml:
version: '2.2'
volumes:
frontend:
services:
frontend:
container_name: container
build:
context: .
dockerfile: Dockerfile
volumes:
- '.:/app'
- '/app/node_modules'
ports:
- '3001:3000'
environment:
- NODE_ENV=development
我正在使用Ubuntu 18.04
在我的Dockerfile中,我添加了cat package.json
以查看是否添加了语义ui。
Step 5/7 : RUN cat package.json
---> Running in b6bbcda3221b
{
"name": "frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"@types/jest": "^24.0.0",
"@types/node": "^12.0.0",
"@types/react": "^16.9.0",
"@types/react-dom": "^16.9.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.0",
"semantic-ui-react": "^0.88.2",
"typescript": "~3.7.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
我已经从终端执行了yarn start
,没有错误,并且在浏览器中打开该项目时,它也能正常运行。
为什么docker无法找到语义ui模块?
显然,这是Docker-compose: node_modules not present in a volume after npm install succeeds的副本
FrederikNS的答案也适用于此。我删除了
[- '/app/node_modules'
来自docker-compose.yml内部的卷,现在它可以识别所有模块