16

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
10
  • 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?
    – 030
    Feb 11, 2018 at 16:05
  • Related to devops.stackexchange.com/q/3275/5879 basically generate static site and then mount it in Nguni container via named volume
    – Moritz
    Feb 11, 2018 at 16:17
  • Why not copying it?
    – 030
    Feb 11, 2018 at 16:22
  • Not 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.
    – Moritz
    Feb 11, 2018 at 16:26
  • Is it generally a good idea with a docker compose/swarm to have each container in its own folder?
    – Moritz
    Feb 11, 2018 at 16:27

2 Answers 2

10

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
2
  • 1
    This 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.
    – Moritz
    Nov 27, 2019 at 10:25
  • 1
    Yes 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. Dec 27, 2019 at 5:41
3

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.

2
  • I 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
    Feb 14, 2018 at 21:19
  • My 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
    Feb 14, 2018 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.