Maui_Ed
Maui_Ed

Reputation: 265

docker-compose on Windows volume not working

I've been playing with Docker for the past week and think the container idea is very useful, but despite reading everything I can for the past 3 days I can't get the volume mapping to work

get docker-compose to use my existing volume.

    Docker Version: 18.03.1-ce
    docker-compose version 1.21.1, build 7641a569

I created a volume using the following via a Dockerfile

    # Reference SQL image
    FROM microsoft/mssql-server-windows-developer

    # Create directory within SQL container for database files mapped to the    volume
    VOLUME sqldata:c:/MSSQL

and here it shows:
    C:\ProgramData\Docker\volumes>docker volume ls
    local               sqldata

Now I've tried probably 60+ different "solutions" based on StackOverflow and Docker forums, but none of them work. (Note despite the names below with Azure I am simply trying to get this to run locally, Azure is next hurdle)

    Docker-compose.yaml:

    version: '3.4'
    services:
      ws:
        image: wsManager
        container_name: azure-wcf
        ports:
           - "80"
        depends_on:
            - db

      db:
        image: dbimage:latest
        container_name: azure-db
        volumes:
             - \sqldata:/mssql
#            - type: volume
#              source: sqldata
#              target: /mssql
        ports:
            - "1433"

I've added a volumes section but it does not help, 

    volumes:
        sqldata:
            external:
                name: sqldata

    changed the - \sqldata:/mssql
    to every possible slash .. . ~ whatever.  Moved the file to yaml file 

to C:\ProgramData\Docker\volumes - basically any suggestion that showed in my search results. The dbImage is a SQL Server image that I need to persist the data from but am wondering what the magic is as nothing I've tried works. Any help is GREATLY appreciated.

I'm running on Windows 10 Pro build 1803.

Why does this have to be so hard? Than you to whomever knows how to make this actually work.

Upvotes: 12

Views: 38785

Answers (8)

Gobinda
Gobinda

Reputation: 7

  1. Launch Docker from your windows taskbar
  2. Click on Settings icon on top
  3. Click Resources
  4. Click File Sharing
  5. Click on (+) sign and add path of local folder in which you want to map the container volume.

It worked for me.

Upvotes: 0

vcycyv
vcycyv

Reputation: 620

Are you sure you really need to map to a certain host directory? If not, my solution is to create a volume beforehand and use it in docker-compose.yaml. I use the same scripts for both windows and linux. That is the beauty of docker. Here is what I did to start both postgres and mysql:

create_db.sh (you can run it in git bash or similiar environment in windows):

docker volume create --name postgres-data -d local
docker volume create --name mysql-data -d local
docker-compose up -d

docker-compose.yaml:

version: '3'

services:
  postgres:
    image: postgres:latest
    environment:
      POSTGRES_DB: datasource
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
    ports:
      - 5432:5432
    volumes:
      - postgres-data:/var/lib/postgresql/data

  mysql:
    image: mysql:latest
    environment:
      MYSQL_DATABASE: 'train'
      MYSQL_USER: 'mysql'
      MYSQL_PASSWORD: 'mysql'
      MYSQL_ROOT_PASSWORD: 'mysql'
    ports:
      - 3306:3306
    volumes:
      - mysql-data:/var/lib/mysql

volumes:
    postgres-data:
      external: true
    mysql-data:
      external: true

Upvotes: 2

aPa
aPa

Reputation: 257

For those who are using Ubunto WSL:

  • sudo mkdir /c
  • sudo mount --bind /mnt/c /c
  • navigate to your project file use new path ( /c/your-project-path and not /mnt/c/your-project-path)
  • edit your docker-compose.yml and use relative path for volume : ( like ./src instead of c/your-project-path/src)
  • docker-compose up

Upvotes: 5

zhrist
zhrist

Reputation: 1558

Docker on Windows is having strange behavior as Windows has limitations with credentials and also with the virtual machine that Docker is using(Hyper-V , VirtualBox - depending on your Docker version and setup).

Basically, you are correct to map a folder in

volumes:

section in your service:

The path is

version: '3.4'
services:
  db:
    image: dbimage:latest
    container_name: azure-db
    volumes:
         - c:/Temp/sqldata:/mssql

Important is that you do not need to explicitly create volume in volumes section, but the docker-compose up will create it(the same is for docker run).

Strange thing is that it will never show up in

docker volume ls

but it will be usable with the same files inside windows directory and inside container path /mssql

You can test it with:

docker run --rm -v c:/Temp/sqldata:/data alpine ls /data

or

docker run --rm -v c:/Temp:/data alpine ls /data

If it Disappear, probably it lost the credentials and Reset it via Docker->Settings->Shared Drives->Reset credentials.

I hope it was clear and covered all the aspects for you.

Upvotes: 0

Oleksandr Yefymov
Oleksandr Yefymov

Reputation: 6509

By default it looks that after installing Docker on Windows, sharing of drivers is disabled - so you won't be able to use volumes(that are stored on disks)

Enabling such sharing, through: Docker in tray - right click - Settings, helped to me, volumes started working fine. enter image description here

Upvotes: 1

kimreik
kimreik

Reputation: 645

If your windows account credentials has been changed, you also have to reset credentials for shared drives. (Settings > Shared Drives > Reset credentials)

In my case, the password was changed by my company security policy.

Upvotes: 3

flavio
flavio

Reputation: 178

I was struggling with a similar problem when trying to mount a volume to a specific path of my Windows machine: basically it didn't work so every time I restarted my Docker instance I lose all my DB data. I finally found out that it is because Docker for Windows by default cannot interpret Windows path so the flag COMPOSE_CONVERT_WINDOWS_PATHS has to be activated. To do so:

  • Run the command "set COMPOSE_CONVERT_WINDOWS_PATHS=1"
  • Restart Docker
  • Go to Settings > Shared Drives > Reset credentials and then select drive and then apply
  • From the command line, kill the containers (docker container rm -f )
  • Re-run the containers

Hope it helps

Upvotes: 3

Maui_Ed
Maui_Ed

Reputation: 265

The solution is to reference the true path on Windows using the volumes: option as below:

sqldb:
    image: sqlimage 
    container_name: azure-db
    volumes:
      - "C:\\ProgramData\\Docker\\volumes\\sqldata:c:\\mssql"

To persist the data I used the following:

environment:
   - "sa_password=ddsql2017@@"
   - "ACCEPT_EULA=Y"
   - 'attach_dbs= {"dbName":"MyDb","dbFiles":"C:\\MSSQL\\MyDb.mdf","C:\\MSSQL\\MyDb.ldf"]}]'

Hope this helps someone else as many of the examples I found searching both on SO and elsewhere did not work for me, and in the Docker forums there are a lot of posts saying mounting volumes not work for Windows.

Upvotes: 11

Related Questions