简体   繁体   中英

What does a root level empty object for volume means in `docker-compose.yml`?

I am trying to learn volume in docker-compose.yml , and I came across this code from this example:

https://docs.docker.com/compose/wordpress/

version: "3.9"

services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
volumes:
  # Make db_data persistant
  db_data: {}

For the global volume db_data , can I understand it as mySQL data(from /var/lib/mysql) will be saved in an object called db_data ? If this understanding is wrong, what is the correct meaning for this code?

Please find more on the official Docker documentation

Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. While bind mounts are dependent on the directory structure and OS of the host machine, volumes are completely managed by Docker. Volumes have several advantages over bind mounts:

You can imagine db_data as a hard drive. Yes, like a physical hard disk that you can move from left to right, you can backup, it has its own lifecycle.

I consider has its own lifecycle the most important thing, because it allows you to Version, backup or manipulate that data as an artifact , independent of the container instance or container image.

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM