0

I have a python function to read a specific docker container log.

First I try to get the container id as below

def test_1(): logging.info("Test debug logs") ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('10.18.18.10', username='usr1', password='pwd1') ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('sudo docker ps -aqf "name=container1"') container_id = ssh_stdout.read().decode("utf-8").replace('\n', '') ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(f'sudo docker logs -f {container_id} --since 4m') container_logs = ssh_stdout.read() ssh.close() 

I am getting the container_id fine. But I am not getting the container_logs. The execution keeps going when I try to retrieve the logs.

P.S. Anyway technically I can read the docker logs by container name too. But I need to know why I was not able to read the output the second time.

1 Answer 1

1

The docker command will keep waiting because it's executed to show live logs.

To solve that I need to do as below

ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(f'sudo docker logs {container_id} --since 4m') 

The '-f' need to be removed from the command so that it won't keep holding.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.