Untitled

Docker has two options for containers to store files on the host machine, so that the files are persisted even after the container stops: volumes, and bind mounts.

Volumes

Volumes are stored in a part of the host filesystem which is managed by Docker (/var/lib/docker/volumes/ on Linux). Non-Docker processes should not modify this part of the filesystem. Volumes are the best way to persist data in Docker.

Commands

Start a container with a volume

docker run -d --name devtest -v myvol2:/app nginx:latest

Use docker inspect devtest to verify that Docker created the volume and it mounted correctly. Look for the Mounts section.

Use a volume with Docker Compose

The example below shows a single Docker Compose service with a volume:

services:
  frontend:
    image: node:lts
    volumes:
      - myapp:/home/node/app
volumes:
  myapp: