Skip to content

Instantly share code, notes, and snippets.

@Rafisto
Last active October 18, 2024 11:59
Show Gist options
  • Select an option

  • Save Rafisto/b7c8229e066f140088ea8b993666ae11 to your computer and use it in GitHub Desktop.

Select an option

Save Rafisto/b7c8229e066f140088ea8b993666ae11 to your computer and use it in GitHub Desktop.
Easily setup MariaDB for testing via docker compose

MariaDB with docker compose

  1. Install docker and docker-compose e.g. via
pacman -S docker docker-compose
  1. Start dockerd e.g. as a systemd process:
systemctl start docker
  1. Download the provided compose.yaml and run it via docker compose
curl https://gist.githubusercontent.com/Rafisto/b7c8229e066f140088ea8b993666ae11/raw/98ee2e47df36bc2647b88210eb68bd05e49e3517/compose.yaml -o compose.yaml
docker compose up -d # start the container in detached terminal mode

Tip

To add $USER to the docker group see docker post-install guide

  1. Execute an SQL shell inside a container
docker exec -it mariadb-container mariadb -D database -u root -prootpassword # double p is intentional, see mariadb reference
  1. Use SQL as you would normally do:
SHOW TABLES;

Add init .sql files to the container

  1. Add desired file/directory into compose.yaml to mount inside the container
services:
  mariadb:
    ...
    volumes:
      - db_data:/var/lib/mysql
      - ./your-sql-file.sql:/docker-entrypoint-initdb.d/init.sql
  1. Recreate the contianer and access the SQL console
docker compose up -d
docker exec -it mariadb-container mariadb -D database -u root -prootpassword
  1. You may now recreate the container or source the file inside the SQL console
-- run inside mariadb container
source /docker-entrypoint-initdb.d/init.sql

Remove / Reset container volume

Have you done something fatal to the database instance, you can always reset the container persistent storage via

docker compose down
docker volume rm cw_db_data
services:
mariadb:
image: mariadb:latest
container_name: mariadb-container
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: database
MYSQL_USER: user
MYSQL_PASSWORD: password
volumes:
- db_data:/var/lib/mysql
ports:
- "3306:3306"
networks:
- mariadb-network
volumes:
db_data:
networks:
mariadb-network:
driver: bridge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment