docker-py icon indicating copy to clipboard operation
docker-py copied to clipboard

Container create/run/attach hangs if container exits immediately

Open andreycizov opened this issue 7 years ago • 5 comments

Docker-py version: 3.4.1 Python version: 3.6.6, 3.6.0

Please see "this line is never reached at all". This container is correctly identified as "exited" by the command-line tools. If I subsequently delete the container the test case will still hang forever.

>> docker version
Server:
 Engine:
  Version:	17.12.1-ce
  API version:	1.35 (minimum version 1.12)
  Go version:	go1.10.1
  Git commit:	7390fc6
  Built:	Wed Feb 28 17:46:05 2018
  OS/Arch:	linux/amd64
  Experimental:	false

Failing test case:

import json
import subprocess
import unittest
from contextlib import closing
from time import sleep

from docker import DockerClient
from docker.types import ContainerConfig


def run_shell(*cmd):
    popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)

    try:
        return json.loads(popen.stdout.read())[0]
    finally:
        popen.stdout.close()


class TestDockerError(unittest.TestCase):
    def test_create_attach_clean(self):
        try:
            with closing(DockerClient('unix:///var/run/docker.sock')) as cli:
                c = cli.containers.create(
                    image='alpine:3.5',
                    command=['sh', '-c', 'echo asd'],
                    name='test_simple'
                )
                c.start()

                print(c.status)  # created
                print(run_shell('docker', 'inspect', 'test_simple')['State']['Status'])  # running
                sleep(5)

                print(c.status)  # created
                print(run_shell('docker', 'inspect', 'test_simple')['State']['Status'])  # exited

                for log in c.attach(stream=True, logs=True):
                    print(log)  # b'asd\n'

                # todo this line is never reached at all

        finally:
            try:
                with closing(DockerClient('unix:///var/run/docker.sock')) as cli:
                    cli.api.remove_container('test_simple')
            except:
                pass

    def test_create_attach_bare(self):
        try:
            with closing(DockerClient('unix:///var/run/docker.sock')) as cli:
                c = cli.api.create_container_from_config(
                    config=ContainerConfig(
                        detach=True,
                        version=cli.api.api_version,
                        labels={'test': '1'},
                        image='alpine:3.5',
                        command=['sh', '-c', 'echo asd']
                    ), name='test_simple'
                )

                cli.api.start(c['Id'])

                print(cli.api.inspect_container(c['Id'])['State']['Status'])  # running
                print(run_shell('docker', 'inspect', 'test_simple')['State']['Status'])  # running
                sleep(5)
                print(run_shell('docker', 'inspect', 'test_simple')['State']['Status'])  # exited

                for log in cli.api.attach(c['Id'], stream=True, logs=True):
                    print(log)  # b'asd\n'

                # todo this line is never reached at all
        finally:
            try:
                with closing(DockerClient('unix:///var/run/docker.sock')) as cli:
                    cli.api.remove_container('test_simple')
            except:
                pass

andreycizov avatar Jul 18 '18 22:07 andreycizov

I had updated my docker to the latest version and this still hangs for the same reason.

Server:
 Engine:
  Version:          18.06.0-ce
  API version:      1.38 (minimum version 1.12)
  Go version:       go1.10.3
  Git commit:       0ffa825
  Built:            Wed Jul 18 19:07:56 2018
  OS/Arch:          linux/amd64
  Experimental:     false

andreycizov avatar Jul 19 '18 09:07 andreycizov

FWIW, I am experiencing the same problem, via the DockerOperator in Airflow:

Client: Docker Engine - Community
 Version:           19.03.8
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        afacb8b7f0
 Built:             Wed Mar 11 01:25:46 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.8
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.17
  Git commit:       afacb8b7f0
  Built:            Wed Mar 11 01:24:19 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

mattinbits avatar Aug 18 '20 13:08 mattinbits

I am seeing similar behavior with this repo installed from source 8590eaa.

Client:
 Cloud integration: v1.0.20
 Version:           20.10.10
 API version:       1.41
 Go version:        go1.16.9
 Git commit:        b485636
 Built:             Mon Oct 25 07:43:15 2021
 OS/Arch:           darwin/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.10
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.9
  Git commit:       e2f740d
  Built:            Mon Oct 25 07:41:30 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.11
  GitCommit:        5b46e404f6b9f661a205e28d59c982d3634148f8
 runc:
  Version:          1.0.2
  GitCommit:        v1.0.2-0-g52b36a2
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

CarterFendley avatar Nov 27 '22 22:11 CarterFendley

My temporary work around is to add a sleep x in my docker commands, but for obvious reasons this is not ideal or precise.

CarterFendley avatar Nov 27 '22 22:11 CarterFendley