Are you trying to mount a directory onto a file (or vice-versa)?
Asked Answered
P

28

163

I have a docker with version 17.06.0-ce. When I trying to install NGINX using docker with command:

docker run -p 80:80 -p 8080:8080 --name nginx -v $PWD/www:/www -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/wwwlogs -d nginx:latest

It shows that

docker: Error response from daemon: oci runtime error: container_linux.go:262: starting container process caused "process_linux.go:339: container init caused \"rootfs_linux.go:57: mounting \\"/appdata/nginx/conf/nginx.conf\\" to rootfs \\"/var/lib/docker/aufs/mnt/dcea22444e9ffda114593b18fc8b574adfada06947385aedc2ac09f199188fa0\\" at \\"/var/lib/docker/aufs/mnt/dcea22444e9ffda114593b18fc8b574adfada06947385aedc2ac09f199188fa0/etc/nginx/nginx.conf\\" caused \\"not a directory\\"\"" : Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.

If do not mount the nginx.conf file, everything is okay. So, how can I mount the configuration file?

Planchette answered 31/8, 2017 at 3:36 Comment(2)
What is the output of ls -al .? Wanna see what your pwd looks like.Silicosis
In my case I had accidentally mapped a directory from the host to a file in the container. Restarting the container didn't work anymore. I had to remove the container (docker rm …), then recreate it.Visibility
T
46

Because docker will recognize $PWD/conf/nginx.conf as a folder and not as a file. Check whether the $PWD/conf/ directory contains nginx.conf as a directory.

Test with

> cat $PWD/conf/nginx.conf 
cat: nginx.conf/: Is a directory

Otherwise, open a Docker issue.
It's working fine for me with same configuration.

Transcontinental answered 31/8, 2017 at 16:4 Comment(3)
As an intermediate-level Linux user I'm curious, what's the reason for Linux recognizing that as a folder and not a file?Lineation
Because it is actually a folder. If the file doesn't exist, docker create a folder because of the volume argument -vTranscontinental
ok, so Linux only recognizes it as a folder if docker had to create it due to the path not previously existing; but if the nginx.conf did already previously exist at that path Linux would recognize it as a file, right?Lineation
M
160

This should no longer happen (since v2.2.0.0), see here


If you are using Docker for Windows, this error can happen if you have recently changed your password.

How to fix:

  1. First make sure to delete the broken container's volume
    docker rm -v <container_name>
    Update: The steps below may work without needing to delete volumes first.
  2. Open Docker Settings
  3. Go to the "Shared Drives" tab
  4. Click on the "Reset Credentials..." link on the bottom of the window
  5. Re-Share the drives you want to use with Docker
  • You should be prompted to enter your username/password
  1. Click "Apply"
  2. Go to the "Reset" tab
  3. Click "Restart Docker"
  4. Re-create your containers/volumes

Credit goes to BaranOrnarli on GitHub for the solution.

Midi answered 22/3, 2018 at 23:17 Comment(7)
Thanks! It works for me starting from the second step and avoiding the last one.Norword
I was able to fix the problem by starting on step 2 and also omitting the last one. I did not have to destroy the containers/volumes to mount again.Gasbag
I agree with @MateoHermosilla, it dosen't need to dete the container, only "Reset Credentials"Bandeen
I'm getting the same error when trying to run proxy-deploy.sh while installing sandbox-proxy (hadoop). Following this soln. did not fix it.Tetanic
Or if you're connecting with a different local use whose password has expired. Thank you!Cheung
This was the issue for me. Password reset is every few months, so I keep forgetting to reset Shared Drive credentials in Docker.Uxmal
I got this error under Ubuntu also when I changed a file mounted in a volume which was missing before.Elizebethelizondo
C
119

TL;DR: Remove the volumes associated with the container.

Find the container name using docker ps -a then remove that container using:

docker rm -v <container_name>

Problem:

The error you are facing might occur if you previously tried running the docker run command while the file was not present at the location where it should have been in the host directory.

In this case docker daemon would have created a directory inside the container in its place, which later fails to map to the proper file when the correct files are put in the host directory and the docker command is run again.

Solution:

Remove the volumes that are associated with the container. If you are not concerned about other container volumes, you can also use:

# WARNING, THIS WILL REMOVE ALL VOLUMES
docker volume rm $(docker volume ls -q)
Carder answered 31/8, 2017 at 4:13 Comment(8)
The command in the original question only listed host volumes as being used. The docker volume command/interface is only for anonymous and named volumes, which are not part of the original question.Tremolant
@Tremolant Look at the error, it says that mount was failing when it tried to mount at /var/lib/docker/aufs/mnt/dcea22444e9ffda114593b18fc8b574adfada06947385aedc2ac09f199188fa0\\\" My deduction: It already has a folder due to a previous run, so if you try to map a file to that folder, it would fail.Carder
Here two things might have gone wrong, either the host has wrong things, or already created volume has incorrect thing. Assuming host to be correct, I thought it would be better to clear issues with existing volume.Carder
This is actually a valid answer for when the container has already been associated with a volume and the type of that volume is being changed in the next run. So removing volume might help!Sublittoral
This was helpful. The problem in my case was indeed that I had old containers still defined. Using docker rm to zap them and then doing a docker-compose up worked properly.Accordance
I found it was helpful just to run docker-compose down -v to clear away the failed volume data, rather than wipe out all volumes, but this answer is still useful for its thoroughness.Mercantile
NB if you are using docker-compose you'll need/want to create the container again after to avoid service "xxx" has no container to start #39563248Harriet
Removing all volumes is precisely what I don't want to do! I run about 8 different containers for different projects. Why would I want to delete all of them when just one of them gives me trouble?Waves
T
46

Because docker will recognize $PWD/conf/nginx.conf as a folder and not as a file. Check whether the $PWD/conf/ directory contains nginx.conf as a directory.

Test with

> cat $PWD/conf/nginx.conf 
cat: nginx.conf/: Is a directory

Otherwise, open a Docker issue.
It's working fine for me with same configuration.

Transcontinental answered 31/8, 2017 at 16:4 Comment(3)
As an intermediate-level Linux user I'm curious, what's the reason for Linux recognizing that as a folder and not a file?Lineation
Because it is actually a folder. If the file doesn't exist, docker create a folder because of the volume argument -vTranscontinental
ok, so Linux only recognizes it as a folder if docker had to create it due to the path not previously existing; but if the nginx.conf did already previously exist at that path Linux would recognize it as a file, right?Lineation
D
9

The explanation given by @Ayushya was the reason I hit this somewhat confusing error message and the necessary housekeeping can be done easily like this:

$ docker container prune
$ docker volume prune
Doubleripper answered 22/12, 2019 at 16:57 Comment(0)
A
8

I had the same problem. I was using Docker Desktop with WSL in Windows 10 17.09.

Cause of the problem:

The problem is that Docker for Windows expects you to supply your volume paths in a format that matches this:

/c/Users/username/app

BUT, WSL instead uses the format:

/mnt/c/Users/username/app

This is confusing because when checking the file in the console I saw it, and for me everything was correct. I wasn't aware of the Docker for Windows expectations about the volume paths.

Solution to the problem:

I binded the custom mount points to fix Docker for Windows and WSL differences:

sudo mount --bind /mnt/c /c

Like suggested in this amazing guide: Setting Up Docker for Windows and WSL to Work Flawlessly and everything is working perfectly now.

Before I started using WSL I was using Git Bash and I had this problem as well.

Awad answered 10/7, 2019 at 17:15 Comment(2)
More details here: github.com/10up/wp-local-docker/issues/…Curia
I found this after I solved it. My solution was to move the current folder with my host mount files from my home dir to /e/ (which is another drive, mounted as you explained in your answer) to get it to work. I appreciate your comment that you explain what the problem was, I just realized my solution worked but not why.Meerschaum
P
8

Answer for people using Docker Toolbox

There have been at least 3 answers here touching on the problem, but not explaining it properly and not giving a full solution. This is just a folder mounting problem.

Description of the problem:

Docker Toolbox bypasses the Hyper-V requirement of Docker by creating a virtual machine (in VirtualBox, which comes bundled). Docker is installed and ran inside the VM. In order for Docker to function properly, it needs to have access to the from the host machine. Which here it doesn't.

After I installed Docker Toolbox it created the VirtualBox VM and only mounted C:\Users to the machine, as \c\Users\. My project was in C:\projects so nowhere on the mounted volume. When I was sending the path to the VM, it would not exist, as C:\projects isn't mounted. Hence, the error above.

Let's say I had my project containing my ngnix config in C:/projects/project_name/

Fixing it:

  1. Go to VirtualBox, right click on Default (the VM from Docker) > Settings > Shared Folders enter image description here

  2. Clicking the small icon with the plus on the right side, Add a new share. I used the following settings:

enter image description here

  1. The above will map C:\projects to /projects (ROOT/projects) in the VM, meaning that now you can reference any path in projects like this: /projects/project_name - because project_name from C:\projects\project_name is now mounted.

To use relative paths, please consider naming the path c/projects not projects

  1. Restart everything and it should now work properly. I manually stopped the virtual machine in VirtualBox and restarted the Docker Toolbox CLI.

In my docker file, I now reference the nginx.conf like this:

volumes:
    - /projects/project_name/docker_config/nginx/nginx.conf:/etc/nginx/conf.d/default.conf

Where nginx.conf actually resides in C:\projects\project_name\docker_config\nginx\nginx.conf

Psychomotor answered 2/11, 2019 at 20:26 Comment(0)
I
8

On my Mac I had to uncheck the box "Use gRPC FUSE for file sharing" in Settings -> General

enter image description here

Inculpable answered 17/6, 2021 at 1:19 Comment(4)
Thank you this solved my issue. I also turned off use Docker Compose v2 in the Experimental Features section.Crudity
This does not work on Apple silicon macs and may cause Docker stuck at starting daemon.Tuber
This worked for me. I'm on a silicon mac.Sate
it didnt work for me.Lid
R
5

Maybe someone finds this useful. My compose file had following volume mounted

./file:/dir/file

As ./file did not exist, it was mounted into ABC (by default as folder).

In my case I had a container resulted from

docker commit ABC cool_image

When I later created ./file and ran docker-compose up , I had the error:

[...] Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.

The container brought up from cool_image remembered that /dir/file was a directory and it conflicted with lately created and mounted ./file .

The solution was:

touch ./file
docker run abc_image --name ABC -v ./file:/dir/file
# ... desired changes to ABC
docker commit ABC cool_image
Roundel answered 5/12, 2019 at 1:34 Comment(1)
Thank you, this was also my issue as I have quite a complex Docker setup!Kendrickkendricks
Y
5

Since I haven't seen this particular solve in the answers yet I'll post what worked for me.

For me (MacOS Intel), even with deleting all volumes and containers, I had to:

  • Comment out the mounted file in the docker-compose.yaml
  • Boot the container without it (docker compose up)
  • Delete the offending file, which was a directory for some reason, in the container
  • docker compose down
  • Re-add the mount in the docker-compose.yaml
  • Reboot the containers (docker compose up)

Voila, it worked.

For some reason, the offending file was still in the container, even after removing all volumes, containers, and the docker-compose.yaml mount.

Yasui answered 4/9, 2023 at 13:40 Comment(0)
B
4

I am using Docker ToolBox for Windows. By default C Drive is mounted automatically, so in order to mount the files, make sure your files and folders are inside C DRIVE.

Example: C:\Users\%USERNAME%\Desktop

Bask answered 19/1, 2019 at 10:42 Comment(6)
my mounted folder is C:\x-suite\ ; I shared my C drive ,but still have not solved my problemLeonard
are you using Docker ToolBox?Bask
minikube+virtualBox+docker ToolBox , localkube was deprecated, what driver should I use?Leonard
if you are mounting from Dockercompose , then use ${pwd}/<path>Bask
if you are mounting volume from command line ,then use -v /c/x-suite/Bask
if you are doing in Dockerfile, then use VOLUME /c/x-suiteBask
V
4

For me, it was enough to just do this:

docker compose down
docker compose up -d
Voorhees answered 19/1, 2023 at 9:44 Comment(1)
There is a dash missing between docker-composeAwildaawkward
E
2

unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type

I had a similar error on niginx in Mac environment. Docker didn't recognize the default.conf file correctly. Once changing the relative path to the absolute path, the error was fixed.

      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
Erek answered 5/9, 2019 at 9:6 Comment(0)
C
2

I'll share my case here as this may save a lot of time for someone else in the future.

I had a perfectly working docker-compose on my macos, until I start using docker-in-docker in Gitlab CI. I was only given permissions to work as Master in a repository, and the Gitlab CI is self-hosted and setup by someone else and no other info was shared, about how it's setup, etc.

The following caused the issue:

volumes:
  - ./.docker/nginx/wordpress/wordpress.conf:/etc/nginx/conf.d/default.conf

Only when I noticed that this might be running under windows (hours scratching the head), I tried renaming the wodpress.conf to default.conf and just set the dir pathnames:

volumes:
  - ./.docker/nginx/wordpress:/etc/nginx/conf.d

This solved the problem!

Columbine answered 26/9, 2019 at 18:47 Comment(1)
Not a bad decision) I had a problem that I could not copy the file to the directory named conf.d And I solved it by copying the entire contents of one directory to anotherOratorical
P
2

Note that this situation will also occur if you try to mount a volume from the host which has not been added to the Resources > File Sharing section of Docker Preferences.

enter image description here

Adding the root path as a file sharing resource will now permit Docker to access the resource to mount it to the container. Note that you may need to erase the contents on your Docker container to attempt to re-mount the volume.

For example, if your application is located at /mysites/myapp, you will want to add /mysites as the file sharing resource location.

Parlin answered 4/6, 2020 at 14:30 Comment(0)
V
2

I had the same issue, docker-compose was creating a directory instead of file, then crashing mid-way.

What I did:

  1. Run the container without any mapping.

  2. Copy the .conf file to the host location:

    docker cp containername:/etc/nginx/nginx.conf ./nginx.conf

  3. Remove the container (docker-compose down).

  4. Put the mapping back.

  5. Re-mount the container.

Docker Compose will find the .conf file and map it, instead of trying to create a directory.

Vehement answered 8/8, 2020 at 1:21 Comment(0)
F
1

Could you please use the absolute/complete path instead of $PWD/conf/nginx.conf? Then it will work.

EX:docker run --name nginx-container5 --rm  -v /home/sree/html/nginx.conf:/etc/nginx/nginx.conf -d -p 90:80 nginx
b9ead15988a93bf8593c013b6c27294d38a2a40f4ac75b1c1ee362de4723765b

root@sree-VirtualBox:/home/sree/html# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
b9ead15988a9        nginx               "nginx -g 'daemon of…"   7 seconds ago       Up 6 seconds        0.0.0.0:90->80/tcp   nginx-container5
e2b195a691a4        nginx               "/bin/bash"              16 minutes ago      Up 16 minutes       0.0.0.0:80->80/tcp   test-nginx
Fortunio answered 26/12, 2018 at 12:5 Comment(1)
if you escape it with double-quotes : docker run -d --rm -v "$PWD/nginx.conf:/etc/nginx/nginx.conf" nginx it should make no difference as the shell will translate it before passing it to docker run and actually, it doesn't make a difference, at least for meAspinwall
L
1

In Windows 10, I just get this error without changing anything in my docker-compose.yml file or Docker configuration in general.

In my case, I was using a VPN with a firewall policy that blocks port 445.

After disconnecting from the VPN the problem disappears.

So I recommend checking your firewall and not using a proxy or VPN when running Docker Desktop.

Check Docker for windows - Firewall rules for shared drives for more details.

I hope this will help someone else.

Linolinocut answered 16/1, 2020 at 10:48 Comment(0)
F
1

I experienced the same issue using Docker over WSL1 on Windows 10 with this command line:

echo $PWD
/mnt/d/nginx

docker run --name nginx -d \
  -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
nginx

I resolved it by changing the path for the file on the host system to a UNIX style absolute path:

docker run --name nginx -d \
  -v /d/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
nginx

or using an Windows style absolute path with / instead of \ as path separators:

docker run --name nginx -d \
  -v D:/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
nginx

To strip the /mnt that seems to cause problems from the path I use bash variable extension:

-v ${PWD/mnt\/}/conf/nginx.conf:/etc/nginx/nginx.conf
Farra answered 30/8, 2020 at 14:26 Comment(2)
Did you notice any performance differences when using the Windows style path vs. the Unix style path?Lineation
I can't tell. I'm just using Docker for Windows for testing/development and never monitored performance.Farra
D
0

Updating Virtual Box to 6.0.10 fixed this issue for Docker Toolbox

https://github.com/docker/toolbox/issues/844

I was experiencing this kind of error:


mlepisto@DESKTOP-VKJ76GO MINGW64 ~/G/Projects
$ touch resolv.conf

mlepisto@DESKTOP-VKJ76GO MINGW64 ~/G/Projects
$ docker run --rm -it -v $PWD/resolv.conf:/etc/resolv.conf ubuntu /bin/bash
C:\Program Files\Docker Toolbox\docker.exe: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "process_linux.go:430: container init caused \"rootfs_linux.go:58: mounting \\\"/c/Users/mlepisto/G/Projects/resolv.conf\\\" to rootfs \\\"/mnt/sda1/var/lib/docker/overlay2/61eabcfe9ed7e4a87f40bcf93c2a7d320a5f96bf241b2cf694a064b46c11db3f/merged\\\" at \\\"/mnt/sda1/var/lib/docker/overlay2/61eabcfe9ed7e4a87f40bcf93c2a7d320a5f96bf241b2cf694a064b46c11db3f/merged/etc/resolv.conf\\\" caused \\\"not a directory\\\"\"": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.

# mounting to some other file name inside the container did work just fine
mlepisto@DESKTOP-VKJ76GO MINGW64 ~/G/Projects/
$ docker run --rm -it -v $PWD/resolv.conf:/etc/resolv2.conf ubuntu /bin/bash
root@a5020b4d6cc2:/# exit
exit

After updating VitualBox all commands did work just fine 🎉

Duwe answered 21/8, 2019 at 12:33 Comment(0)
C
0

Had the same head scratch because I did not have the file locally so it created it as a folder.

mimas@Anttis-MBP:~/random/dockerize/tube$ ls
Dockerfile
mimas@Anttis-MBP:~/random/dockerize/tube$ docker run --rm -v $(pwd)/logs.txt:/usr/app/logs.txt devopsdockeruh/first_volume_exercise
docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "process_linux.go:430: container init caused \"rootfs_linux.go:58: mounting \\\"/Users/mimas/random/dockerize/tube/logs.txt\\\" to rootfs \\\"/var/lib/docker/overlay2/75891ea3688c58afb8f0fddcc977c78d0ac72334e4c88c80d7cdaa50624e688e/merged\\\" at \\\"/var/lib/docker/overlay2/75891ea3688c58afb8f0fddcc977c78d0ac72334e4c88c80d7cdaa50624e688e/merged/usr/app/logs.txt\\\" caused \\\"not a directory\\\"\"": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.
mimas@Anttis-MBP:~/random/dockerize/tube$ ls
Dockerfile  logs.txt/
Certain answered 23/8, 2019 at 20:27 Comment(0)
I
0

For me, this did not work:

volumes:
  - ./:/var/www/html
  - ./nginx.conf:/etc/nginx/conf.d/site.conf

But this, works fine (obviously moved my config file inside a new directory too:

volumes:
  - ./:/var/www/html
  - ./nginx/nginx.conf:/etc/nginx/conf.d/site.conf
Invaluable answered 12/9, 2019 at 12:27 Comment(0)
R
0

I had this problem under Windows 7 because my dockerfile was on different drive.

Here's what I did to fix the problem:

  1. Open VirtualBox Manager
  2. Select the "default" container and edit the settings.
  3. Select Shared Folders and click the icon to add a new shared folder
  4. Folder Path: x:\
  5. Folder Name: /x
  6. Check Auto-mount and Make Permanent
  7. Restart the virtual machine

At this point, docker-compose up should work.

Rowdy answered 19/3, 2020 at 19:37 Comment(0)
A
0

I got the same error on Windows10 after an update of Docker: 2.3.0.2 (45183).

... caused \\\"not a directory\\\"\"": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type

I was using absolute paths like this //C/workspace/nginx/nginx.conf and everything worked like a charm.
The update broke my docker-compose, and I had to change the paths to /C/workspace/nginx/nginx.conf with a single / for the root.

Acrodrome answered 18/5, 2020 at 7:37 Comment(0)
A
0

In my case it was a problem with Docker for Windows and use partition encrypted by Bitlocker. If you have project files on encrypted files after restart and unlock drive Dokcer doesn't see project files properly.

All you need to do is just need to restart Docker

Aggravate answered 3/12, 2020 at 15:56 Comment(0)
S
0

CleanWebpackPlugin can be the problem. In my case, in my Docker file I copy a file like this:

COPY --chown=node:node dist/app.js /usr/app/app.js

and then during development I mount that file via docker-compose:

 volumes:
      - ./dist/app.js:/usr/app/app.js

I would intermittently get the Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type. error or some version of it.

The problem was that the CleanWebpackPlugin was deleting the file and before webpack re-built. If Docker was trying to mount the file while it was deleted Docker would fail. It was intermittent.

Either remove CleanWebpackPlugin completely or configure its options to play nicer.

Staten answered 1/2, 2022 at 21:35 Comment(0)
J
0

I had this happen when the json file on the host had the executable permission set. I don't know the reason behind this.

Jabez answered 21/10, 2022 at 15:38 Comment(0)
A
0

The issue is caused by having the target already existing on the image as file. The options could be:

  1. remove the target file from the image (e.g. by creating a new image FROM the original and then running a CMD to delete the target file.)
  2. Add the whole /etc/nginx directory as volume (instead of a single file). You need to copy all other config files from under /etc/nginx to outside the container, which differ depending of your image flavour (e.g. Debian, Redhat/Centos or Alpine based structure).
  3. or as I did, I used the long Docker Config -syntax instead of a volume:

docker-compose.yml

version: "3.5"
services:
  proxy:
    image: my-image-already-containing-nginx.conf
    volumes:
      #- ./etc/nginx/nginx.conf:/etc/nginx/nginx.conf
    configs:
      - source: nginx-conf
        target: /etc/nginx/nginx.conf
configs:
  nginx:
    name: nginx-conf
    file: ./etc/nginx/nginx.conf
Awildaawkward answered 18/7, 2023 at 8:10 Comment(0)
R
-2

l have solved the mount problem. I am using a Win 7 environment, and the same problem happened to me.

Are you trying to mount a directory onto a file?

The container has a default sync directory at C:\Users\, so I moved my project to C:\Users\, then recreated the project. Now it works.

Riess answered 16/4, 2019 at 5:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.