Requirements: SQLite is commonly used for embedded platforms, and this article uses containers for testing. Nodejs and Python are used to test how the container works and how data is shared. The test code is network-derived, but modified.

Nodejs environment

Create a project directory. Run container:

sudo docker run -itd --rm --name node -v $PWD:/home/node node:alpine sh

sudo docker exec -it node sh
Copy the code

The node version of the container is 14.2.0

Sudo NPM I-g n Sudo n 14.2.0Copy the code

Install sqlite:

sudo npm install sqlite3
Copy the code

Write database core code:

/* Note 1: Node is asynchronous and cannot create tables or insert data sequentially. */ var sqlite3 = require('sqlite3').verbose(); var db; db = new sqlite3.Database("db.db", function(err) { if (err) throw err; }); console.log("db: ", db) db.run(`create table IF NOT EXISTS user (id INT,name VARCHAR,password VARCHAR)`, function( err ) { if (err) throw err; console.log("Create Table Success!" ); }); // Run Insert Data db.run(`insert into user values (666,"admin","admin")`, function(err) { if (err) throw err; console.log("Insert Data Success!" ); }); db.close(function(err) { if (err) throw err; });Copy the code

Note that since nodejs is asynchronous, you might be prompted that the user table does not exist, and execute it multiple times. This article is only a demonstration, not a practical guide.

Query database core code:

var sqlite3 = require('sqlite3').verbose(); var db; db = new sqlite3.Database("db.db", function(err) { if (err) throw err; }); function show() { console.log("inside...." ) db.all("select * from user", function(err, rows) { if (err) throw err; console.log(rows); }); setTimeout(show, 1000); } show(); /* db.close(function(err) { if (err) throw err; }); * /Copy the code

Query database and print data every 1 second.

Test conclusion: host computer write, read database, pass. The host machine writes the database, the container reads the database, and passes. The container failed to write to the database. Execute prompt segment error Segmentation faults in the container. Try executing in a pure Docker directory, as well.

Nodejs arm environment

This section describes only the environment setup. Requirements: Implement nodeJS container on ARM platform, including KOA, SQLITe3. Run the ARM container on x86.

docker pull arm32v7/node:10-slim

docker run -itd --rm -v $PWD:/home/node -p 3000:3000 --name nodejsbuild arm32v7/node:10-slim sh  
docker exec -it nodejsbuild sh

Copy the code

Installation:

npm install sqlite3
Copy the code

Error:

npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! [email protected] install: 'node-pre-gyp install --fallback-to-build' NPM ERR! Exit status 1 npm ERR! npm ERR! Failed at the [email protected] install script.npm ERR! This is probably not a problem with npm. There is likely additional logging output above.Copy the code

Analysis:

node-pre-gyp WARN Using needle for node-pre-gyp https download node-pre-gyp WARN Tried to download(403): https://mapbox-node-binary.s3.amazonaws.com/sqlite3/v4.2.0/node-v64-linux-arm.tar.gz node - pre - gyp WARN the pre - built Binaries not found for [email protected] and [email protected] (Node-v64 ABI, glibc) (falling back to source compile with node-gyp) // !! The ARM platform is not precompiled, so you need to install Gyp ERR from source! find Python // !! Python Gyp ERR is not installed! find Python Python is not set from command line or npm configuration gyp ERR! find Python Python is not set from environment variable PYTHON gyp ERR! build error gyp ERR! stack Error: not found: make // !! Tools required for compilation are not installedCopy the code

Solution:

Apt-get install python3 ln -s /usr/bin/python3 /usr/bin/python//python/apt-get install build-essentialCopy the code

Make a memo:

cd /home/latelee/nodejs/node_arm/node docker build -t nodejsapp . docker tag nodejsapp registry.cn-hangzhou.aliyuncs.com/latelee/nodejsapp:armweb docker push registry.cn-hangzhou.aliyuncs.com/latelee/nodejsapp:armweb docker run -itd --rm -p 9000:3000 --name nodejsapp1 nodejsapp  docker run -itd --rm -p 3000:3000 --name nodejsapp1 registry.cn-hangzhou.aliyuncs.com/latelee/nodejsapp:armweb docker load -i nodejsapp.img docker run -itd --rm -p 3000:3000 -v /mnt/data:/mnt/data --name nodejsapp1 registry.cn-hangzhou.aliyuncs.com/latelee/nodejsapp:armweb docker run -itd --rm -p 3000:3000 -v /mnt/aaa/nodejsapp/node:/home/node -v /mnt/data:/mnt/data --name nodejs nodebase docker run -itd --rm -p 3000:3000 -v /mnt/data:/mnt/data --name nodejsapp nodejsapp docker run -itd --rm -p 9000:3000 -v $PWD/data:/mnt/data --name nodejsapp Nodejsapp (Note: Export port 3000 for easy access by other hosts. Mount/MNT /data to access the database.Copy the code

Python environment

Run container:

Docker run -itd --rm --name python -v $PWD:/home/python python:3.5-slim-stretch sh docker exec it python shCopy the code

Note: This image already includes the SQlite3 library and no additional installation is required.

Write database core code:

import sqlite3

conn = sqlite3.connect("db.db")
cursor = conn.cursor()
cursor.execute("insert into user values (777,\"python11\",\"adminpython\")")

cursor.close()
conn.commit()
conn.close()

Copy the code

Query database:

import sqlite3

conn = sqlite3.connect("db.db")
cursor = conn.cursor()
cursor.execute("select * from user")
values = cursor.fetchall()
print(values)

cursor.close()
conn.commit()
conn.close()
Copy the code

Conclusion: Same as above, but you can write to a database in a container. In addition, python container write database, nodeJS container query database, normal.

summary

Sqlite requires only database files. Suitable for small systems. This article’s tests found that the NodeJS container could not write to the database (including creating tables). Databases can be manipulated across containers as long as the database files are the same.