HiveBrain v1.2.0
Get Started
← Back to all entries
snippetdockerModerate

How to deal with docker compose's naming convention of named volumes?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
conventionvolumeswithdockernamingnamedcomposehowdeal

Problem

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

Solution

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

Code Snippets

volumes: 
  data:
    driver: local
    name: mongodata

networks: 
  internal-network:
    driver: bridge
    name: frontend-network
services:
  mongo-database:
      volumes: 
        - data:/data/db
      networks: 
        - internal-network
services:
  mongo-database:
    container_name: mongo

Context

StackExchange DevOps Q#3329, answer score: 10

Revisions (0)

No revisions yet.