Persistent databases in docker
Docker containers are ephemerals and they are not designed to store important data in.
Nevertheless here two methods on how to achieve this goal.
Paste it in docker-compose.yml
version: '3'
services:
web:
image: mitlabs/apache-php7.4
ports:
- 8080:80
volumes:
- .:/var/www/html
links:
- 'mysql'
mysql:
image: mariadb:latest
restart: always
volumes:
- mariadb-volume:/var/lib/mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: example
restart: always
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- 'mysql:db'
ports:
- 8081:8081
adminer:
image: adminer
restart: always
ports:
- 8082:8082
# volumes:
# mariadb-volume:
You can use any similar container for mysql:
version: '3'
services:
db:
image: mysql:5.7
container_name: db
environment:
MYSQL_ROOT_PASSWORD: my_secret_password
MYSQL_DATABASE: app_db
MYSQL_USER: db_user
MYSQL_PASSWORD: db_user_pass
ports:
- "6033:3306"
volumes:
- ./data/db:/var/lib/mysql
After creating file, just run the below command to launch container.
docker-compose up -d
Output:
Creating network "db_default" with the default driver
Creating db ... done
In this case the MySQL container creats all files on host machine under ./data/db
directory. To view these files, just run below command.
ll ./data/db
total 122940
drwxrwxr-x 4 999 jazio 4096 Nov 20 17:54 ./
drwxrwxr-x 3 jazio jazio 4096 Nov 20 17:52 ../
-rw-rw---- 1 999 docker 32768 Nov 20 17:54 aria_log.00000001
-rw-rw---- 1 999 docker 52 Nov 20 17:54 aria_log_control
-rw-rw---- 1 999 docker 976 Nov 20 17:54 ib_buffer_pool
-rw-rw---- 1 999 docker 12582912 Nov 20 17:54 ibdata1
-rw-rw---- 1 999 docker 100663296 Nov 20 17:54 ib_logfile0
-rw-rw---- 1 999 docker 12582912 Nov 20 17:54 ibtmp1
-rw-rw---- 1 999 docker 0 Nov 20 17:54 multi-master.info
drwx------ 2 999 docker 4096 Nov 20 17:54 mysql/
drwx------ 2 999 docker 4096 Nov 20 17:54 performance_schema/
##Data persistence
Option 1 Keep data in data volumes
Data volumes
volumes:
mariadb-volume:
This is the recommended method (donno why) You store the mysql data inside the container.
It will persists even if you run
docker-compose down
But you can DESTROY it if you run:
docker-compose down -v
Removing network flowera-drupal_default
WARNING: Network flowera-drupal_default not found.
Removing volume flowera-drupal_mariadb-volume
Note: when changing the password make sure you comment the volumes lines. It will affect the new user and keys even if you create fresh container. Remove mount location, as it will pick a user name and password from this location, also will not run your DB init script too.
You can vew anytime the volume with docker volume ls
##Option 2 – Storing MySQL Data on Host Machine
you can keep database files on the host machine. In any case docker container get terminated, you can relaunch container using the existing data files.
Create a directory to keep your MySQL data files. I am creating below directory structure under the current directory.
mysql:
image: mariadb:latest
restart: always
volumes:
- ./data/db:/var/lib/mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: mossad
restart: always
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- 'mysql:db'
ports:
- 8081:8081
adminer:
image: adminer
restart: always
ports:
- 8082:8082
mkdir -p ./data/db
Then configure docker-compose.yml
to use ./data/db
as volume to store all files created by the MySQL server. Next create compose file in current directory.
docker-compose.yml:
version: '3'
services:
db:
image: mysql:5.7
container_name: db
environment:
MYSQL_ROOT_PASSWORD: my_secret_password
MYSQL_DATABASE: app_db
MYSQL_USER: db_user
MYSQL_PASSWORD: db_user_pass
ports:
- "6033:3306"
volumes:
- ./data/db:/var/lib/mysql