Error Mounting Host Path Volume in Docker with Colima
7/29/2024Context
We were using Docker Compose to run an application that leverages OpenSearch. To customize the configuration, we needed to overwrite the opensearch.yml
file.
Instead of hardcoding this file in the Dockerfile, we opted to mount it using Docker Compose.
We were using Colima to manage our Docker environment.
The docker-compose service:
search:
build:
dockerfile: ./search/Dockerfile
image: my-app-search
container_name: my-app-search
ports:
- '9200:9200'
- '9600:9600'
environment:
- discovery.type=single-node
- plugins.security.disabled=true # disable SSL
volumes:
- my_app_search_data:/usr/share/opensearch/data
- ./artifacts/local/opensearch.yml:/usr/share/opensearch/config/opensearch.yml
- ./artifacts/local/synonyms.txt:/usr/share/opensearch/config/analysis/synonyms.txt
Issue
During the docker-compose up process, we encountered the following error:
Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "/dev/my-app/docker/artifacts/local/opensearch.yml" to rootfs at "/usr/share/opensearch/config/opensearch.yml": mount /dev/my-app/docker/artifacts/local/opensearch.yml:/usr/share/opensearch/config/opensearch.yml (via /proc/self/fd/6), flags: 0x5000: 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
Analysis
- We verified that the file existed on the host machine.
- We checked the file permissions to ensure they were correct.
- We ran an OpenSearch container without the mounted volume, SSHed into it, and confirmed that the default opensearch.yml file was present at the target path. This indicated that the target path was correct.
- The
synonyms.txt
was mounted, but it was treated like a folder.
Solution
The root cause of the problem was that Docker was trying to find the file /dev/my-app/docker/artifacts/local/opensearch.yml
in the Colima virtual machine rather than on the physical host machine.
By inspecting the Colima configuration file (cat ~/.colima/_lima/colima/colima.yaml
), we noticed the following under the mount section:
# Colima default behaviour: $HOME and /tmp/colima are mounted as writable.
# Default: []
mounts: []
This indicated that our specified path was not included as a shared drive. To resolve this issue, we needed to either edit the colima.yaml
file to add the path to the list or run Colima with the —mount parameter to include the necessary path (/dev/my-app/docker
).
The synonyms.txt
was mounted as folder because in the Docker image there was no file to overwrite, therefore docker treat it as an empty folder.
But in the case of opensearch.yml
there was an existing file that needed to be overwritten, so Docker knew it is a file, and not a folder, But docker
didn’t find the source file. That’s why the error mentions Are you trying to mount a directory onto a file (or vice-versa)?