Create named docker volume with docker-compose?

I tried this solution and for me works

version: '3.1'

services:
  alp:
    image: alpine
    volumes:
        - my-jenkins-volume:/your/local/path
    command: sleep 10000


volumes:
    my-jenkins-volume:
        external: false

external true if you provide your volume from an external source, not directly from the docker-compose spec

Reference with the doc https://docs.docker.com/compose/compose-file/#volume-configuration-reference


Yes, it's possible. In fact, I think it's better to create it in the docker compose. Here is an example of one of my docker-compose.yml:

version: '2'
services:

  db:
    image: mysql
    restart: always
    volumes:
      - "wp-db:/var/lib/mysql:rw"

volumes:
  wp-db: {}

Here, the named volume is "wp-db" Then, if you want to use your volume in another docker-compose.yml file, you can using the "external" keyword:

volumes:
    my-jenkins-volume:
        external: true

(note: this is in the top-level volume keyword, not in the service section)