Detaching volumes (for a fresh restart) when shutting down Docker

  docker, docker-compose, docker-volume

I have a Docker Compose (file is named my-main-db.yml) that creates a MySQL 8.0 container:

version: "3.7"
services:
  my-main-db:
    env_file:
      - .env
    image: mysql:8
    container_name: my-main-db
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: $MY_SERVICE_DB_ROOT_PASSWORD
      MYSQL_DATABASE: my_service_db_local
      MYSQL_USER: $MY_SERVICE_DB_APP_USER
      MYSQL_PASSWORD: $MY_SERVICE_DB_APP_PASSWORD
    volumes:
      - ./my-service-db-data:/var/lib/mysql

To fire this up I run:

docker-compose -f my-main-db.yml up

And it runs just fine, I can connect to it and also see it running via docker ps.

If I want to stop this container and keep all the data inside of it, I shut it down with:

docker-compose -f my-main-db.yml down

But if I want to stop this container and erase all the data inside of it, so that it starts up brand new (and clean), my understanding is that I need to detach or delete the volume. So I have been trying to shut it down like so:

docker-compose -f my-main-db.yml down -v

However, when I do this, and then start it back up, my data is still all there, whereas I was hoping for a brand new (empty) database.

Can anyone spot where I’m going awry?

Source: Docker Questions

LEAVE A COMMENT