How do I load a database into MariaDB?

Hello,
The docker-compose.yml file looks like this:

db:
   image: mariadb:latest
   container_name: db
   restart: unless-stopped
   environment:
     MARIADB_ROOT_PASSWORD: rootsecret
     MARIADB_DATABASE: laravel_db
     MARIADB_USER: laravel_user
     MARIADB_PASSWORD: secret
   volumes:
     - mariadb_data:/var/lib/mysql
     - ./mysql/my.cnf:/etc/mysql/my.cnf
     - ./file.sql:/docker-entrypoint-initdb.d/file.sql
   ports:
     - "3306:3306"
   networks:
     - app_network

After the container was started, I entered it with the command docker exec -it db bash and ran the following command:

# mariadb -u laravel_user -psecret laravel_db -e "SHOW TABLES;"

+------------------------+
|...                     |
| form_fields            |
| form_orders            |
| frequencies            |
| gallery_products       |
| job_batches            |
| jobs                   |
| login_logs             |
| migrations             |
| option_form_orders     |
| password_reset_tokens  |
| permission_role        |
| permissions            |
| personal_access_tokens |
| products               |
| remotes                |
| roles                  |
| sessions               |
| shell_colors           |
| sizes                  |
| statuses               |
| tab_forms              |
| type_matrices          |
| type_orders            |
| units                  |
| users                  |
| vents                  |
| wax_guards             |
+------------------------+

I stopped the container using the docker compose down -v command and removed the line - ./file.sql:/docker-entrypoint-initdb.d/file.sql. I restarted the container and logged into it and ran the following command and it had no output:

# mariadb -u laravel_user -psecret laravel_db -e "SHOW TABLES;"
#

I have a few questions:

1- Should that line always be present in the compose file?

2- If that line exists, then new data will be lost every time the container is restarted.

Thank you.

Please read the doc, specifically “Initializing the database contents”.

You store the database data within a Docker volume:

You explicitly remove that volume in your down command:

That’s why your database is empty after restart.

Using -v will delete data stored in volumes, which is not recoverable unless backed up. Only use it when you’re sure you don’t need the persistent data anymore.

1 Like

Hi,
Thank you so much for your reply.