Docker run -v to link volume from windows host to linux container - docker

My scenario is a virtual machine running windows where I am using docker with linux containers.
My purpose is to launch a sql server container and I am having a hard time to have persistent data.
My question is how to and an explanation if posible of the command run -v to link a host folder with container.
I have read here but I am sorry I dont understand totally. Also docker documentation did not clarify at all.
My attempts failed:
docker create volume sql-data
docker run -v sql-data:C:/temp/
Error response from daemon: invalid mode: /temp/
What I read is a known error but can't find solution nor updated information about the error.
Thanks in advance.

Your problem is directly addressed in the docker docs, see here (I recommend you read the Mount volume section in full, it is fairly short). Actually, the docs specifically point out that your syntax will NOT work. To get your command to work, your destination path should be one of the following (from the same link):
a non-existing or empty directory; or a drive other than C:. Further,
the source of a bind mount must be a local directory, not a file.
Furthermore, the docs specify:
On Windows, the paths must be specified using Windows-style semantics.
Apply the above statement to your command and it should work. I'm not a windows guy but I would try:
docker run -v sql-data:c:\emptyDir

Related

Cannot start Cassandra container, getting "CommitLogReadException: Could not read commit log descriptor in file"

JVMStabilityInspector.java:196 - Exiting due to error while processing commit log during initialization.
org.apache.cassandra.db.commitlog.CommitLogReadHandler$CommitLogReadException: \
Could not read commit log descriptor in file /opt/cassandra/data/commitlog/CommitLog-7-1676434400779.log
I ran the Cassandra container in Docker, and the above error appears and stops.
It worked well before, but it doesn't seem to work well after deleting and recreating the Cassandra container.
I think we need to clear the /opt/cassandra/data/commitlog/CommitLog-7-1676434400779.log file.
However, I am not used to using dockers.
How do I erase this file?
I'm not sure if erasing the file will fix the error.
I also asked about this problem in chatgpt. However, after asking a lot of questions for an hour, they told me to try again next time, so I haven't solved it yet. So I'm going to post on Stack Overflow.
So this error likely means that the commitlog file specified is corrupted. I would definitely try deleting it.
If it's on a running docker container, you could try something like this:
Run a docker ps to get the container ID.
Remove the file using docker exec. If my container ID is f6b29860bbe5:
docker exec f6b29860bbe5 rm -rf /opt/cassandra/data/commitlog/CommitLog-7-1676434400779.log
Your question is missing a lot crucial information such as which Docker image you're running, the full Docker command you ran to start the container, and other relevant settings you've configured so I'm going to make several assumptions.
The official Cassandra Docker image (see the Quickstart Guide on the Cassandra website) that we (the Cassandra project) publish stores the commit logs in /var/lib/cassandra/commitlog/ but your deployment stores it somewhere else:
Could not read commit log descriptor in file /opt/cassandra/data/commitlog/CommitLog-7-1676434400779.log
Assuming that you're using the official image, it indicates to me that you have possibly mounted the container directories on a persistent volume on the host. If so, you will need to do a manual cleanup of all the Cassandra directories when you delete the container and recreate it.
The list of directories you need to empty include:
data/
commitlog/
saved_caches/
In your case, it might be just as easy to delete the contents of /opt/cassandra/.
If those directories are not persisted on the Docker host then you can open an interactive bash session into the Cassandra container. For example if you've named your container cassandra:
$ bash exec -it cassandra bash
For details, see the docker exec manual on the Docker Docs website. Cheers!

Save Docker container file to host filesystem

I have a Docker container that runs a Python app and generates a file as a result.
I would like to persist this file in my local file system and not to lose it at the end of the container's execution.
After reading other posts and documentation of volumes and data storage in Docker I have tried to solve it in different ways like:
Using a volume (created before):
docker run --name my_container -v my_volume:/container_file_path my_container
I'm missing something here because I understand that I'm not referencing the host's route at any time.
Or directly referencing host path and container path (with this option I also got some problems with absolute or relative paths usage):
docker run --name my_container -v host_path:container_file_path my_container
I also tried some other "variants" of the commands above (--mount instead of -v, changing target/source values, etc.) but I couldn't get it to work.
I'm using Windows Subsystem for Linux (WSL), which I've read may be the cause of the problem.
Could you guide me in what I am doing wrong? Thanks!
The second option with the bind-mount volume sounds good to me. You do need to use absolute paths, but you can use e.g. $(pwd) to make it simpler.

Access Host Folder from Docker Container without run -v command

I want to share access with my host (Ubuntu) or from an nfs server and a container or image (Ubuntu). I can't use the -v command, since the container is started by a program that only allows the container name and runs it itself. Copying is not possible since the folder is big and the content might change regulary.
The nfs-mount inside of the container does throw the error: "Protocol not supported"(done the same way as on host).
Until now it got the information that a "hardcoded" mount is not possible for images and nfs-mounts might not work with docker.
I'd be open for some "hacky" solutions as well if docker might not support it.
Bind mounts (the docker run -v option) are the only way to do this. It's considered a major design goal and security feature of Docker that containers can't generally access the host filesystem, so it'd be a major bug if there was some way to bypass this isolation.
You need to change the calling code to include the -v option, or rebuild your image to embed the data you need (if it's read-only).

How can I use a local file on container?

I'm trying create a container to run a program. I'm using a pre configurate image and now I need run the program. However, it's a machine learning program and I need a dataset from my computer to run.
The file is too large to be copied to the container. It would be best if the program running in the container searched the dataset in a local directory of my computer, but I don't know how I can do this.
Is there any way to do this reference with some docker command? Or using Dockerfile?
Yes, you can do this. What you are describing is a bind mount. See https://docs.docker.com/storage/bind-mounts/ for documentation on the subject.
For example, if I want to mount a folder from my home directory into /mnt/mydata in a container, I can do:
docker run -v /Users/andy/mydata:/mnt/mydata myimage
Now, /mnt/mydata inside the container will have access to /Users/andy/mydata on my host.
Keep in mind, if you are using Docker for Mac or Docker for Windows there are specific directories on the host that are allowed by default:
If you are using Docker Machine on Mac or Windows, your Docker Engine daemon has only limited access to your macOS or Windows filesystem. Docker Machine tries to auto-share your /Users (macOS) or C:\Users (Windows) directory. So, you can mount files or directories on macOS using.
Update July 2019:
I've updated the documentation link and naming to be correct. These type of mounts are called "bind mounts". The snippet about Docker for Mac or Windows no longer appears in the documentation but it should still apply. I'm not sure why they removed it (my Docker for Mac still has an explicit list of allowed mounting paths on the host).

Unable to use the volume flag with a windows container

I am trying to map a host folder to the guest in the same way that is easily accomplished on linux/mac via -v "$(pwd)":/code. I can't come up with a simple example to make this work with Windows Containers.
docker build -t="webdav" .
docker run --rm -it -v C:\junk:C:\code --name webdav webdav powershell
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: container f0fa313478fddb73e34d47699de0fc3c2a3bdb202ddcfc2a124c5c8b7523ec09 encountered an error during Start: failure in a Windows system call: The connection with the Virtual Machine hosting the container was closed. (0xc037010a).
I have tried countless other variations, and the accepted answer here gives the same error.
The docs seem to only refer to Docker Toolbox. The example only gives me invalid bind mount spec.
My Dockerfile
FROM microsoft/windowsservercore
RUN powershell -Command Add-WindowsFeature Web-Server
RUN powershell -Command mkdir /code
WORKDIR /code
ADD * /code/
OS Name: Microsoft Windows 10 Pro
OS Version: 10.0.14393 N/A Build 14393
Version 17.03.1-ce-win5 (10743)
Channel: stable
b18e2a5
Disclaimer: I originally posted this on the docker forums but haven't had any responses.
EDIT:
Found it. https://docs.docker.com/engine/reference/builder/#volume
"When using Windows-based containers, the destination of a volume inside the container must be one of: a non-existing or empty directory; or a drive other than C:"
Or here: https://docs.docker.com/engine/reference/commandline/run/#mount-volume--v---read-only
"The following examples will fail when using Windows-based containers, as the destination of a volume or bind-mount inside the container must be one of: a non-existing or empty directory; or a drive other than C:. Further, the source of a bind mount must be a local directory, not a file."
It strikes me that these are non-obvious places to document this difference. Where did you look for documentation of this issue? I'll add this there :)
Is there a general need for a summary of differences between Linux and Windows?
OLD ANSWER (for context)
Here's a step-by-step guide on mounting volumes with the GUI:
https://rominirani.com/docker-on-windows-mounting-host-directories-d96f3f056a2c
From reading through some other forum posts it sounds like special characters in passwords may trip things up.
If you are still having issues here is one thread you could read through:
https://github.com/docker/docker/issues/23992
Hope this helps!
I'm not sure if/where the moby repo docs publish to Docker docs, but this issue indicates that a volume cannot reference an existing folder in the container. In my example, I was first creating c:\code. If I change the command:
docker run --rm -it -v C:\junk:C:\code2 --name webdav webdav powershell
... it will create and mount c:\code2 in the container to point to c:\junk on the host.

Resources