Tms91
Tms91

Reputation: 4214

Cannot map a host directory with spaces to a container directory (bind mounts) - Docker Quickstart Terminal (Bash), windows 10

I am a newbie with Docker. I am following a tutorial in which I am using bind mounts and volumes, I am using windows 10 Docker quickstart terminal (bash) and Docker 19.03.1.

I am in the directory .../dockerfile-sample-2 (Note: this path contains spaces) containing:

$ ls -al
total 18
drwxr-xr-x 1 Tommaso 197121   0 mag 10 11:55 ./
drwxr-xr-x 1 Tommaso 197121   0 mag  1 19:20 ../
-rw-r--r-- 1 Tommaso 197121 410 apr 11 09:06 Dockerfile
-rw-r--r-- 1 Tommaso 197121 249 apr 11 09:06 index.html
-rw-r--r-- 1 Tommaso 197121   0 mag 10 11:55 testme.txt

Now I run and get the followings:

.../dockerfile-sample-2
$ docker container run -d --name nginx -p 80:80 -v $(pwd):/usr/share/nginx/html nginx
b8f24ee0e0b76d0b06503ce90fbd6a9e2110e40eaa4432e8c77556510c61a989

.../dockerfile-sample-2
$ docker container run -d --name nginx2 -p 8080:80 nginx
3450433e18097291936d7e62071769521eb36e92f509ad931c9e927f135df71a

Now, according to the tutorial, by accessing the IP addresses http://192.168.99.101/ and http://192.168.99.101:8080, I should be able to see, respectiverly, a custom landing page from the index.html file and the original nginx landing page.

However, I only get the original nginx landing page twice.

And more, according to my tutorial, by running

docker container exec -it nginx bash
cd /usr/share/nginx/html
ls –al

I should get the same content of the previous directory .../dockerfile-sample-2.

However, I only get

root@b8f24ee0e0b7:/usr/share/nginx/html# ls -al
total 16
drwxr-xr-x 2 root root 4096 Apr 13 19:20 .
drwxr-xr-x 3 root root 4096 Apr 13 19:20 ..
-rw-r--r-- 1 root root  494 Apr 13 15:13 50x.html
-rw-r--r-- 1 root root  612 Apr 13 15:13 index.html

and again, according to my tutorial, if I'd make a new file in my .../dockerfile-sample-2 directory, it should appear among the results of

docker container exec -it nginx bash
cd /usr/share/nginx/html
ls –al

but it does not.

What am I doing wrong?


Here you have some context:

My running containers:

$ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
3450433e1809        nginx               "/docker-entrypoint.…"   20 minutes ago      Up 20 minutes       0.0.0.0:8080->80/tcp   nginx2
b8f24ee0e0b7        nginx               "/docker-entrypoint.…"   21 minutes ago      Up 21 minutes       0.0.0.0:80->80/tcp     nginx

In my Dockerfile:

# this shows how we can extend/change an existing official image from Docker Hub
FROM nginx:latest
# highly recommend you always pin versions for anything beyond dev/learn

WORKDIR /usr/share/nginx/html
# change working directory to root of nginx webhost
# using WORKDIR is preferred to using 'RUN cd /some/path'

COPY index.html index.html

# I don't have to specify EXPOSE or CMD because they're in my FROM

In my Index.html:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">

  <title>Your 2nd Dockerfile worked!</title>

</head>

<body>
  <h1>You just successfully ran a container with a custom file copied into the image at build time!</h1>
</body>
</html>

UPDATE:

I bet the problem is related to the fact that the current working directory contains spaces.

So I removed the nginx container and tryed run other ones.

The following commands:

docker container run -d --name nginx -p 80:80 -v "$(pwd)":/usr/share/nginx/html nginx

docker container run -d --name nginx -p 80:80 -v "/d/Files Tommaso/Programmazione/Docker/udemy-docker-mastery-main/dockerfile-sample-2:/usr/share/nginx/html" nginx

docker container run -d --name nginx -p 80:80 -v "/d/Files Tommaso/Programmazione/Docker/udemy-docker-mastery-main/dockerfile-sample-2":/usr/share/nginx/html nginx

as I try to access http://192.168.99.101/, make my browser get 403 Forbidden.


while these other commands:

docker container run -d --name nginx -p 80:80 -v $(pwd):/usr/share/nginx/html nginx

docker container run -d --name nginx -p 80:80 -v /$(pwd):/usr/share/nginx/html nginx

docker container run -d --name nginx -p 80:80 -v `pwd -W`:/usr/share/nginx/html nginx

docker container run -d --name nginx -p 80:80 -v $(pwd):/usr/share/nginx/html nginx

docker container run -d --name nginx -p 80:80 -v /d/Files Tommaso/Programmazione/Docker/udemy-docker-mastery-main/dockerfile-sample-2:/usr/share/nginx/html nginx

make my terminal respond with:

C:\Applicazioni_Tommaso\Docker Toolbox\docker.exe: invalid reference format: repository name must be lowercase.
See 'C:\Applicazioni_Tommaso\Docker Toolbox\docker.exe run --help'.

I bet this means it cannot parse spaces.


and these other commands:

docker container run -d --name nginx -p 80:80 -v %cd%:/usr/share/nginx/html nginx

make my terminal respond with:

C:\Applicazioni_Tommaso\Docker Toolbox\docker.exe: Error response from daemon: create YYYY: "YYYY" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path.

Upvotes: 0

Views: 3080

Answers (3)

oscarz
oscarz

Reputation: 1254

Using --mount will solve the issue
docker container run -d --name nginx -p 80:80 --mount type=bind,source="$(pwd)",destination=/usr/share/nginx/html nginx

Upvotes: 1

sqr00t
sqr00t

Reputation: 1

I ran into pretty much the same problem (given assumptions of spacing in directory causing problems) when binding directory:volume using -v.

Context:
running an aws lambda function deployment package build task container using lambci/lambda:build-python3.8 image, utilising files in windowspath\build.

The following command worked for me, in the sense that files in windowspath\build were recognised and used for the following /bin/sh -c commands; therefore binding to /var/task worked as intended.

Note: after -v flag, double quotes only for the source directory (windows style pathing, supposedly would work for unix style as well), end double quotes before the colon, directory /var/task referred after colon is the docker run environment directory (unix style/WSL backend).

docker run -v "C:\windowspath\build":/var/task "lambci/lambda:build-python3.8" /bin/sh -c "pip install -r requirements.txt -t package/;cd package;zip -r ../deployment_package.zip .;zip -g ../deployment_package.zip ../lambda_function.py"

Upvotes: 0

Tms91
Tms91

Reputation: 4214

After several attempts I can assume that

  • on windows 10
  • using Docker Quickstart Terminal

Docker 19.03.1 cannot manage the use of <path containing spaces> inside -v parameter

docker container run -d --name <name> -p 80:80 -v <path containing spaces>:<destination path> <image>

The only way I found to solve this problem is to change the source volume directory to a directory in which no folder contains spaces, and running

docker container run -d --name nginx -p 80:80 -v $(pwd):/usr/share/nginx/html nginx

Upvotes: 0

Related Questions