Create named docker volume with docker-compose?

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?

Answers


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)


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.


Need Your Help

ReactJS state vs prop

reactjs

This may be treading that line between answerable and opinionated, but I'm going back and forth as to how to structure a ReactJS component as complexity grows and could use some direction.