Accessing a db inside a docker container

I’ve successfully created a container that has a Python program that received data from a serial port and writes it down to an SQLite database.

The database is created, I guess, automatically by the Python script when you launch the sqlite3.connect() method. When I was testing the code on my DEV environment i was able to surf the db with a GUI software such as dbeaver.

Now, can I access the single .db file from the outside (basically is it saved on some folder inside docker) with dbeaver or do I need to create a volume from docker and save there the .db to access it?

Thanks

Good evening Nico,

I would store the db-file outside of the container. This is also a good point for data-persistence because a container might be killed and recreated resulting in loosing your sqlite.db otherwise.
So copy the db-file from the existing container (because it is already filled with some data) using the docker cp accrding to the following example:

docker cp <containername/containerid>:/path/to/sqlite.db ./

Afterwards you can stop and start the container again include mounting this copied sqlite.db-file to its original location as in this docker-compose.yml:

version: "3.2"

services:
  nginx:
    image: ....
....
    volumes:
      - ./sqlite.db:/path/to/sqlite.db
....

Hope it helps? :slight_smile:

1 Like

Thanks, that helps!

Just one question. You mentioned copying a database from the outside but, after having added/deleted row/columns, or updated the database copied inside the docker container, does it update also the database outside the container?

Don’t know if I got your question right:
You are copying the sqlite.db from within the container to your Docker-host.
Then you recreate the container and mount (not copy) the sqlite.db (now residing on the Docker-host) into the new container - so the file usable file inside the container and the one on your Docker-host are identical (not two file with the same content - it is the same file).
So if you modify the file inside the container you are modifying the one on your Docker-host (and vice versa).

1 Like

Ok, I may have understood.

Since I need, after launching the container, that everything should be done autonomously, is it possible to load the volume, even if it’s empty, and then my script will create sqlite.db if not existing on the volume or it will use the already existing one?

If you mount a non-existing file into a container it will not work: a directory named as your non-existing file is created on your Docker-host.
So for sqlite you have to create an empty file before starting the container for the first time - otherwise you will run into problems described above.

Or instead of mounting the single sqlite.db-file into the container you can mount a directory into the container. Within the container you can create the sqlite.db-file as usual.

I think mounting an empty directory and then using it to create/store the sqlite.db is the best solution.
Thanks for the head up, I’ll give it a try