Answers: <p>What about creating docker volumes yourself instead of letting docker to that for you? <a href="https://docs.docker.com/engine/reference/commandline/volume_create/" rel="nofollow noreferrer">https://docs.docker.com/engine/reference/commandline/volume_create/</a></p>
  
  <p>If one creates docker volumes yourself then the parent folder will not be automatically concatenated.</p>
  
Answers: CC BY-SA 3.0
Answers: 2018-02-14T21:19:48.270Z
Answers: 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.
Answers: Moritz
Answers: CC BY-SA 3.0
Answers: 2018-02-14T23:25:49.990Z
Answers: 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
Answers: Moritz
Answers: CC BY-SA 4.0
Answers: 2024-11-12T19:31:47.593Z
Answers: You have to add `external: true` as a sub-property under the volumes: volume. Then the name you give the volume is the _entire_ name instead of prepended with the directory name.
Answers: Andrew Arnott
Answers: CC BY-SA 3.0
Answers: 2018-02-11T18:58:16.703Z
Answers: 2018-02-11T18:58:16.703Z
Answers: 030
Answers: <p>Docker Prepends the current folder name with all the components name created using docker compose file.</p>
  
  <p>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 <strong>-p</strong> 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.</p>
  
  <p>The <strong>Solution</strong> to the above problem is using the <strong>name</strong> property as in below.</p>
  
  <pre><code>volumes: 
    data:
      driver: local
      name: mongodata
  
  networks: 
    internal-network:
      driver: bridge
      name: frontend-network
  </code></pre>
  
  <p>This volume can be referred in the service section as </p>
  
  <pre><code>services:
    mongo-database:
        volumes: 
          - data:/data/db
        networks: 
          - internal-network
  </code></pre>
  
  <p>The above <strong>name</strong> attribute will prevent docker-compose to prepend folder name.</p>
  
  <p>Note : For the container name one could use the property <strong>container_name</strong></p>
  
  <pre><code>services:
    mongo-database:
      container_name: mongo
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-11-27T10:25:11.330Z
Answers: 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.
Answers: Moritz
Answers: CC BY-SA 4.0
Answers: 2019-12-27T05:41:48.653Z
Answers: 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.
Answers: Shubhanshu Rastogi
Answers: CC BY-SA 4.0
Answers: 2019-11-27T08:59:53.353Z
Answers: 2019-11-27T08:59:53.353Z
Answers: Shubhanshu Rastogi
Body: <p>When defining named volumes in <code>docker-compose.yml</code>, 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?</p>
  
  <p>An example scenario would include the following <code>docker-compose.yml</code>:</p>
  
  <pre><code>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:
  </code></pre>
  
  <p>Where the <code>jekyll-data</code> named volume is populated by the following bash script:</p>
  
  <pre><code>docker run \
    --name helper \
    --volume="parent_folder_jekyll-data:/web" \
    -it busybox \
    true
  docker cp jekyll/web/. helper:/web
  docker rm helper
  </code></pre>
  
  <p>In the above case, <code>parent_folder</code> 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?</p>
  
  <p>The abridged output of <code>docker volume ls</code> where unnamed volumes have been removed:</p>
  
  <pre><code>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
  </code></pre>
  
Comments: CC BY-SA 3.0
Comments: 2018-02-11T16:05:34.230Z
Comments: `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?
Comments: 030
Comments: CC BY-SA 3.0
Comments: 2018-02-11T16:17:12.690Z
Comments: Related to https://devops.stackexchange.com/q/3275/5879 basically generate static site and then mount it in Nguni container via named volume
Comments: Moritz
Comments: CC BY-SA 3.0
Comments: 2018-02-11T16:22:05.387Z
Comments: Why not copying it?
Comments: 030
Comments: CC BY-SA 3.0
Comments: 2018-02-11T16:26:05.313Z
Comments: 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.
Comments: Moritz
Comments: CC BY-SA 3.0
Comments: 2018-02-11T16:27:46.977Z
Comments: Is it generally a good idea with a docker compose/swarm to have each container in its own folder?
Comments: Moritz
Comments: CC BY-SA 3.0
Comments: 2018-02-11T16:30:01.583Z
Comments: Perhaps something to mention in your question that you try to use it in a cluster.
Comments: 030
Comments: CC BY-SA 3.0
Comments: 2018-02-11T16:40:18.527Z
Comments: `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.
Comments: 030
Comments: CC BY-SA 3.0
Comments: 2018-02-11T16:45:23.743Z
Comments: I’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
Comments: Moritz
Comments: CC BY-SA 3.0
Comments: 2018-02-11T16:47:02.353Z
Comments: Could you add the outcome of `docker volume ls` to the question?
Comments: 030
Comments: CC BY-SA 3.0
Comments: 2018-02-11T22:12:16.183Z
Comments: The most stable solution might be to create a symlink and then copy the files in the build process *facepalm.jpg*
Comments: Moritz
Tags: <docker><docker-compose><volume>
Title: How to deal with docker compose's naming convention of named volumes?
UserDisplayName: Moritz
ThreadText: # How to deal with docker compose's naming convention of named volumes?
  
  (Asked by: username_0 on 2018-02-11)
  
  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
  
  ```
  
  **Tags:** docker, docker-compose, volume
  
  > **Comments:**
  > **username_1**: `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?  
  > **username_0**: Related to https://devops.stackexchange.com/q/3275/5879 basically generate static site and then mount it in Nguni container via named volume  
  > **username_1**: Why not copying it?  
  > **username_0**: 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.  
  > **username_0**: Is it generally a good idea with a docker compose/swarm to have each container in its own folder?  
  > **username_1**: Perhaps something to mention in your question that you try to use it in a cluster.  
  > **username_1**: `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.  
  > **username_0**: I’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  
  > **username_1**: Could you add the outcome of `docker volume ls` to the question?  
  > **username_0**: The most stable solution might be to create a symlink and then copy the files in the build process *facepalm.jpg*  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2019-11-27. Score: 10 | Accepted answer
  
  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
  
  ```
  
  > **Comments:**
  > **username_0**: 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.  
  > **username_3**: Yes username_0 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.  
  
  ---
  
  ### Answer by username_1 on 2018-02-11. Score: 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.
  
  > **Comments:**
  > **username_0**: 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.  
  > **username_0**: 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  
  > **username_2**: You have to add `external: true` as a sub-property under the volumes: volume. Then the name you give the volume is the _entire_ name instead of prepended with the directory name.  
  
  ---
  
  