📅  最后修改于: 2023-12-03 14:40:49.091000             🧑  作者: Mango
Docker Compose Volumes provide a way to persist data across containers and services.
Docker Compose Volumes allow you to share data between containers and services by creating a shared filesystem that can be accessed by multiple containers. This allows you to store and persist data even if a container is removed or restarted.
To create a Docker Compose Volume, you can simply add the volumes
key to your docker-compose.yml
file:
version: '3'
services:
web:
image: nginx:latest
volumes:
- my-volume:/usr/share/nginx/html
volumes:
my-volume:
In this example, we create a volume called my-volume
and mount it as a directory inside the web
container.
Once a volume is created, it can be shared between multiple services. To do this, simply reference the same volume in the volumes
key of each service:
version: '3'
services:
db:
image: postgres:latest
volumes:
- my-volume:/var/lib/postgresql/data
web:
image: nginx:latest
volumes:
- my-volume:/usr/share/nginx/html
volumes:
my-volume:
In this example, we create a volume called my-volume
and share it between a db
service running Postgres and a web
service running Nginx.
Docker Compose Volumes allow you to persist data even if a container is removed or restarted. This can be achieved by using a named volume, as shown in the above examples.
Named volumes are stored in a location on the host filesystem and are not deleted when a container is removed. This makes them ideal for persisting data that should survive container restarts, such as database data.
Docker Compose Volumes provide a way to share data between containers and services, and to persist data even if a container is removed or restarted. They are an essential tool for building complex containerized applications, and are easy to use and configure.