Docker for Windows - Mount directory is coming empty

2.7k Views Asked by At

I have an image with MYSQL installed. I need to map the /var/lib/mysql directory to my host system. Following is the screenshot that I see within that directory, when I use the following command

docker run --rm -it --env-file=envProxy --network mynetwork --name my_db_dev -p 3306:3306 my_db /bin/bash

Screenshot of the Docker container without mount from the same image

Now when I try to mount a directory from my host ( Windows 10 ), by running another container from the same image, the mysql directory is blank.

docker run --rm -it --env-file=envProxy --network mynetwork -v D:/docker/data:/var/lib/mysql --name my_db_dev1 -p 3306:3306 my_db /bin/bash

Also tried this, but none works

docker run --rm -it --env-file=envProxy --network mynetwork -v D:\docker\data:/var/lib/mysql --name my_db_dev1 -p 3306:3306 my_db /bin/bash

enter image description here

One thing that I see, is that the mysql directory in the path has now root user, instead of mysql as in the previous case.

I wanted all the content from the existing container (mysql directory ) to be copied back to the host mount directory

Is that Possible ? and How can that be achieved ?

5

There are 5 best solutions below

4
grapes On

If you need to get files from container into host, better use docker cp command: https://docs.docker.com/engine/reference/commandline/cp/

It will look like: docker cp my_db_dev1:/var/lib/mysql d:\docker\data

UPD

Actually I want to persist the database files across other containers, so I wanted use volumes

In this case you have to:

  1. Start using docker-compose to orchestrate containers.

  2. In docker-compose.yml you create volume, which is shared between all containers. Something like:

docker-compose.yml

version: '3'
services:
  db1:
    image: whatever
  volumes:
    - myvol:/data

  db2:
    image: whatever2
  volumes:
    - myvol:/data

volumes:
  myvol:  

Description: https://docs.docker.com/compose/compose-file/#volume-configuration-reference

0
Michal Rakovský On

Use Windows paths writing with backslash '\' and it is recommended using variables to specify path. On the other side for Linux use slash '/' For example:

docker run -it -v %userprofile%\work\myproj\some-data:/var/data
0
Diya Li On

Same problem on Docker Desktop(2.0.0.3 (31259)). I'd got the solution from this issues.

I ensured the containers were stopped, opened docker settings, selected "Shared Drives", removed the tick on "C" and added it again. Docker asked for the Windows account credentials and I entered the new ones. After that and starting containers, mount volumes were ok. Problem solved.

It could fix the problem more simply by just reset the credentials in Docker Settings.

0
Duc Dat Nguyen On

try to turn off anti virus program or fire wall. Then click on "reset credentials" under settings/shared drives. That worked for me.

Best regards.

0
Rajitha Bhanuka On

First create a folder structure like below,

C:\Users\rajit\MYSQL_DATA\MYSQL_CONFIG

C:\Users\rajit\MYSQL_DATA\DATA_DIR

then please adjust like below,

docker pull mysql:8.0
docker run --name mysql-docker -v C:\Users\rajit\MYSQL_DATA\MYSQL_CONFIG:/etc/mysql/conf.d --env="MYSQL_ROOT_PASSWORD=root" --env="MYSQL_PASSWORD=root" --env="MYSQL_DATABASE=test_db" -v C:\Users\rajit\MYSQL_DATA\DATA_DIR:/var/lib/mysql -d -p 3306:3306 mysql:8.0 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci