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?
Here's an example of a single Docker Compose service with a volume: services: frontend: image: node:lts volumes: - myapp:/home/node/app volumes: myapp: Running docker compose up for the first time creates a volume. The same volume is reused when you subsequently run the command.
These volumes are created inside /var/lib/docker/volume local host directory. As we can see, we don't have to specify the host directory. We just need to specify the directory inside the container. If we remove the volume instruction from the docker-compose.
Running docker-compose up results in creation of directories specified via volumes directive on the host system.
When a container is created using Docker, Docker provides a way to name the container using the the --name <container_name> flag. However, if no name is provided by the user while creating/running a Docker container, Docker automatically assigns the container a name.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With