Skip to content

Commit

Permalink
Warn if Docker for Mac is likely to be slow
Browse files Browse the repository at this point in the history
As per [docker issue #3419](docker/compose#3419 (comment)), Docker for Mac uses "localunixsocket" instead of "localhost" to resolve sockets, which causes an order of magnitude slowdown (85s vs 5s for LoggingIntegrationTests on my laptop) unless it is explicitly redirected to 127.0.0.1 in /etc/hosts. Check for this condition, and issue a clear warning once per test run.
  • Loading branch information
alicederyn committed Feb 14, 2017
1 parent e8b8f66 commit 4ae4c66
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ protected String dockerComposePath() {

@Override
public Process execute(String... commands) throws IOException {
DockerForMacHostsIssue.issueWarning();

List<String> args = ImmutableList.<String>builder()
.add(dockerComposePath())
.addAll(projectName().constructComposeFileCommand())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2016 Palantir Technologies, Inc. All rights reserved.
*/

package com.palantir.docker.compose.execution;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;

/**
* Check whether Mac OS X users have pointed localunixsocket to localhost.
*
* <p>docker-compose takes an order of magnitude longer to run commands without this tip!
*
* @see <a href="https://github.com/docker/compose/issues/3419#issuecomment-221793401">Docker Compose Issue #3419</a>
*/
public class DockerForMacHostsIssue {

private static final String REDIRECT_LINE = "127.0.0.1 localunixsocket\n";
private static final String WARNING_MESSAGE = "\n\n **** WARNING: Your tests may be slow ****\n"
+ "Please add the following line to /etc/hosts:\n "
+ REDIRECT_LINE
+ "\nFor more information, see https://github.com/docker/compose/issues/3419#issuecomment-221793401\n\n";
private static volatile boolean checkPerformed = false;

public static void issueWarning() {
if (!checkPerformed) {
if (onMacOsX() && !localunixsocketRedirectedInEtcHosts()) {
System.err.print(WARNING_MESSAGE);
}
}
checkPerformed = true;
}

private static boolean onMacOsX() {
return System.getProperty("os.name", "generic").equals("Mac OS X");
}

private static boolean localunixsocketRedirectedInEtcHosts() {
try {
return Files.toString(new File("/etc/hosts"), Charsets.UTF_8).contains(REDIRECT_LINE);
} catch (IOException e) {
return true; // Better to be silent than issue false warnings
}
}

public static void main(String[] args) {
issueWarning();
}

private DockerForMacHostsIssue() {}
}

0 comments on commit 4ae4c66

Please sign in to comment.