Docker - 节点,找不到 mysql 模块
Docker - Node, cannot find mysql module
我正在尝试创建一个 docker 节点,mysql 并在前端做出反应。但是当我尝试导入 mysql 库时,出现以下错误:
const express = require("express");
const router = express.Router();
const mysql = require('mysql');
Error: Cannot find module 'mysql'
我已经查看了我的 node_modules 文件夹,那里有一个 mysql 目录。
这是我的 package.json:
{
"name": "api",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "node bin/www",
"dev": "./node_modules/.bin/nodemon bin/www"
},
"dependencies": {
"cookie-parser": "~1.4.3",
"cors": "^2.8.4",
"debug": "~2.6.9",
"express": "~4.16.0",
"http-errors": "~1.6.2",
"jade": "~1.11.0",
"morgan": "~1.9.0",
"mysql": "^2.18.1",
"node-mysql": "^0.4.2"
},
"devDependencies": {
"nodemon": "^1.14.12"
}
}
这是我的Dockerfile,我尝试在容器内全局安装mysql,但没有解决:
FROM mhart/alpine-node:12
WORKDIR /api
COPY package*.json /api/
RUN npm i -G nodemon
RUN npm install -G mysql
RUN npm install
COPY . /api/
EXPOSE 80
CMD ["npm", "run", "dev"]
docker-compose.yml
version: "3"
services:
client:
container_name: MY_api
image: mhart/alpine-node:12
build: ./client
restart: always
ports:
- "3000:3000"
working_dir: /client
volumes:
- ./client:/client
entrypoint: ["npm", "start"]
links:
- api
networks:
- MY_network
api:
container_name: MY_client
build: ./api
restart: always
ports:
- "9000:9000"
working_dir: /api
volumes:
- ./api:/api
- /api/node_modules
depends_on:
- mysqldb
networks:
- MY_network
mysqldb:
container_name: MY_mysql
image: mysql:8.0.19
restart: always
ports:
- "3307:3306"
environment:
- MYSQL_ROOT_PASSWORD=MY_PASS
- MYSQL_USER=USER
- MYSQL_PASSWORD=MY_PASS
- MYSQL_DATABASE=MY_DATABASE
networks:
- MY_network
networks:
MY_network:
driver: bridge
从 docker-compose 中删除已安装的卷 - /api/node_modules
。
并删除 Dockerfile 中的 RUN npm i -G nodemon
RUN npm install -G mysql
行。
一切正常
我正在尝试创建一个 docker 节点,mysql 并在前端做出反应。但是当我尝试导入 mysql 库时,出现以下错误:
const express = require("express");
const router = express.Router();
const mysql = require('mysql');
Error: Cannot find module 'mysql'
我已经查看了我的 node_modules 文件夹,那里有一个 mysql 目录。
这是我的 package.json:
{
"name": "api",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "node bin/www",
"dev": "./node_modules/.bin/nodemon bin/www"
},
"dependencies": {
"cookie-parser": "~1.4.3",
"cors": "^2.8.4",
"debug": "~2.6.9",
"express": "~4.16.0",
"http-errors": "~1.6.2",
"jade": "~1.11.0",
"morgan": "~1.9.0",
"mysql": "^2.18.1",
"node-mysql": "^0.4.2"
},
"devDependencies": {
"nodemon": "^1.14.12"
}
}
这是我的Dockerfile,我尝试在容器内全局安装mysql,但没有解决:
FROM mhart/alpine-node:12
WORKDIR /api
COPY package*.json /api/
RUN npm i -G nodemon
RUN npm install -G mysql
RUN npm install
COPY . /api/
EXPOSE 80
CMD ["npm", "run", "dev"]
docker-compose.yml
version: "3"
services:
client:
container_name: MY_api
image: mhart/alpine-node:12
build: ./client
restart: always
ports:
- "3000:3000"
working_dir: /client
volumes:
- ./client:/client
entrypoint: ["npm", "start"]
links:
- api
networks:
- MY_network
api:
container_name: MY_client
build: ./api
restart: always
ports:
- "9000:9000"
working_dir: /api
volumes:
- ./api:/api
- /api/node_modules
depends_on:
- mysqldb
networks:
- MY_network
mysqldb:
container_name: MY_mysql
image: mysql:8.0.19
restart: always
ports:
- "3307:3306"
environment:
- MYSQL_ROOT_PASSWORD=MY_PASS
- MYSQL_USER=USER
- MYSQL_PASSWORD=MY_PASS
- MYSQL_DATABASE=MY_DATABASE
networks:
- MY_network
networks:
MY_network:
driver: bridge
从 docker-compose 中删除已安装的卷 - /api/node_modules
。
并删除 Dockerfile 中的 RUN npm i -G nodemon
RUN npm install -G mysql
行。
一切正常