Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Connecting to a docker container inside a server

I am very new to server side operations and I want to write an API that starts the training models on GPU server and allows the train outputs to be displayed to client.
Since more than one person is connected to the server, we work in docker containers so that the operations we do, do not cause any problems.
I want to use the code below in my future API. The code simply connect to main server with ssh. I tried to use Paramiko for that purpose.
Then I want to get into my container, activate my anaconda environment in that container and run a python script to start training.

import paramiko


host = "xxx.xx.x.xxx"       # actual host IP
username = "support"
password = "password"

client = paramiko.client.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host, username=username, password=password)
_stdin, _stdout, _stderr = client.exec_command("pwd")
_stdin.close()
print(_stdout.read().decode())              # It prints the directory in the main server --> /home/support 
print(_stderr.read().decode())
_stdin, _stdout, _stderr = client.exec_command("docker exec -it <container id> /bin/bash; pwd")
_stdin.close()
print(_stdout.read().decode())              # It still prints the directory in the main server --> /home/support. I guess it does not get into container.
print(_stderr.read().decode())              
_stdin, _stdout, _stderr = client.exec_command("docker exec -it <container id> /bin/bash; conda activate <my conda env>; python train.py")       # The real command series that I want to do.
_stdin.close()
print(_stdout.read().decode())
print(_stderr.read().decode())              # It prints the  
                                            # input device is not a TTY
                                            # bash: conda: command not found

client.close()

But it does not work like how I imagine. I could not figure out how to get into my container. Since this approach does not work, I also tried to connect the container with ssh, but it did not work either.

My question is how can i perform such the operation I described.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

When you use ; as the separator between commands, they become separate commands on the host. So in your first command, the pwd is run on the host.

Also, the -it option shouldn’t be used here, since you’re not interacting with the container from your tty.

Try these two commands

_stdin, _stdout, _stderr = client.exec_command("docker exec <container id> /bin/bash -c pwd")

and

_stdin, _stdout, _stderr = client.exec_command("docker exec <container id> /bin/bash -c 'conda activate <my conda env> && python train.py'")       # The real command series that I want to do.

I’ve changed <image id> to <container id> in the commands since what you’re interacting with is a container and not an image. It’s probably what you meant, so I don’t think that’s where your issue comes from.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading