-2

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?

2
  • 1
    ./mysql_206:/var/lib/mysql is a bind mapping which means that the data is stored on your disk in the ./mysql_206 directory. It's not a volume. I'm confused as why you try to delete a volume called mysql-data. Where are you getting that name from? It's nowhere in your compose file. Commented Apr 17 at 22:45
  • 1
    docker-compose down also doesn't normally delete named volumes (declared in a top-level volumes: block in the Compose file); you specifically need a docker-compose down -v option to tell it to delete persistent data. Commented Apr 17 at 23:28

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.