43

I'm on docker version 1.11.2. I'm able to create named docker volumes:

docker volume create --name my-jenkins-volume

Than I'm able to connect my container with the named-volume with the -v option:

docker run -d -u jenkins --name jenkins -p 50000:50000 -p 443:8443 -v my-jenkins-volume:/var/jenkins_home

Is it possible to create this named volume in docker-compose?

lvthillo
  • 28,263
  • 13
  • 94
  • 127

4 Answers4

51

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

Alexis.Rolland
  • 5,724
  • 6
  • 50
  • 77
GianArb
  • 1,604
  • 1
  • 14
  • 17
  • 9
    thanks for your answer. I tried it but it seems that the named volume must exist before I can perform docker-compose up -d: Volume my-jenkins-volume declared as external, but could not be found. I want to know if it's possible to create this named volume with docker-compose and after that, use it – lvthillo Jun 08 '16 at 16:14
  • 3
    Future readers: the comment above was written when the answer stated to use `external: true`. This has been amended to `external: false` - which will create the volume if not exists. See: https://docs.docker.com/compose/compose-file/07-volumes/#external – M. Simon Apr 11 '23 at 05:46
7

Using the latest compose version (3) you can create your volumes automatically.

With your example :

version: '3'
services:
  jenkins:
    image: jenkins/jenkins
    container_name: 'jenkins'
    volumes:
      - jenkins_home:/var/jenkins_home
    ports:
      - '8080:8080'
      - '50000:50000'
    networks:
      - 'ci'
    environment:
      TZ: 'Europe/Paris'
    restart: unless-stopped
volumes:
  jenkins_home:

If you specify external: true you have to create the volume externally, using the command that you specified.

Michaël COLL
  • 1,153
  • 13
  • 18
5

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)

toussa
  • 1,058
  • 12
  • 21
  • should the volume referenced from the other compose file not have the same name then? how would `my-jenkins-volume` know that it should map to the `wp-db` volume (which is external from its perspective)? – Cee McSharpface Jun 14 '23 at 14:35
5

I personally find the long syntax that was added in version 3.2 more clear then the old short syntax.

In the example below we can see a named volume (mydata) being used by the web service with the long syntax.

The db service also uses a named volume called dbdata (second path under db service volumes), but defines it using the old string format for mounting a named volume:

version: "3.8"
services:
  web:
    image: nginx:alpine
    volumes: # Long/new syntax below this block
      - type: volume
        source: mydata
        target: /data
        volume:
          nocopy: true

  db:
    image: postgres:latest
    volumes: 
      - "dbdata:/var/lib/postgresql/data" # <--- Short/old syntax

volumes:
  mydata:
  dbdata:

(*) Notice that named volumes must be listed under the top-level volumes key.
(**) Read more in here.

muya_
  • 768
  • 7
  • 21
Rot-man
  • 18,045
  • 12
  • 118
  • 124