How to deal with docker compose's naming convention of named volumes?
Solution 1
Docker Prepends the current folder name with all the components name created using docker compose file.
Eg : If the current folder name containing the docker-compose.yml file is test, all the volumes,network and container names will get test appended to it. In order to solve the problem people earlier proposed the idea of using -p flag with docker-compose command but the solution is not the most feasible one as a project name is required just after the -p attribute. The project name then gets appended to all the components created using docker compose file.
The Solution to the above problem is using the name property as in below.
volumes:
data:
driver: local
name: mongodata
networks:
internal-network:
driver: bridge
name: frontend-network
This volume can be referred in the service section as
services:
mongo-database:
volumes:
- data:/data/db
networks:
- internal-network
The above name attribute will prevent docker-compose to prepend folder name.
Note : For the container name one could use the property container_name
services:
mongo-database:
container_name: mongo
Solution 2
What about creating docker volumes yourself instead of letting docker to that for you? https://docs.docker.com/engine/reference/commandline/volume_create/
If one creates docker volumes yourself then the parent folder will not be automatically concatenated.
Related videos on Youtube
![Moritz](https://i.stack.imgur.com/bB4KQ.jpg?s=256&g=1)
Comments
-
Moritz almost 2 years
When defining named volumes in
docker-compose.yml
, their names are prepended with their parent folder name. This causes a problem when scripts outside of docker compose have to interact with them. The question is, what is the best way to deal with this?An example scenario would include the following
docker-compose.yml
:version: "3" services: nginx: build: ./nginx container_name: nginx ports: - "80:80" volumes: - jekyll-data:/usr/share/nginx/html:ro networks: backend: volumes: jekyll-data:
Where the
jekyll-data
named volume is populated by the following bash script:docker run \ --name helper \ --volume="parent_folder_jekyll-data:/web" \ -it busybox \ true docker cp jekyll/web/. helper:/web docker rm helper
In the above case,
parent_folder
is the name of the parent folder. This means that moving the contents to a different folder would break the application. Is there a proper way to deal with this situation?The abridged output of
docker volume ls
where unnamed volumes have been removed:DRIVER VOLUME NAME local flaskthymedata_grafana-data local flaskthymedata_influxdb-data local flaskthymedata_postgres-data local veleda_grafana-data local veleda_influxdb-data local veleda_jekyll-cache local veleda_jekyll-data local veleda_postgres-data local veledaio_grafana-data local veledaio_influxdb-data local veledaio_jekyll-cache local veledaio_jekyll-data local veledaio_postgres-data
-
Admin over 6 years
Where the jekyll-data named volume is populated by the following bash script
Why do you use a bash script to create a docker volume? -
Admin over 6 yearsRelated to devops.stackexchange.com/q/3275/5879 basically generate static site and then mount it in Nguni container via named volume
-
Admin over 6 yearsWhy not copying it?
-
Admin over 6 yearsNot possible due to the folder structure. Jekyll and Nginx are sibling folders, each for their own container. COPY is not allowed to access files outside its own folder.
-
Admin over 6 yearsIs it generally a good idea with a docker compose/swarm to have each container in its own folder?
-
Admin over 6 yearsPerhaps something to mention in your question that you try to use it in a cluster.
-
Admin over 6 years
moving the contents to a different folder would break the application
. Are you planning to rename it? Docker-volumes are immutable. I have got the impression that you see the volumes as mutable. -
Admin over 6 yearsI’m not sure what you mean by that. The main thing is that the script becomes brittle because it depends on the parent folder’s name
-
Admin over 6 yearsCould you add the outcome of
docker volume ls
to the question? -
Admin over 6 yearsThe most stable solution might be to create a symlink and then copy the files in the build process facepalm.jpg
-
-
Moritz over 6 yearsI deleted all volumes, created volumes without the folder name prefix and then ran docker compose again. It recreated the volumes with the folder name prefix.
-
Moritz over 6 yearsMy bad, when resetting my environment I used docker system prune -a, but I would have had to run docker volume prune after that. Great solution to my problem
-
Moritz over 4 yearsThis is a useful solution since the naming scheme used by docker-compose changes. They used to remove hyphens in the parent folder name, but now include them. Your solution removes this abiguity.
-
Shubhanshu Rastogi over 4 yearsYes Moritz you are absolutely correct on your statement. The naming convention using folder name provided by Docker is also not feasible in Production. The reasoning that they provide ie you can run multiple containers for different environment (Developer and Production) based on the username is also not appealing to me. Maybe you can mark this as the accepted Answer as the above answer somehow does not provide the escape to the problem saved. Though it is quite informative on its part.