Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 660
lvthillo Avatar asked Jun 08 '16 14:06

lvthillo


People also ask

Does Docker compose create volumes?

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.

Where does Docker compose create volume?

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.

Does Docker compose create volume directories?

Running docker-compose up results in creation of directories specified via volumes directive on the host system.

How do I name a docker container while creating?

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.


2 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

like image 183
GianArb Avatar answered Oct 11 '22 03:10

GianArb


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)

like image 27
toussa Avatar answered Oct 11 '22 05:10

toussa