dockerdocker-composedocker-volumedocker-containerdocker-network

Volume mysql-data keeps getting recreated automatically in Docker Compose


I'm facing a strange behavior with Docker Compose and a MySQL container. Even after I run docker-compose down to stop and remove the container and network, the volume seems to be automatically recreated the next time I run docker-compose up -d.

docker-compose.yml file:

version: '3.9'
services:
  mysql_206:
    container_name: mysql_206
    hostname: mysql_206
    image: mysql:8
    restart: always
    command:
      - --authentication-policy=mysql_native_password
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_unicode_ci
      - --innodb_force_recovery=0
    volumes:
      - ./mysql_206:/var/lib/mysql
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: senha
      MYSQL_DATABASE: base_de_dados
      TZ: America/Sao_Paulo

I tried those things:

I ran the command: docker stop mysql_206 to stop the MySQL container.

I used docker-compose down to remove the container and network, expecting that all related resources would be deleted.

I attempted to manually remove the volume using docker volume rm mysql-data, but encountered the error message:

bash Copiar Editar Error response from daemon: get mysql-data: no such volume

Afterward, I ran docker-compose up -d again, and the system recreated the volume automatically as if it had never been removed.

How can I fix this?


Solution

  • Hans Kilian has already pointed out the issue. In your case

    volumes:
          - ./mysql_206:/var/lib/mysql
    

    Make it like following

    volumes:
          - mysql_206:/var/lib/mysql
    

    This will make it named volume. Whic means the volume will still remain. To remove it as David Maze mentioned, use command docker-compose down -v
    Otherwise if you want to keep bind mapping, after you take the container down manually delete ./msql_206 from your local and the bring it up again.