Comments (8)
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.
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.
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.
@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.
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.
@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.
@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.
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)
- Docker::Image.create raises 404 when there is not enough disk space
- Feature request: support docker system calls.
- Warning: URI.escape is obsolete (Ruby 2.7.0) HOT 4
- warning: URI.escape is obsolete HOT 1
- undefined method `copy' on 2.0.0 HOT 2
- validate_version! doesn't exist HOT 2
- Please tag v2 releases in github
- Docker version in README
- Docker::Container.get throws `no implicit conversion of nil into String (TypeError)` Error
- The default connection type should be application/x-tar HOT 1
- The connection content_type should be modified based on the type of operation occuring
- Question: is BuildKit supported, for SSH credentials specifically? HOT 2
- Cannot use Docker API gem with Docker Desktop on Windows HOT 2
- Parameters that accept (string) Paths should also accept Pathname objects
- Ability to read a file from the container by chunks
- .dockerignore support seems broken
- class Docker::Plugin object that maps to the Plugins section of the API
- How to pass env variables when starting a container? HOT 1
- The `:host` parameter should not be set for `unix://` connections. HOT 1
- Docker::Event.stream documentation should mention how to waive the read_timeout HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from docker-api.