Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

docker

nginx

People also ask

How do I list a docker container?

In order to list the Docker containers, we can use the “docker ps” or “docker container ls” command. This command provides a variety of ways to list and filter all containers on a particular Docker engine.

What is docker compose version?

The Compose file is a YAML file defining services, networks, and volumes for a Docker application. The latest and recommended version of the Compose file format is defined by the Compose Specification. The Compose spec merges the legacy 2.


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.


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)

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.


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


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