17

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
030
  • 13,383
  • 17
  • 76
  • 178
Moritz
  • 1,227
  • 2
  • 11
  • 24

2 Answers2

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
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.

030
  • 13,383
  • 17
  • 76
  • 178