I just recently started using Docker. I'm able to run my servers, and communicate between them.
What I don't understand is: why do I need to run Docker commands, like $ docker run somerepo/image from the window opened by Docker Quickstart Terminal.
Running it from "regular" Terminal windows returns
$ docker run dockerinaction/hello_world
docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?. See 'docker run --help'.
I understand that on OS X and Windows, Docker needs to start one virtual machine with Linux 2.6 (in my case), and that all containers are run within that VM.
I thought docker CLI was connecting to a docker deamon that runs inside that VM - and then I don't understand what happens in the Quickstart Terminal.
I don't understand what does happen in the Quickstart Terminal.
You can use a regular terminal, provided by docker-machine env, and do operations similar to osx/mpkg/quickstart.app/Contents/Resources/Scripts/start.sh:
dockerm-machine start dev
eval "$(docker-machine env dev)"
(replace 'dev' with the name of your docker machine. By default, it is named... "default")
Once those environment variables for the Docker client are set, you can execute docker command directly from your shell.
A Quickstart Terminal would set those same variable for you.
You can see what it does by watching the terminal output. In my case it ran bash --login '/Applications/Docker/Docker Quickstart Terminal.app/Contents/Resources/Scripts/start.sh' attempting to start the virtual machine. Once it's running it uses the equivalent of eval "$(docker-machine env default)" to set some environment variables so your terminal is ready to access the Docker VM.
If you need more information have a look at that script start.sh.
What does docker quickstart terminal do?
From Docker docs:
It will create and start a VirtualBox VM running Docker Engine, then
configure the command-line environment so that you can talk to it
In other words it sets some environment variables so your terminal is ready to access the Docker VM. - (thanks to #Nauraushaun)
Related
I am trying to use the python in a docker container on a remote machine as the interpreter in Pycharm. Since that is a mouthful, here is a diagram:
There is a Jupyter Notebook running in the container, which I am able to connect to through my local browser (although this is just for testing the connection). The command I am using to launch the Docker container is
docker run --runtime=nvidia -it --rm --shm-size=2g -v /home/timo/storage:/storage -v /etc/passwd:/etc/passwd -v /etc/group:/etc/group --ulimit memlock=-1 -p 8888:8888 -p 7722:22 --ipc=host latest:latest
I can forward the port 8888 which the Jupyter notebook is running on with ssh -L 8888:0.0.0.0:8888 BBB.BBB.BBB.BBB and thus use it on the local machine. But I don't much like using Jupyter for developing and would like to use the Python interpreter in the Docker Container in Pycharm.
When I select "Add Python Interpreter" in Pycharm, I get the following options:
The documentation for Pycharm suggests using the "Add Python Interpreter/Docker" tool which looks like this:
However the documentation doesn't say how to set up the Docker container and the connections if the Docker is on a remote machine.
So my questions are: should I use a Unix or a TCP socket to connect to my remote docker? Or should I somehow forward all the relevant ports from the container and use the "SSH Interpreter" option? And if so, how do I set this all up? Am I setting up my Docker Container properly in the first place?
I think I have trawled through every forum and online resource, over the last two days, but have not come any closer to getting this to work. I have also tried to get this to work in Spyder, but to no avail either. So any advice is very appreciated!
Many thanks!
Thank you for depicting the dilemma so poignantly and clearly in your cartoon :-). My colleague and I were trying to do something similar and what ultimately worked beautifully was creating an SSH config directly to the Docker container jumping from the remote machine, and then setting it as a remote SSH interpreter so that pycharm doesn't even realize it's a Docker container. It also works well for vscode.
set up ssh service in docker container (subset of steps in https://dev.to/s1ntaxe770r/how-to-setup-ssh-within-a-docker-container-i5i, port22 stuff wasn't needed)
docker exec -it <container> bash: create admin interactive prompt for docker
apt-get install openssh-server
service ssh start
confirm with service ssh status -> * sshd is running
determine IP and test SSHing from remote machine into container (adapted from https://phoenixnap.com/kb/how-to-ssh-into-docker-container, steps 2 and 3)
from normal command prompt on remote machine (not within container): docker inspect -f "{{ .NetworkSettings.IPAddress }}" <container> to get container IP
test: ping -c 3 <container_ip>
ssh: ssh <container_ip>; should drop you into the container as your user; however, requires container to be configured properly (docker run cmd has -v /etc/passwd:/etc/passwd:ro \ etc.). It may ask for a password. note: if you do this for a different container later that is assigned the same IP, you will get a warning and may need to delete the previous key from known_hosts; just follow the instructions in the warning.
test SSH from local machine
if you don't have it set up already, set up passwordless ssh key-based authentication to the remote machine with the docker container
make SSH command that uses your remote machine as a jump server to the container: ssh -J <remote_machine> <container_ip>, as described in https://wiki.gentoo.org/wiki/SSH_jump_host; if successful you should drop into the container just as you did from the remote machine
save this setup in your ~/.ssh/config; follow the ProxyJump Example from https://wiki.gentoo.org/wiki/SSH_jump_host
test config with ssh <container_host_name_defined_in_ssh_config>; should also drop you into interactive container
configure pycharm (or vscode or any IDE that accepts remote SSH interpreter)
Preferences -> Project -> Python Interpreter -> Add -> SSH Interpreter -> New server configuration
host: <container_host_name_defined_in_ssh_config>
port: 22
username: <username_on_remote_server>
select interpreter, can navigate using the folder icon, which will walk you through paths within the docker, or you can enter the result of which python from the container
follow pycharm prompts
When I use docker-machine in a Windows environment (installed with docker-toolbox), every docker run command uses that docker-machine as the docker daemon.
However, when I use docker-machine in a Linux environment, which has native docker daemon installed along with docker-machine, docker run command uses native docker daemon even if there is a running docker-machine instance.
Questions are:
How does docker run command decide which daemon to use?
Are there any method to list running containers on a docker-machine instance?
For the second one, I know I can SSH to the docker-machine instance and query docker ps in it, but I want check it from outside the instance.
Thanks in advance.
The Docker Machine stack works by firing up a VM, and then setting the DOCKER_HOST environment variable to point at it. In particular, it also does the required setup to TLS-encrypt the connection and to set up a TLS client certificate to authenticate the caller. (Without this setup, a remote DOCKER_HOST is extremely dangerous.)
So: docker run and every other Docker command uses the DOCKER_HOST environment variable to decide where to run things. If DOCKER_HOST points at a Docker Machine VM, docker ps will list the containers there; you won’t usually need to docker-machine ssh (though it’s a useful tool when you really need it).
On a native Linux host it’s far easier to just directly use a local Docker daemon. If you do have both a local daemon and a docker-machine VM, you can
# switch to the Docker Machine VM
eval $(docker-machine env default)
# switch back to the host Docker
eval $(docker-machine env -u)
I am running docker commands in the docker quick start terminal. I need to run those commands in my windows terminal. When I check the version of docker, It shows correctly but when I run docker ps or other docker commands. It shows like :
C:\Users\rajasekar.sambandam>docker ps
error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.29/containers
/json: open //./pipe/docker_engine: The system cannot find the file specified.
In the default daemon configuration on Windows, the docker client must be run elevated to connect. This error may also indicate that the docker daemon is not running.
On Windows, docker runs in the background in an linux VM. The quick start terminal delegates all the calls to the VM, and thats why the docker commands work.
When running from a normal terminal, run the command:
eval $(docker-machine env default)
This command will connect the current terminal to the backgound machine.
I used docker with docker-machine ( can access container server by 192.168.99.100 ). I would like not to use docker-machine. so I can directly access my container by localhost (127.0.0.1). I shut down docker-machine (docker-machine stop) and tried to build image and container, but It said 'no daemon'. how should I completely shut down docker-machine and use local docker?
I think what you want is unset all docker-machine environment variables to use you host Docker daemon. This can be achieved with this command.
eval $(docker-machine env -u)
There are two different installs for docker on Mac. Both use a VM running Linux under the covers.
The older method includes docker toolbox and docker machine to manage the VM in virtualbox. When you use docker machine to stop this VM, the docker commands have no host to run on and will error out as you've seen.
The newer install uses xhyve to run the VM and various other tricks to make it appear seamless. This is a completely different install that you download and run from Docker, and it requires your Mac be at least version 10.10.3 with Yosemite.
See this install page for more details: https://store.docker.com/editions/community/docker-ce-desktop-mac?tab=description
I am window user and working fine with the docker on default machine. I can build images and run it perfectly. But now I have scenario where I have to run two docker-machine parallel.
I have created new docker machine from following command:
docker-machine create --driver virtualbox NAMEOFNEWMACHINE
Now when I run docker-machine ls I can see there is two docker machine running.
Then I run docker-machine ip so it gives me the IP of default machine so basically I am not able to switch from default to new dev machine on docker.
I have read docker docs & I run commands which they mentioned to switch the machine
eval "$(docker-machine env NAMEOFNEWMACHINE)"
docker-machine env NAMEOFNEWMACHINE
but after running above command it still shows me default machine ip, Therefor I cannot build my image on new machine
I am pretty new to docker so is there anyone who can help me in how to run two docker machine parallel?
Thanks
Just had the same problem on windows 7. Solved it by setting the windows DOCKER_HOST environment variable. Check your machine ip (docker-machine ls) and use the complete ip in the command:
SET DOCKER_HOST=