Compose: Unable to get volumes to work with docker-compose in windows

Created on 3 Mar 2016  路  14Comments  路  Source: docker/compose

I am trying to setup a docker-compose but no matter what I do, I cannot get the volumes to work.

Here is what my docker-compose.yml

version: '2'

services:
    base:
        build: ./base
    ui:
        build: ./ui
        ports:
            - "80:80"
        volumes:
            - ./ui:/src

I have tried several different configurations of volume, but none have worked so far.

If I run my image like this then the volume is mounted correctly

docker run -it -v ui:/src 6d8e034c7fd4 /bin/bash

If I change the docker-compose.yml volume to look like this

volumes:
    - ui:/src

I get an error "Named volume "ui:/src:rw" is used in service "ui" but no declaration was found in the volumes section."

I am using Docker version 1.10.1, build 9e83765 on Windows 7

kinquestion

Most helpful comment

ui:/src and ./ui:/src mean two different things. The first one is using a named volume, with name ui. The second is a host volume with the path ./ui.

If you're trying to use a named volume, you must declare it in the volumes section (https://docs.docker.com/compose/compose-file/#volume-configuration-reference).

If you're trying to use a host volume, the equivalent with the docker cli is docker run -it -v $PWD/ui:/src ... (at least on osx and linux, I'm not sure what the variable is on windows).

Also note that if you're toolbox, only your home directory and subdirectories are shared with the vm, so the host volume must be under your home directory.

All 14 comments

ui:/src and ./ui:/src mean two different things. The first one is using a named volume, with name ui. The second is a host volume with the path ./ui.

If you're trying to use a named volume, you must declare it in the volumes section (https://docs.docker.com/compose/compose-file/#volume-configuration-reference).

If you're trying to use a host volume, the equivalent with the docker cli is docker run -it -v $PWD/ui:/src ... (at least on osx and linux, I'm not sure what the variable is on windows).

Also note that if you're toolbox, only your home directory and subdirectories are shared with the vm, so the host volume must be under your home directory.

Thanks for the response.

I am trying to setup a host volume, not a named volume.

If you're trying to use a host volume, the equivalent with the docker cli is docker run -it -v $PWD/ui:/src ... (at least on osx and linux, I'm not sure what the variable is on windows).

This must not be true on windows because the physical path of my code is C:\projects\new_project. ui is a folder under new_project. When I use

docker run -it -v ui:/src 6d8e034c7fd4 /bin/bash

I am able to see my code in the container

Also note that if you're toolbox, only your home directory and subdirectories are shared with the vm, so the host volume must be under your home directory.

Does this mean with docker compose my source has to be under my user directory?

Because when I use

docker run -it -v ui:/src 6d8e034c7fd4 /bin/bash

I am able to see my code in the container and it is not under my user directory. If they are different, then that could be throwing me off.

I tried moving my project files into C:\Users\rmclaughlin\projects\powerdialer-ui and I set my volume like this

volumes:
        - /c/users/rmclaughlin/projects/powerdialer-ui/ui:/src

I also tried this

volumes:
        - ./rmclaughlin/projects/powerdialer-ui/ui:/src

but both failed.

alright, I got it to work. I not only had to move my project under the user directory, but I had to run it from that location also. Now relative paths also work, like this

volumes:
    - ./ui:/src

As this is currently the first google result for "but no declaration was found in the volumes section", here is a bit more information. In case you actually like to mount an already existing docker volume (not a host directory) using docker-compose, you are dealing with what docker-compose calls an "external volume", which you have to specify in the docker-compose. yml in the following way. So again, let's assume there is already a docker volume with name _ui-volume_, then this would have to read:

version: '2'
services:
    base:
        build: ./base
    ui:
        build: ./ui
        ports:
            - "80:80"
        volumes:
            - ui-volume:/src
volumes:
    ui-volume:
        external: true

With Bash on Windows, if it can help, I got it to work with this:
``` volumes:

  • //c/Users/winuser/code/:/var/www/html

@justechn could you describe what you mean by "running it in that location?" I am encountering the same problem as yourself, tried the relative path, having the project under my user files, running docker-compose up from the root of my project. Still getting:
ERROR: for web_db Cannot create container for service web_db: Invalid bind mount spec "C:\\Users\\Daniel\\coding\\project\\database:/var/lib/mysql:rw": Invalid volume specification: 'C:\Users\Daniel\coding\project\database:/var/lib/mysql:rw'

with:

    volumes:
      - ./database/:/var/lib/mysql

I had to run docker from the project directory instead of giving docker the project path.

Instead of this:

c/users/rmclaughlin/projects/powerdialer-ui/ui/docker run

I did

cd c/users/rmclaughlin/projects/powerdialer-ui/ui
Docker run

And you aren't getting the error message when doing docker-compose up ?

I'm having the same. I have tried to set the volumes to:
//c/Users/k/dev/angular-seed/:/home/app/angular-seed
c/Users/k/dev/angular-seed:/home/app/angular-seed
.:/home/app/angular-seed
etc

I have also tried adding a .env folder with:
COMPOSE_CONVERT_WINDOWS_PATHS=1
COMPOSE_CONVERT_WINDOWS_PATHS=0

When I do docker-compose up I see everything from:
Recreating angular-seed-start Attaching to angular-seed-start angular-seed-start | npm info it worked if it ends with ok angular-seed-start | npm info using [email protected] angular-seed-start | npm info using [email protected] angular-seed-start | npm info lifecycle [email protected]~prestart: [email protected] angular-seed-start | npm info lifecycle [email protected]~start: [email protected] angular-seed-start | angular-seed-start | > [email protected] start /home/app/angular-seed angular-seed-start | > gulp serve.dev --color angular-seed-start | angular-seed-start | sh: 1: gulp: not found angular-seed-start | angular-seed-start | npm info lifecycle [email protected]~start: Failed to exec start script angular-seed-start | npm ERR! Linux 4.4.43-boot2docker angular-seed-start | npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
to
ERROR: Named volume "c/Users/k/dev/angular-seed:/home/app/angular-seed:rw" is used in service "angular-seed" but no declaration was found in the volumes section.
to
`Recreating 90b0444c8ee3_90b0444c8ee3_90b0444c8ee3_90b0444c8ee3_angular-seed-start

ERROR: for angular-seed Cannot create container for service angular-seed: invalid bind mount spec "C:\Users\k\dev\angular-seed\dev\angular-seed:/home/app/angular-seed:rw": invalid volume specification: 'C:\Users\k\dev\angular-seed\dev\angular-seed:/home/app/angular-seed:rw'
ERROR: Encountered errors while bringing up the project. to Recreating 90b0444c8ee3_90b0444c8ee3_90b0444c8ee3_angular-seed-start

ERROR: for angular-seed Cannot create container for service angular-seed: create \c\Users\k\dev\angular-seed: "\c\Users\k\dev\angular-seed" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intented to pass a host directory, use absolute path
ERROR: Encountered errors while bringing up the project.`

@krissm where you able to resolve your issue here?

In my case (I was trying to compose from pyCharm) adding .env didn't helped.
for me, adding the COMPOSE_CONVERT_WINDOWS_PATHS=1 as User Environment Variable did the trick.

@krissm I believe the .env should be a file, not a folder. Also, doesn't your second line cancel out your first before it has a chance to affect anything?

Was this page helpful?
0 / 5 - 0 ratings