简体   繁体   中英

Docker volumes mounting on Windows 8 is not working

Context

I want to run a Docker Compose application on a Windows 8. I made it under a Ubuntu 16.04 and it's perfectly working on it.

This Docker Compose run:

  • nginx
  • php-fpm

The two containers use volumes.

Files

My .env file:

COMPOSE_CONVERT_WINDOWS_PATHS=1
APPLICATION_PATH=//C/Users/my_user/Documents/Development/my_application

My docker-compose.yml file:

version: '2'

services:
  web:
    build: ../application-web/
    ports:
     - "80:80"
    tty: true
    # Add a volume to link php code on the host and inside the container
    volumes:
     - ${APPLICATION_PATH}:/usr/share/nginx/html/application
     - ${APPLICATION_PATH}/docker_files/docker-assistant:/usr/share/nginx/html/assistant
    # Add hostnames to allow devs to call special url to open sites
    extra_hosts:
     - "localhost:127.0.0.1"
     - "assistant.docker:127.0.0.1"
     - "application.dev:127.0.0.1"
    depends_on:
     - custom-php
    links:
     - custom-php:custom-php

  custom-php:
    build: ../application-php/
    ports:
     - "50:50"
    volumes:
     - ${APPLICATION_PATH}:/usr/share/nginx/html/application
     - ${APPLICATION_PATH}/docker_files/docker-assistant:/usr/share/nginx/html/assistant

Problem

When I run docker-compose up , everything goes well. Containers start. But when I try to reach http://192.168.99.100 in my web browser, I got a 403 error.

My investigations show that there is no mounted volumes in the nginx and the php containers:

docker exec -it compose_web_1 bash
ls -la /usr/share/nginx/html/assistant/

shows

drwxr.xr.x 2 root root   80 May 18 15:30 .
drwxr.xr.x 2 root root 4096 May 18 16:10 ..

It seems that Docker cannot mount volumes. Why?

Other information

  • I am using the Docker Toolbox: https://www.docker.com/products/docker-toolbox
  • I know that's the good IP address because when I try to reach it in my web browser, I see my nginx container displaying logs.
  • The environment variable APPLICATION_PATH set as //C:/Users/my_user/Documents/Development/my_application cannot work because Docker use the ":" character as separator for volume declaration:

    ERROR: Volume //C:/Users/my_user/Documents/Development/my_application://C:/Users/my_user/Documents/Development/my_application has incorrect format, should be external:internal[:mode]

  • It's not a nginx problem because when I create an index.phtml file in the folder, I am able to run it:

     <?php echo 'Hello world!'; 

Ok, I finally did it!

TL;DR

Follow those instructions to be able to access C:\\ inside your containers.

1. Install the Docker Toolbox

Go get it here: https://www.docker.com/products/docker-toolbox

Install it.

2. Run a Hello world

Open a Docker Quickstart Terminal.

Docker快速启动终端已打开

Run in it:

docker run hello-world

3. Share C:\\ with Docker

Open Virtualbox

Virtualbox打开了

Open configuration of the default virtual machine and go to shared folders

配置默认虚拟机

Modify or create a new shared folder by clicking on buttons to the right. Set options to:

  • C:\\
  • C
  • Auto mount
  • Permanent configuration

Then validate.

在此输入图像描述

4. Activate sharing

Shutdown the default virtual machine then restart it.

5. Set your paths

eG if you have a .env file:

COMPOSE_CONVERT_WINDOWS_PATHS=1
APPLICATION_PATH=//C/path_from_C_to_the_folder_you_want_to_share_on_the_volume

/!\\ you need to set COMPOSE_CONVERT_WINDOWS_PATHS to 1!

6. Start your Compose

In the Docker Quickstart Terminal:

Docker快速启动终端已打开

Go to your Docker Compose folder, then start it:

cd /path_to_your_compose_folder
docker-compose up

Why have I to do that? It's so complicated!

The Docker technology rely on Linux namespaces. Without Linux, it can't work. To allow use of Docker on a Windows, Docker needs to install a Linux virtual machine. All the containers will run inside it.

Docker堆栈解释

The default virtual machine is now created and running within Virtualbox, that's why you have to share your folders using Virtualbox.

After sharing, the default virtual machine will have a mounted folder in it with a custom name (in the above example, it's C but it could be elephant or whatever).

Finally, Docker will mount volumes from the default virtual machine to the container: you have to use the name of the default machine shared folder in your volume declaration (in the above example, it's C but it could be elephant or whatever).

Docker文件夹共享说明

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