How to run application from docker container from host OS? - docker

I'm using docker container with ubuntu:14.04 and some C++ application that I compiled inside docker container.
Is it possible to run application that is inside container from host OS(in my case Win 7)?
Something like:
docker run <path-to-binary>/mybinary -f 10 -o output.txt
UPDATE:
Yes, it's possible
docker run -it <my-image> <path-to-binary>/mybinary
So ideally I want application inside docker will be just like native applications on Windows host OS.
Also is it possible to specify files and folder in host OS as input arguments to application that docker container can't see?
UPDATE:
I tried to mount shared folder at container start
docker run -v C:\shared_with_VM:/temp my_image
and also
docker run -v "C:\shared_with_VM":/temp my_image
But I get error:
C:\Program Files\Docker Toolbox\docker.exe: Error response from daemon: Invalid
bind mount spec "C:\\shared_with_VM:/temp": invalid mode: /temp.
See 'C:\Program Files\Docker Toolbox\docker.exe run --help'.
As said here right path format on Windows should be
docker run -v /c/shared_with_VM:/temp my_image

I am not sure if I correctly understand your question...
You can mount folders from your host to the container to make it accessible from within your container:
docker run -v /host/folder:/container/ -it <image> <executable> <arguments>
For example:
docker run -v /tmp:/tmphost -it ubuntu ls -al /tmphost
# or in Windows
docker run -v //c/Users/mrgloom/Desktop/data:/tmphost -it ubuntu ls -al /tmphost
This creates the folder /container/ in your container and links it with /host/folder. You can then bidirectonally read / write files inside these folders. Your binary has to point to the input file, which might be located in /container/input.txt

Related

How to run a python file which is present outside the docker container

Inside VM I created a docker container, Now there are some python files present outside the container(present in host directory of VM) how can I execute these python files from container anyone help me with this
You should mount the vm directory like this:
docker run -d -it --name your-container-name -v /host/path:/usr/local/bin container:image
Also you must be sure /host/path permissions are propperly set.
Volumes in docker
you can copy or mount the python file inside the docker container then execute the python with CMD.
For ex:
docker run -it -v myfile.py:/tmp/myfile.py python python /tmp/myfile.py
docker run -t -i -v <host_dir>:<container_dir> ubuntu /bin/bash
By this command I'm able to access host directory by inside the container where the directories are mounted and can able to run python file present in host by container

Windows WSL2 docker.exe volume mount differs from wsl docker volume mounts

I got Docker Desktop installed on Windows with WSL2 support. Everything works as expected. When I run my containers with a volume mount docker run -it --rm -v W:\projects:/projects busybox i can access all my windows files inside this folder.
Sadly the performance isn't that great with windows shares inside docker, so i tried to mount a path from my wsl machine.
i was under the impression that docker would run inside wsl? So I expected the two commands to output the same:
docker run -it --rm -v /home/:/myHome busybox ls -l /myHome
wsl docker run -it --rm -v /home/:/myHome busybox ls -l /myHome
but the output using docker is just total 0 where as the output using wsl is my home directory.
Can someone explain to me where this /home directory is (physically / in wsl / my computer) when I run docker from windows? And is it possible to run docker and it runs wsl docker without symlinks / path modifications so i can mount my linux directory inside the container?
If wsl-2 is installed, you can access its file system by going to the following path :-
\\wsl$
/home wouldn't just work as its not physically present in Windows's file system
You can however use /home or any other linux based directories if you login to your wsl distro. Please note that the following command won't mount any volumes if you run it from windows. It should be run only from your wsl distro
docker run --name mycontainer -v /home:myhome busybox
To access the /home directory in an Ubuntu-16.04 distro from windows:-
\\wsl$\Ubuntu-16.04\home
You can replace Ubuntu-16.04 with your distro name - version
To mount any of the directories which is under wsl, ensure that you have turned on the option "Enable integration with my default wsl distro"
https://docs.docker.com/docker-for-windows/wsl/
To mount a wsl's directory from windows as a volume, provide your host volume path in the given format
docker run --name mycontainer -v \\wsl$\Ubuntu-16.04\home:/myHome busybox
Basically, docker run -v has an effect from which environment its being executed i.e either windows or wsl
And docker volumes are present in the following path if you have enabled wsl-2 for docker but don't want to use your distro's file system
\\wsl$\docker-desktop-data\version-pack-data\community\docker\volumes\

Trying to run "comitted" Docker image, get "cannot mount volume over existing file, file exists"

I am developing a Docker image. I started with a base image and was working inside it interactively, using bash. I installed a bunch of stuff, and the install (which included compiling a lot of code) took over 20 minutes, so to save my work, I used:
$ docker commit 0f08ac958391 myproject:wip
Now when I try to run the image:
$ docker run --rm -it myproject:wip
docker: Error response from daemon: cannot mount volume over existing file, file exists /var/lib/docker/overlay2/95aa9a9ea7cc0b1ba302adbd287e4d7059ee4fbe64183404df3bc65df332ee63/merged/run/host-services/ssh-auth.sock.
What is going on? How do I fix this?
Note about related/duplicate questions: while there are other questions about this error message, none of the answers directly explain why the error happens in this situation or what to do about it. In fact, most of the questions have no answers at all.
When I ran the base image, I included a mount for the SSH agent socket:
$ docker run --rm -it -v /run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock myproject:dev /bin/bash
This bind mounts a file from the host (actually the Docker daemon VM) to a file in the Docker container. When I committed the running image, the image contained the file /run/host-services/ssh-auth.sock. The image also contained an empty volume reference to /run/host-services/ssh-auth.sock. This means that when I ran
$ docker run --rm -it myproject:wip
It was equivalent to running
$ docker run -v /run/host-services/ssh-auth.sock --rm -it myproject:wip
Unfortunately, what that command does is create an anonymous volume and mount it into the directory /run/host-services/ssh-auth.sock in the container. This works if the container has such a directory or even if it does not. What causes it to fail is if the target name is taken up by a file. Docker will not mount a volume over a file.
The solution is to explicitly provide a mapping from a host file to the target volume. Any host file will do, but in my case it is best to use the original. So this works:
docker run --rm -it -v /run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock myproject:wip

How can I make Docker Images / Volumes (Flask, Python) accessible for my host machine (macOS)?

I am running the latest macOS (Sierra) with Docker and Kitematic installed. I am also using Virtualbox for emulation.
I want to use the uwsgi-nginx-flask image but I have no idea how I can make the python files and the nginx directory inside my container accessible from outside the virtual machine ?
Haven't found anything about that on the website either.
Folders between the host and containers can be mapped and mounted by using the -v tag during runtime.
$ docker run -it -v /host/directory:/container/directory imagename:tag
You can alternatively use docker cp to copy stuff inside and outside of the container. For example
$ docker cp /path/to/file ContainerName:/path/inside/container
or
$ docker cp ContainerName:/path/inside/container/file .
you can mount the host directory to docker container which will be shared between host and docker
docker run --name container_image -d -v ~/host_dir:/container_dir docker_image

How to access a directory in hosts machine from inside a docker container?

I'm using docker version Docker version 1.9.1, build a34a1d5 on ubuntu:14.04. I have created a docker container with a volume option like this docker run -it -p 80:8080 -v host/folder:container/folder ubuntu:14.04 /bin/bash. host/folder has some files that i'm trying to access from container/folder. The files from host/folder isnt available from container/folder.
when you mount a host folder as a volume with -v you must specify absolute paths, so replace host/folder and container/folder with the absolute path.
Something like docker run -it -p 80:8080 -v /home/uday/host/folder:/container/folder ubuntu:14.04 /bin/bash (of course set the correct path to your host/folder directory)

Resources