2023-10-09

How to keep an empty database the database docker container is started again

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?



No comments:

Post a Comment