First off, I’m completely new to docker and wsl. I found a great guide and are fully setup. I’ve moved my projects to the Ubuntu filesystem, and installed docker and it works perfectly. So does git.
Now, I created a quick and dirty app to test it out using mysql.
My docker-compose.yml file:
# Use root/example as user/password credentials
version: '3.1'
services:
db:
image: mysql
# NOTE: use of "mysql_native_password" is not recommended: https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password
# (this is just an example, not intended to be a production configuration)
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- 3306:3306
volumes:
- ./db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: example
adminer:
image: adminer
restart: always
ports:
- 8080:8080
My index.js file:
const express = require('express')
const mysql = require('mysql2/promise')
const app = express()
let db
async function go() {
db = await mysql.createConnection({
host: 'localhost',
port: 3306,
user: 'root',
password: 'example',
database: 'pets',
})
app.listen(3000)
}
go()
app.get('/', async (req, res) => {
const [users] = await db.execute('SELECT * FROM users')
console.log(users);
res.send(`<ul>${users.map(animal => `<li>${animal.name}</li>`).join('')}</ul>`)
})
As said, it works flawlessly.
However, now I want to add and commit the changes to git. And it reports this error:
➜ example-project-1 git:(master) ✗ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.js
modified: node_modules/.package-lock.json
modified: package-lock.json
modified: package.json
Untracked files:
(use "git add <file>..." to include in what will be committed)
db/
docker-compose.yml
node_modules/denque/
node_modules/generate-function/
node_modules/is-property/
node_modules/long/
node_modules/lru-cache/
node_modules/mysql2/
node_modules/named-placeholders/
node_modules/seq-queue/
node_modules/sqlstring/
no changes added to commit (use "git add" and/or "git commit -a")
➜ example-project-1 git:(master) ✗ git add -A
warning: could not open directory 'db/#innodb_temp/': Permission denied
warning: could not open directory 'db/performance_schema/': Permission denied
warning: could not open directory 'db/mysql/': Permission denied
warning: could not open directory 'db/sys/': Permission denied
warning: could not open directory 'db/#innodb_redo/': Permission denied
warning: could not open directory 'db/pets/': Permission denied
error: open("db/#ib_16384_0.dblwr"): Permission denied
error: unable to index file 'db/#ib_16384_0.dblwr'
fatal: adding files failed
➜ example-project-1 git:(master) ✗
Clearly it doesn’t have permission to the /db/ folder. So to my question:
Is this normal, and I should use some other folder structure to not have the database folder in the same folder I wanna commit? Or is there something wrong with my permissions?
As said I’m new to this so I don’t really know what is expected behavior and what’s not.
I have tried restarting docker, the pc, installed the vscode-docker extension, and using git commit from both vscode and windows terminal (Ubuntu-instance) – nothing worked so far
2
Answers
Run
ls -l
in your project to see owner of db folder. If you didn’t create it before starting your containers, it was created by docker (which most likely runs as root), so you current account doesn’t have permissions for it.On the other hand mysql stores its data at
/var/lib/mysql
(where your./db
is mapped by docker). Judging by your question I doubt that you really want to store datafiles in git.Yes, in most cases datafolder should be separate from your source code folder.
You do not want to store your database files in source control. They’re stored in on opaque binary format, and so Git (and basically every other system) will be very inefficient in storing them and you won’t be able to get meaningful diffs between commits.
One approach here is to include the
db
directory in a.gitignore
file. Literally include the name of the directory in the file on its own lineThis will cause
git add
to ignore it.Since the database files are in an opaque binary format that you can’t directly access, you may not even need them on the host system. Using a Docker named volume here will also store these files in a place Git can’t see them (and in some environments it is potentially faster). That’s a straightforward change in the
docker-compose.yml
file: