Connection reset by peer in Linux
Description
I am trying to run commands in the docker containers by using the docker-client library in my integration tests. This approach is working fine in my local machine (MAC OS X with Docker for Mac). When I try to do the same in the CI machine (Debian Wheezy with kernel v3.16.7) most of the commands fail saying that the socket connection has been reset by peer.
Do you have any idea why this is happening?
How to reproduce
My code looks something like this (Scala):
val docker = new DefaultDockerClient(URI.create("unix:///var/run/docker.sock"))
val container = docker.listContainers().find(_.image().contains("kafka")).get
val command = Array(
"kafka-topics",
"--zookeeper", "zookeeper",
"--list"
)
val execCreate = docker.execCreate(container.id(), command,
DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr())
docker.execStart(execCreate.id).readFully().split("\n")
What do you expect
I would expect to be able to run commands in the docker containers from my Scala code in a similar way as I do with the docker exec command line.
What happened instead
A few commands are run successfully and then I start getting the error of connection reset by peer.
Software:
docker version: 1.12.1- Spotify's docker-client version: 6.0.0
Full backtrace
[info] java.lang.RuntimeException: java.io.IOException: Connection reset by peer
[info] at com.google.common.base.Throwables.propagate(Throwables.java:160)
[info] at com.spotify.docker.client.LogStream.computeNext(LogStream.java:61)
[info] at com.spotify.docker.client.LogStream.computeNext(LogStream.java:35)
[info] at com.google.common.collect.AbstractIterator.tryToComputeNext(AbstractIterator.java:143)
[info] at com.google.common.collect.AbstractIterator.hasNext(AbstractIterator.java:138)
[info] at com.spotify.docker.client.LogStream.readFully(LogStream.java:81)
[info] at my.code.KafkaDockerManager$class.listKafkaTopics(KafkaDockerManager.scala:37)
[info] ...
[info] Cause: java.io.IOException: Connection reset by peer
[info] at jnr.enxio.channels.NativeSocketChannel.read(NativeSocketChannel.java:82)
[info] at sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:59)
[info] at sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:109)
[info] at sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:103)
[info] at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:137)
[info] at org.apache.http.impl.io.SessionInputBufferImpl.read(SessionInputBufferImpl.java:198)
[info] at org.apache.http.impl.io.IdentityInputStream.read(IdentityInputStream.java:93)
[info] at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:137)
[info] at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
[info] at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
[info] ...
I've just forgot to say that the following command works fine in the same Linux machine:
docker -H unix:///var/run/docker.sock ps
I can see where the problem is happening, if not what. When you create the exec you get back a stream, and when you call readFully() you’re trying to exhaust the stream and parse log messages out of it.
In my experience doing a similar task, reading docker events off the stream, I found that A. the docker API will leave streams open and ready to send more even when there is no more to send, and B. the apache http library that docker-client uses to read the stream does not handle that situation gracefully.
I am not certain that's the same problem you’re having. I don't know why the connection would be reset during that process. I can't give you any real advice or direction.
Can you reproduce the problem reliably? Is it just with your own images, or can you run some command in busybox or whatever that you can get to trigger the same error? Which CI service are you using?
Hi @johnflavin,
Thanks for the quick response. My CI service is Jenkins. But even running the tests manually in that machine (Debian Wheezy) gives the same results.
I tried to get rid of all the readFully() and make sure that I close all the LogStreams in my code. But still the same issue. Moreover, the error happens almost immediately.
I will try to find a way to reproduce in problem in a way I can share with you.
Nevertheless, I am starting to think that the main issue is I am not using your library for the right purpose. Do you have any suggestion of how to run integration tests with Docker in an isolated and multi platform way? We are considering running the tests inside one of the containers as an alternative.
Thanks
Are you able to run your docker exec ... commands by hand on the Wheezy machine? As in, are the errors present when you’re doing any docker exec on that machine, or only when you do it through docker-client? If the former, then it seems like docker has a problem on Wheezy or you have a problem with your installation; if the latter, then there is some actual problem in docker-client that we need to fix.
I can't say whether or not you’re using this library for the "right" purpose. I don't know specifically what you’re trying to accomplish. But I can tell you that the purpose of this library is very simple: to let you communicate with your docker server from within java code (or scala, or groovy, etc.). If you wanted to, you could eschew the use of docker-client and accomplish all the exact same tasks by directly calling the docker REST API; you'd lose a ton of convenience functions, but you could also tailor things to just what you need.
I've got the error with docker-client. When trying to use docker exec it works fine:
$ docker exec -i my_kafka_1 kafka-console-producer --broker-list localhost:9092 --topic test << EOF
> > message 1
> > message 2
> > message 3
> > EOF
>
^C
$ docker -H unix:///var/run/docker.sock exec -i my_kafka_1 kafka-console-consumer --zookeeper zookeeper --from-beginning --topic test --max-messages 10
message 1
message 2
message 3
^C
$ docker -H unix:///var/run/docker.sock exec -i my_kafka_1 kafka-topics --zookeeper zookeeper --list
test
The only potential issue is that I have to terminate the commands, since both kafka-console-producer and kafka-console-consumer will never finish.
I've just noticed that another test that I have with Cassandra is working fine (I use docker-client to run some CQL statements). So definitely it has to be an issue with Kafka. I will try to investigate further and let you know if I manage to solve the issues.
For what it's worth, I'm seeing the same "java.io.IOException: Connection reset by peer" error. It seems intermittent, though. Probably happening every one out of three times.
I can reproduce it with the following client application:
import com.spotify.docker.client.DefaultDockerClient;
import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.LogStream;
import com.spotify.docker.client.exceptions.DockerException;
import com.spotify.docker.client.messages.ExecCreation;
public class DockerTest {
public static void main(String arg[]) {
try (DockerClient docker = new DefaultDockerClient("unix:///var/run/docker.sock")) {
String[] cmd = {"echo", "'All is well'"};
ExecCreation execCreation = docker.execCreate("ssprservice", cmd, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr());
final LogStream output = docker.execStart(execCreation.id());
String execOutput = output.readFully();
System.out.println("Docker command output: " + execOutput);
} catch (DockerException | InterruptedException e) {
e.printStackTrace();
}
}
}
Here's the stack trace I see after running it 3-4 times:
Exception in thread "main" java.lang.RuntimeException: java.io.IOException: Connection reset by peer
at com.google.common.base.Throwables.propagate(Throwables.java:160)
at com.spotify.docker.client.DefaultLogStream.computeNext(DefaultLogStream.java:58)
at com.spotify.docker.client.DefaultLogStream.computeNext(DefaultLogStream.java:35)
at com.google.common.collect.AbstractIterator.tryToComputeNext(AbstractIterator.java:143)
at com.google.common.collect.AbstractIterator.hasNext(AbstractIterator.java:138)
at com.spotify.docker.client.DefaultLogStream.readFully(DefaultLogStream.java:77)
at DockerTest.main(DockerTest.java:15)
Caused by: java.io.IOException: Connection reset by peer
at jnr.enxio.channels.NativeSocketChannel.read(NativeSocketChannel.java:82)
at sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:71)
at sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:121)
at sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:115)
at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:137)
at org.apache.http.impl.io.SessionInputBufferImpl.read(SessionInputBufferImpl.java:198)
at org.apache.http.impl.io.IdentityInputStream.read(IdentityInputStream.java:93)
at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:137)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:257)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:297)
at java.io.BufferedInputStream.read(BufferedInputStream.java:356)
at java.io.FilterInputStream.read(FilterInputStream.java:144)
at java.io.FilterInputStream.read(FilterInputStream.java:144)
at org.glassfish.jersey.message.internal.EntityInputStream.read(EntityInputStream.java:102)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream.read(ReaderInterceptorExecutor.java:296)
at com.google.common.io.ByteStreams.read(ByteStreams.java:733)
at com.spotify.docker.client.LogReader.nextMessage(LogReader.java:49)
at com.spotify.docker.client.DefaultLogStream.computeNext(DefaultLogStream.java:56)
Docker Version: 1.12.5, build 8eab29e Docker Client Jar Version: 7.0.0
Is there anything else you think I should try?
I'm facing a similar issue, sounds like a race condition or something. Was having the issue in an ubuntu server, and once I enabled docker daemon debug mode it stopped for the time being, but at least whenever it appears I hope to have more data to be able to trace further the issue. In my local machine with docker for mac, I was not able to reproduce the issue.
I am running Docker Version: 1.13.0, with docker client library 7.0.2.
I'm also experiencing the same issue, but it happens more than half of the times I run it.
Docker Version: 1.12.6 Docker Client Version: 7.0.0
I'm experiencing this issue very frequently as well (more often than not).
Docker Version: 1.13.0 Docker Client Version: 7.0.0
Same problem for me (version 1.12.3) I found that it work on docker prior to version 1.11.2 (included).
Like johnflavin said in his first post, it's due to the stream from docker.execStart(..) function.
Any operation on that stream triggers the error because the stream is still open waiting more datas.
Any solution to parse the logs ?
I also have the same problem but I didn't know there was an issue created about it (just found out since now I added myself as a watcher).
What I did as a workaround was to wrap the output.readFully() in a try - catch clause, as seen here.
It does not solve the issue, but maybe it helps you to move on while a solution is found.
I've taken another look at this thread, and I think I see two different but related issues:
- The issue of
execCreateblocking the thread. This one is probably not solvable right now. CallingLogStream.readFully()—or really any method onLogStream—when the stream from docker is still open but without any data will cause the thread to block. There really isn't anything we can do to change that*, because all the libraries that we use to make the connections are built on blocking IO. - The intermittent
RuntimeExceptions are confusing to me. It appears to me that the "connection reset by peer" error is from the connection being disrupted or aborted from docker's end. Until and unless someone can find a way to reliably reproduce this, I think that @diemol's solution is probably the way to go: just catch theRuntimeException.
*I did want to try one possible solution to the problem that reading from a still-open-but-no-data connection will block: check the input stream's available() method before reading. However, that didn't work like I expected. I got back a 0 which means "no data available" even when reading from the stream would not only not block but would actually return data. I filed a bug with Apache HTTP Core about it: HTTPCORE-449.
@johnflavin It seems that the LogStream class don't have access to the input stream's available()method or i am probably missing something here.
I use a cassandra container for my tests and i execute some statement into, so i want to check out the log for an error and crashed when it fails, so i don't mind having a 0 because it will mean that all cassandra statements are corrects.
I'm not sure if it would work for everyone in this thread, but I suspect that some of the reasons that people are using exec commands would be better served by having images with HEALTHCHECK commands.
As of Docker
Client:
Version: 17.03.0-ce
API version: 1.26
Go version: go1.7.5
Git commit: 60ccb22
Built: Thu Feb 23 11:02:43 2017
OS/Arch: linux/amd64
Server:
Version: 17.03.0-ce
API version: 1.26 (minimum version 1.12)
Go version: go1.7.5
Git commit: 60ccb22
Built: Thu Feb 23 11:02:43 2017
OS/Arch: linux/amd64
Experimental: false
with version com.spotify:docker-client:8.1.1 I cannot get this to work any single time. For me its finally broken.
@GameScripting What command are you trying to exec? You say it never works in docker-client 8.1.1; can you confirm that it did work at least sometimes in older versions of docker-client?
I'm digging into your comment to try to answer some questions:
- Is this actually a regression? Was there a thing that reproducibly worked before that reproducibly does not work now?
- If so, is it in... 2a. docker-client? Or 2b. docker?
@rinscy Sorry, meant to respond to this a while ago. The call to available() would be in LogReader, not LogStream. Unfortunately I don't think that approach will work. The issue I reported in HTTPCORE-449, which renders available() useless, is probably not going to be fixed soon if ever. Then if a fix did come out all the docker-client dependencies that use it would need to be updated for that, etc.
@johnflavin Yes i just notice that. I have to tell that when i use docker 1.11.2 or prior it works very well, but when i use a version above 1.11.2 (like 1.12) it fail. It has to be link to change of docker API.
I'm not sure if anyone else has tried this, but I noticed that this issue seems be revolve around using the UNIX socket that the Docker daemon sets up by default. I was wondering why I wasn't running into the same issue, so I tried to replicate it on my end. In my environment and use case, I need to communicate with local and remote Docker hosts. As such, I've configured my daemons to listen on TCP port 2375 (no TLS). Using the code snippet provided by @jalbr74, I replaced the string literal for the URL so that it uses the TCP port on the box. The result is that the provided snippet works. To double check, I used the original snippet with the UNIX socket, and received the same failures as others here.
Including my snippet:
package test;
import com.spotify.docker.client.DefaultDockerClient;
import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.LogStream;
import com.spotify.docker.client.exceptions.DockerException;
import com.spotify.docker.client.messages.ExecCreation;
import org.testng.annotations.Test;
public class TestDockerDriver {
private static final String URL = "http://localhost:2375";
private static final String[] CMD = {"/bin/bash"};
private static final String IMAGE = "ubuntu:latest";
@Test
public void testDockerClient() {
try (DockerClient docker = new DefaultDockerClient("http://localhost:2375")) {
String[] cmd = {"echo", "'All is well'"};
ExecCreation execCreation = docker.execCreate("test", cmd, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr());
final LogStream output = docker.execStart(execCreation.id());
String execOutput = output.readFully();
System.out.println("Docker command output: " + execOutput);
} catch (DockerException | InterruptedException e) {
e.printStackTrace();
}
}
}
EDIT: For purposes of clarity, I'm using the 8.1.1 JAR with Docker 1.12.6 and 1.13.1, both locally and remotely, on Fedora 25 Server, Ubuntu 16.04 Server, and openSUSE LEAP 42.2.
@ArcticPheenix Ok, it sounds like you’re able to reliably reproduce the error, and when you switch out the default /var/run/docker.sock connection for a http://localhost:2375 the error goes away. I want you to help me figure out how I can reproduce this behavior, if you are willing.
The test you have provided is a good start, but that by itself doesn't let me reproduce the behavior. We already have a test that creates a simple exec like this: DefaultDockerClientTest#testExec. Plus, your test code doesn't appear to be complete. If that test is supposed to work, it looks like you have to start a container named "test" before running the test. (Because the first argument to DockerClient#execCreate is supposed to be the ID (or name, I suppose) of a running container.)
I think what I want to do is figure out if we can reproduce this error by testing on different operating systems. You mentioned several: Fedora 25 Server, Ubuntu 16.04 Server, and openSUSE LEAP 42.2. But I can't tell exactly which were working, and whether they worked all the time or whether they failed using the socket and worked using tcp. So could you give more detailed information of all the configurations you have tried, socket vs. tcp, server OS, what works, what doesn't, etc.?
What other variables have been mentioned in the thread?
- In the original post, @gfl said they were seeing the error when using "Debian Wheezy with kernel v3.16.7".
- @gfl also said the error does not show up on Docker for Mac. I can confirm that, because that's what I'm using.
- Our Travis CI builds use Ubuntu 14.04, and they do not see this error.
- @rabelenda said they do see the error on "an Ubuntu server", but that if docker is in debug mode it goes away. That's curious. It makes me wonder if Docker for Mac defaults to being in debug mode, and maybe that's why it doesn't show this error? ...
<pause to look that up>... Yes, according to the Docker for Mac release notes, as of Beta 3 Release (2016-03-15 1.10.3-beta3), "Docker runs in debug mode by default for new installs". So that might explain why I've never seen this on my machines. However, our Travis CI builds do not run docker in debug mode. See the docker opts here: .travis.sh#L35. So something else must be going on there. - @rinscy has said that the error only happens for docker versions greater than 1.11.2.
If anyone else wants to report in, please provide the following:
- Docker server version
- Is docker running in debug mode?
- docker-client version
- Server OS, optionally with kernel version
- Are you connecting to docker using a socket or tcp? Does the error go away if you use the other one?
Now that I've typed this all out, it makes me strongly suspect that this is an issue in docker's API and not in docker-client. Still, if someone can help track it down and reproduce it, that will make it all the easier to report to docker.