我正在尝试将docker用于我的项目,但是我遇到了问题; 无论我做什么,sqlite3都安装不正确,它包含在package.json中,我也尝试卸载并重新安装它,但它不起作用。 当我克隆项目并在Windows中运行npm i时,我遇到了相同的问题,但是使用npm i -S sqlite3再次安装它在Windows中工作,但不在Docker容器中。 有人遇到过同样的问题吗?
Docker-compose
version: '3'
services:
backend:
container_name: vacation_backend
build: "./backend"
image: backend:backend
volumes:
- ./:/app
ports:
- 3000:3000
network_mode: "bridge"
env_file: "backend/.env"
Dockerfile
FROM node:10-stretch
# COPY fix-proxy-apt /etc/apt/apt.conf.d/99fixbadproxy
RUN apt update && apt install -y apt-transport-https ca-certificates
RUN npm config set unsafe-perm
RUN npm i -g npm forever
WORKDIR /app/backend
COPY ./ /app/backend
RUN npm install
RUN npm uninstall --save sqlite3
RUN npm install --save sqlite3
RUN npm run build
EXPOSE 3000
WORKDIR /app/backend
COPY backend.sh /backend.sh
RUN chmod +x /backend.sh
ENTRYPOINT ["/backend.sh"]
package.json
{
"name": "project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "cross-env NODE_ENV=\"development\" nodemon --exec babel-node src/index.js",
"build": "babel src --out-dir dist",
"serve": "forever dist/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@babel/polyfill": "^7.4.4",
"@koa/cors": "^2.2.3",
"@types/sequelize": "^4.28.1",
"cron": "^1.7.1",
"googleapis": "^40.0.0",
"graphql": "^14.3.1",
"graphql-relay": "^0.6.0",
"graphql-sequelize": "^9.3.6",
"jsonwebtoken": "^8.5.1",
"koa": "^2.7.0",
"koa-bodyparser": "^4.2.1",
"koa-graphql": "^0.8.0",
"koa-router": "^7.4.0",
"ldapjs": "^1.0.2",
"moment": "^2.24.0",
"node-cron": "^2.0.3",
"nodemailer": "^6.2.1",
"sequelize": "^5.8.6",
"sqlite3": "^4.0.9"
},
"devDependencies": {
"@babel/cli": "^7.5.0",
"@babel/core": "^7.4.5",
"@babel/node": "^7.4.5",
"@babel/preset-env": "^7.4.5",
"babel-plugin-module-resolver": "^3.2.0",
"cross-env": "^5.2.0",
"forever": "^1.0.0",
"nodemon": "^1.19.0"
}
}
docker-compose build
Step 9/15 : RUN npm install --save sqlite3
---> Running in a72a4cbda37c
> sqlite3@4.0.9 install /app/backend/node_modules/sqlite3
> node-pre-gyp install --fallback-to-build
node-pre-gyp WARN Using request for node-pre-gyp https download
[sqlite3] Success: "/app/backend/node_modules/sqlite3/lib/binding/node-v64-linux-x64/node_sqlite3.node" is installed via remote
npm WARN project@1.0.0 No description
npm WARN project@1.0.0 No repository field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
+ sqlite3@4.0.9
added 70 packages from 65 contributors, updated 1 package and audited 7673 packages in 8.868s
found 1 low severity vulnerability
run `npm audit fix` to fix them, or `npm audit` for details
Removing intermediate container a72a4cbda37c
docker记录哈希值
Error: Please install sqlite3 package manually
at ConnectionManager._loadDialectModule (/app/backend/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:81:15)
at new ConnectionManager (/app/backend/node_modules/sequelize/lib/dialects/sqlite/connection-manager.js:22:21)
at new SqliteDialect (/app/backend/node_modules/sequelize/lib/dialects/sqlite/index.js:14:30)
at new Sequelize (/app/backend/node_modules/sequelize/lib/sequelize.js:320:20)
at Object.<anonymous> (/app/backend/dist/models/db.js:15:17)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/app/backend/dist/models/user.js:8:34)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/app/backend/dist/models/index.js:25:36)
at Module._compile (internal/modules/cjs/loader.js:701:30)
error: Forever detected script exited with code: 1
error: Script restart attempt #X
答案 0 :(得分:0)
今晚我遇到了类似的问题。问题可能是Node映像不包含SQLite。
要确认,您可以使用docker-compose up -d
启动应用以分离模式启动容器,使用exec
命令在容器中打开Bash以访问容器的命令lie,然后运行which sqlite3
命令在您的容器中以确认您没有安装它。
$ docker-compose up -d
$ docker-compose exec backend /bin/bash
$ which sqlite3
$ exit
如果运行which sqlite3
不返回任何输出,则说明您的容器中未安装SQLite。它已在Windows操作系统中运行,因为它已安装在Windows操作系统中。
要解决此问题,您需要在第4行的Dockerfile中安装SQLite。
Dockerfile第4行当前:
RUN apt update && apt install -y apt-transport-https ca-certificates
Dockerfile第4行已更新:
RUN apt update && apt install -y apt-transport-https ca-certificates sqlite3
我仍在尝试找出是否有更好的方法可以在Node容器中使用SQLite,但是我希望这会有所帮助!