Running docker in subprocess on Python PyCharm - python

I have a command that looks like:
p = subprocess.Popen(['docker', 'run', 'imagename'])
in a Python program. I am able to execute this successfully from terminal, however when I run it in PyCharm I receive this error:
Cannot connect to the Docker daemon. Is the docker daemon running on this host
How can I fix this error to run in the Python IDE?

The key is in understanding what eval "$(docker-machine env dockermachinename)" returns (where dockermachinename is your docker machine name(you can check the name with "docker-machine ls" command)).
When you run docker-machine env dockermachinename which is what you need to configure your shell to connect with Docker, it prints:
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://999.999.99.999:999"
export DOCKER_CERT_PATH="/Users/enderland/.docker/machine/machines/dockermachinename"
export DOCKER_MACHINE_NAME="dockermachinename"
# Run this command to configure your shell:
# eval $(docker-machine env default)
These environment variables need to be within PyCharm. By adding them into your configuration environment variables list, you will be able to connect with Docker.
This assumes your Docker machine is running (if not, you need to do docker-machine start dockermachinename).

Related

How to activate venv- remote interpreter- VM from GCP connected via SSH

I would like to activate the venv. I'am using the remote interpreter because pycharm has got the connection via SSH with GCP VM. I used to activate env by using this command:
On Unix or MacOS, using the bash shell: source /path/to/venv/bin/activate
In local mode there is no tackle with it but for remote interpreter I do not know how to find the source. Could you please help me with this tackle?

VSCODE how to debug python in conda environment in docker container in remote server

I start by enabling the docker container in the remote server, then I connect VSCODE to that server via SSH, and finally I attach VSCODE to the docker (docker extension installed).
I have selected the interpreter in the conda environment by editing a ./.vscode/launch.json file
When I start the python program debug the packages available in the conda environment are not visible to the python program.
What am I doing wrong?
thanks
You most likely need to select the Interpreter inside VSCode.
First open up the Command Palette Ctrl+Shift+P, then type Python: Select Interpreter then select the environment you want to use.

ssh running a command gives different results to running it locally

I have a python script that uses Popen to create an appium server for a simulator on a mac
self.appium_process = subprocess.Popen(["/usr/local/bin/appium", "-a", self.ip, "--nodeconfig", self.node_file_path, "--relaxed-security", "-p", str(appium_port), "-dc", default_capabilities], stdout=log_file, stderr=subprocess.STDOUT)
I created a bash shell script that calls the python script. When I run the script from the local box it works and the appium logs show the connection.
I need to run this remote via ssh however. So I use the following to call the script:
ssh 10.18.66.99 automation_fw/config/testscript.sh
This, however, always ends up with the log showing:
env: node: No such file or directory
I checked and the node app has an extra slash before its called:
$ which node
/usr/local/bin//node
$
I tried changing the path on the machine but no change. How can I get this to run from ssh in the same way as it can run locally on that same box
A
When you are running a command vis SSH you are not starting what's called a login shell (more about that here).
From the details you've shared, I would say it's some thing in your environment (running outside a logged-in shell), more specifically a problem with your $PATH variable. You might want to check /etc/environment or similar paths (depending on your Linux flavour) for the wrong value.

Docker from python

Please be gentle, I am new to docker.
I'm trying to run a docker container from within Python but am running into some trouble due to environment variables not being set.
For example I run
import os
os.popen('docker-machine start default').read()
os.popen('eval "$(docker-machine env default)"').read()
which will start the machine but does not set the environment variables and so I can not pass a docker run command.
Ideally it would be great if I did not need to run the eval "$(docker-machine env default)". I'm not really sure why I can't set these to something static every time I start the machine.
So I am trying to set them using the bash command but Python just returns an empty string and then returns an error if I try to do docker run my_container.
Error:
Post http:///var/run/docker.sock/v1.20/containers/create: dial unix /var/run/docker.sock: no such file or directory.
* Are you trying to connect to a TLS-enabled daemon without TLS?
* Is your docker daemon up and running?
I'd suggest running these two steps to start a machine in a bash script first. Then you can have that same bash script call your python script and access docker with docker-py
import docker
import os
docker_host = os.environ['DOCKER_HOST']
client = docker.Client(docker_host)
client.create(...)
...

PyCharm add remote Python interpreter inside the Docker

So I have set up a docker on my laptop. I'm using Boot2Docker so I have one level of indirection to access the docker. In PyCharm, I can set a remote python interpreter via SSH but I'm not sure how to do it for dockers that can only be accessed via Boot2Docker?
Okay so to answer your question(s):
In PyCharm, I can set a remote python interpreter via SSH but I'm not sure how to do it for dockers that can only be accessed via Boot2Docker?
You need:
To ensure that you have SSH running in your container
There are many base images that include SSH. See: Dockerizing an SSH Daemon
Expose the SSH service to the Boot2Docker/VirtualBox VM.
docker run -d -p 2222:22 myimage ...
Setup PyCharm to connect to your Boot2Docker/VirtualBox VM.
boot2docker ip
Attaching to a running container is easy too!
$ boot2docker ssh
$ docker exec -i -t <cid> /bin/bash
Where <cid> is the Container ID or Name (if you used --name.

Categories