Reputation: 30723
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?
Upvotes: 50
Views: 84806
Reputation: 22058
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.
Upvotes: 5
Reputation: 1098
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)
Upvotes: 6
Reputation: 1674
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
Upvotes: 57
Reputation: 1287
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.
Upvotes: 10