Search code examples
dockersqlite

sqlite3 docker container can't create or read db file


Download the Docker tar.gz binaries and extract all binaries, execute ./dockerd & directly as root, and pull the sqlite3 image docker pull keinos/sqlite3:latest. (Can't install docker simply by apt install docker, its a public machine of our lab)

Run the following command in the doc:

$ docker run --rm -it -v "$(pwd):/workspace" -w /workspace keinos/sqlite3
SQLite version 3.28.0 2019-04-16 19:49:53
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .open ./sample.db
sqlite> CREATE TABLE table_sample(timestamp TEXT, description TEXT);
sqlite> INSERT INTO table_sample VALUES(datetime('now'),'First sample data. Foo');
sqlite> INSERT INTO table_sample VALUES(datetime('now'),'Second sample data. Bar');
sqlite> .quit
$ ls
sample.db

but can't open ./sample.db as the error message shown:

Error: unable to open database "sample.db": unable to open database file

I have tried changing the mounted volume dir mode to 666 and 777, not gonna help. If I create a .db file as touch sample.db before running docker image, then it still pop up an error:

Runtime error: attempt to write a readonly database (8)

What should I do, is the sqlite3 not supposed to be used in docker at all? Or the way I launch the docker is not compatible with this sqlite3 docker image user privilege?


Solution

  • Thanks to the point provided by @David Maze, the problem is this image use a default user sqlite with user and group id as:

    root@b8316:/home/guest/programfiles/docker# docker run --rm -it keinos/sqlite3 /bin/sh
    / $ id
    uid=101(sqlite) gid=102(sqlite) groups=102(sqlite)
    

    while the host mounted dir has user and group id 1000, therefore, changing the user and group id of container user is the right way:

    root@b8316:/home/guest/programfiles/docker# docker run --rm -it --user 1000:1000 -v "/home/guest/workspace:/workspace" -w /workspace keinos/sqlite3
    SQLite version 3.47.2 2024-12-07 20:39:59
    Enter ".help" for usage hints.
    Connected to a transient in-memory database.
    Use ".open FILENAME" to reopen on a persistent database.
    sqlite> .open sample.db
    sqlite> CREATE TABLE table_sample(timestamp TEXT, description TEXT);
    sqlite> INSERT INTO table_sample VALUES(datetime('now'),'First sample data. Foo');
    sqlite> INSERT INTO table_sample VALUES(datetime('now'),'Second sample data. Bar');
    sqlite> .table
    table_sample