There is the following code in my docker-compose.yml
mariadb:
image: mariadb:10.8.2
restart: always
environment:
MARIADB_ROOT_PASSWORD: test
MARIADB_DATABASE: mydb
ports:
- "3306:3306"
container_name: mariadb
networks:
- my-network
then I used the following command to start a Docker container
docker stop mariadb
docker rm mariadb
docker-compose up -d --remove-orphans mariadb
sleep 15 # wait until database is fully started
docker exec mariadb mysql -u root -ptest -e "CREATE DATABASE product;"
After starting the Docker container, i.e. the command docker-compose up -d --remove-orphans mariadb
, the next command is to create a database called product
. However, problem is that there is the following error ERROR 1007 (HY000) at line 1: Can't create database 'dictionary'; database exists
Question: Why does the previously created database still exists even though the container and image are removed at the beginning? How to create the container for the second time without an existing database?
2
Answers
You did not post your compose file, but I can guess that you mapped MariaDB data folder to external volumes, so running again compose, you get the old DB data.
There are anonymous volumes created with VOLUME in dockerfiles.
It’s odd that you mix docker compose with docker commands. Use
docker compose down -v
to remove volumes and anonymous volumes.Additionally, nowadays consider using
docker compose
v2 instead of now olddocker-compose
.