简体   繁体   中英

Docker for Windows - Mount directory is coming empty

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

未从同一镜像挂载的 Docker 容器的屏幕截图

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

在此处输入图像描述

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 ?

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.

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

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM