Giter Club home page Giter Club logo

Comments (8)

wader avatar wader commented on July 20, 2024

I've seen a issue similar to this when a very short lived container finishes executing before the attach request gets to docker. This happens if you do create, start and attach in sequence, ideally i think one should create, attach (and wait for headers etc?) and then start.

from docker-api.

ucarion avatar ucarion commented on July 20, 2024

In case anyone wants to try to reproduce this error, here's a little script that illustrates the bug:

require 'docker'

image = Docker::Image.all.find { |img| img.info['Repository'] == 'ubuntu' }

def run_cmd(image, should_log)
  container = image.run(['/bin/bash', '-c', 'echo hello world'])
  messages = container.attach(logs: should_log)

  stdout = messages[0].join
  stderr = messages[1].join
  output = stdout + stderr

  p output
end

puts "Without logs ..."
10.times do
  run_cmd(image, false)
end

puts "With logs ..."
10.times do
  run_cmd(image, true)
end

Output:

Without logs ...
"hello world\n"
""
""
"hello world\n"
""
""
""
"hello world\n"
""
"hello world\n"
With logs ...
"hello world\n"
"hello world\n"
"hello world\n"
"hello world\n"
"hello world\n"
"hello world\n"
"hello world\n"
"hello world\n"
"hello world\n"
"hello world\n"

from docker-api.

wader avatar wader commented on July 20, 2024

I think that is exactly the issue i describe. The times attach returns empty is when bash has already exited and terminated the container before you attach. Try to run ['/bin/bash', '-c', 'sleep 1; echo hello world'] to check if that is the case.

from docker-api.

ucarion avatar ucarion commented on July 20, 2024

@wader Yup, you're right. The containers I'm creating are extremely short-lived. If I run sleep 1 this issue does not occur anymore, so the lifetime of the container is the issue.

Do you have a solution to this problem? Is there a parameter I should be adding to my Docker connection to circumvent this?

from docker-api.

wader avatar wader commented on July 20, 2024

Not really, i've minimized the times it happens by running attach in thread after creating the container and then start it. To really fix the race condition i think one need to make sure the attach http request is ready and waiting before stating the container but im not sure how to that, probably have to inspect the docker source code.

from docker-api.

ucarion avatar ucarion commented on July 20, 2024

@wader I'm not sure I understand you. Are you saying that the issue is that Container#attach is being called after the container has already finished?

If this is really the issue, then what is logs: true doing?

from docker-api.

wader avatar wader commented on July 20, 2024

@ulyssecarion Yes attach without logs: true will only return output from the container that is produced after you have attached. With logs: true it will also include output from before you attached and also even if the container have already stopped.

Was there some other problem when using logs: true?

from docker-api.

tlunter avatar tlunter commented on July 20, 2024

This seems to be a timing issue that we really can't fix, right? Perhaps even the CLI has this issue unless logs: true? Going to close unless either of you thinks this is a bug.

from docker-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.