Docker + SQLite 3

in #docker4 years ago

Did you just started with Docker and have no idea how to introduce persistend data to your Docker container? Like everything is being destroyed once you rebuild or remove your container right? So how the hack you do handle like… databases? Well Docker has a solution for this. Let’s dive into that.

A container is something that sits on the host machine and is isolated from the host machine. Docker container is an OS running on top of another OS. Right? And once the container is volatile in terms of once you release a new version of your app you replace current container with a new one… do you see that? You just cannot store data inside the container. The container is just a worker that runs your code, but once it wants to store some data it cannot store them inside itself. So you do need to store them outside of your container. You want to store them on the host machine (your notebook, the production server, …).

How to achieve that? Quite simply. The only thing you need to do is create a directory/file on the host machine and link it to the container. Not link, but mount. Just like you mount USB disk to your notebook. Docker calls these
volumes

Ok, so let’s pretend you have a fully working container based on your image. Now we are gonna tell Docker to mount a file (for SQLite 3 you can go with a single file) from host machine to the container. Let’s do that in our compose file:

version: "2"
services:
    app:
        ...
        volumes:
            - /home/user/db.sqlite3:/var/www/app/db.sqlite3
        ...

This tells to Docker to grab file db.sqlite3 from /home/user directory and mount it to our container to path /var/www/app/db.sqlite3

Boom, done.

Sort:  

The best and most beautiful things in the world cannot be seen or even touched - they must be felt with the heart.

Coin Marketplace

STEEM 0.27
TRX 0.07
JST 0.033
BTC 23334.51
ETH 1832.78
USDT 1.00
SBD 3.23