在我的项目中,我需要 sqlite3 的 Dockerfile,因为我将微服务与 Kubernetes 和 Docker 一起使用,在 Windows 和 MacOS 上,安装时它给了我同样的错误。
该项目使用nodejs版本16,typeorm版本0.3.20,better-sqlite3版本^9.4.1,我还需要该项目的部署文件才能将其与Kubernetes一起使用。
这正是我尝试在本地计算机上安装时出现的错误:
# dalthon@MacBookPro in ~/Documents/project on git:main x [23:25:11] C:1
$ yarn
yarn install v1.22.19
[1/4] 🔍 Resolving packages...
[2/4] 🚚 Fetching packages...
[3/4] 🔗 Linking dependencies...
[4/4] 🔨 Building fresh packages...
[1/2] ⠄ better-sqlite3
error /Users/dalthon/Documents/project/node_modules/better-sqlite3: Command failed.
Exit code: 1
Command: prebuild-install || node-gyp rebuild --release
Arguments:
Directory: /Users/dalthon/Documents/project/node_modules/better-sqlite3
Output:
prebuild-install warn install No prebuilt binaries found (target=16.20.0 runtime=node arch=x64 libc= platform=darwin)
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | darwin | x64
gyp info find Python using Python version 3.12.1 found at "/Users/dalthon/.pyenv/versions/3.12.1/bin/python3"
gyp info spawn /Users/dalthon/.pyenv/versions/3.12.1/bin/python3
gyp info spawn args [
gyp info spawn args '/Users/dalthon/.nvm/versions/node/v16.20.0/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args 'binding.gyp',
gyp info spawn args '-f',
gyp info spawn args 'make',
gyp info spawn args '-I',
gyp info spawn args '/Users/dalthon/Documents/project/node_modules/better-sqlite3/build/config.gypi',
gyp info spawn args '-I',
gyp info spawn args '/Users/dalthon/.nvm/versions/node/v16.20.0/lib/node_modules/npm/node_modules/node-gyp/addon.gypi',
gyp info spawn args '-I',
gyp info spawn args '/Users/dalthon/Library/Caches/node-gyp/16.20.0/include/node/common.gypi',
gyp info spawn args '-Dlibrary=shared_library',
gyp info spawn args '-Dvisibility=default',
gyp info spawn args '-Dnode_root_dir=/Users/dalthon/Library/Caches/node-gyp/16.20.0',
gyp info spawn args '-Dnode_gyp_dir=/Users/dalthon/.nvm/versions/node/v16.20.0/lib/node_modules/npm/node_modules/node-gyp',
gyp info spawn args '-Dnode_lib_file=/Users/dalthon/Library/Caches/node-gyp/16.20.0/<(target_arch)/node.lib',
gyp info spawn args '-Dmodule_root_dir=/Users/dalthon/Documents/project/node_modules/better-sqlite3',
gyp info spawn args '-Dnode_engine=v8',
gyp info spawn args '--depth=.',
gyp info spawn args '--no-parallel',
gyp info spawn args '--generator-output',
gyp info spawn args 'build',
gyp info spawn args '-Goutput_dir=.'
gyp info spawn args ]
Traceback (most recent call last):
File "/Users/dalthon/.nvm/versions/node/v16.20.0/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py", line 42, in <module>
import gyp # noqa: E402
^^^^^^^^^^
File "/Users/dalthon/.nvm/versions/node/v16.20.0/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/__init__.py", line 9, in <module>
import gyp.input
File "/Users/dalthon/.nvm/versions/node/v16.20.0/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/input.py", line 19, in <module>
from distutils.version import StrictVersion
ModuleNotFoundError: No module named 'distutils'
gyp ERR! configure error
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack at ChildProcess.onCpExit (/Users/dalthon/.nvm/versions/node/v16.20.0/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:284:16)
gyp ERR! stack at ChildProcess.emit (node:events:513:28)
gyp ERR! stack at Process.ChildProcess._handle.onexit (node:internal/child_process:293:12)
gyp ERR! System Darwin 23.4.0
gyp ERR! command "/Users/dalthon/.nvm/versions/node/v16.20.0/bin/node" "/Users/dalthon/.nvm/versions/node/v16.20.0/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--release"
gyp ERR! cwd /Users/dalthon/Documents/project/node_modules/better-sqlite3
gyp ERR! node -v v16.20.0
gyp ERR! node-gyp -v v9.1.0
gyp ERR! not ok
为了解决这个错误,我发现修改并生成了这个使用node:16-slim的Dockerfile
FROM node:16-slim
# Instalar dependencias necesarias para compilar better-sqlite3
RUN apt-get update \
&& apt-get install -y \
python3 \
make \
g++ \
sqlite3 \
libsqlite3-dev \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY package*.json ./
RUN npm install --only=prod && npm cache clean --force
COPY . .
EXPOSE 5000
CMD ["npm", "start"]
访问数据库的kubernetes部署是这样的:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: sqliteproject-data-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: sqliteproject-depl
spec:
replicas: 1
selector:
matchLabels:
app: sqliteproject
template:
metadata:
labels:
app: sqliteproject
db: sqlite
spec:
containers:
- name: sqliteproject
image: dalthonmh/sqliteproject
imagePullPolicy: IfNotPresent
ports:
- containerPort: 5000
name: http
volumeMounts:
- name: sqlite-data
mountPath: /app/data
- name: sqlitebrowser
image: lscr.io/linuxserver/sqlitebrowser:latest
ports:
- containerPort: 3000
name: sqlite
- containerPort: 3001
name: sqlitessl
env:
- name: TZ
value: "America/Lima"
volumeMounts:
- name: sqlite-data
mountPath: /app/data
readOnly: false
volumes:
- name: sqlite-data
persistentVolumeClaim:
claimName: sqliteproject-data-pvc
---
apiVersion: v1
kind: Service
metadata:
name: sqliteproject-srv
spec:
selector:
app: sqliteproject
ports:
- name: http
protocol: TCP
port: 5000
targetPort: 5000
- name: sqlite
protocol: TCP
port: 3000
targetPort: 3000
当我需要访问 sqlite3 数据库时,我使用 kubernetes port-forward
kubectl port-forward podname 3000:3000
然后进入端口 3000 中的网络浏览器。当我第一次尝试修改数据时,出现错误“无法写入,因为仅用于读取”,要解决此问题,请启用对 /app/data 的完全访问权限因为它有initial-db.sqlite 文件:
kubectl exec -it podname -c ingresante -- //bin//bash -c "chmod -R 777 ./data"
注意:每次重启pod时,我都需要运行enable命令