Rules_docker: In Mac, curl: (7) Failed to connect to localhost port 8080: Connection refused

Created on 23 Mar 2018  路  7Comments  路  Source: bazelbuild/rules_docker

Bazel can't listen 8080 port on Docker on Mac.

Versions

Bazel

$ bazel version
Build label: 0.11.1-homebrew
Build target: bazel-out/darwin-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Sun May 31 16:57:39 +50150 (1520426998659)
Build timestamp: 1520426998659
Build timestamp as int: 1520426998659

Docker

$ docker version
Client:
 Version:       17.12.0-ce
 API version:   1.35
 Go version:    go1.9.2
 Git commit:    c97c6d6
 Built: Wed Dec 27 20:03:51 2017
 OS/Arch:       darwin/amd64

Server:
 Engine:
  Version:      17.12.0-ce
  API version:  1.35 (minimum version 1.12)
  Go version:   go1.9.2
  Git commit:   c97c6d6
  Built:        Wed Dec 27 20:12:29 2017
  OS/Arch:      linux/amd64
  Experimental: true

OS

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.13.3
BuildVersion:   17D102

Command

$ bazel run //:go_image
INFO: Analysed target //:go_image (0 packages loaded).
INFO: Found 1 target...
Target //:go_image up-to-date:
  bazel-bin/go_image-layer.tar
INFO: Elapsed time: 1.496s, Critical Path: 1.31s
INFO: Build completed successfully, 7 total actions

INFO: Running command line: bazel-bin/go_image
Loaded image ID: sha256:14573dc8ad689434fd3bf6147347d2e746c1c7e7f641f5a156838fa5b972078b
Tagging 14573dc8ad689434fd3bf6147347d2e746c1c7e7f641f5a156838fa5b972078b as bazel:go_image

Expected

$ curl 127.0.0.1:8080
{
  "Body": "Hello World!"
}

Actual

$ curl 127.0.0.1:8080
curl: (7) Failed to connect to localhost port 8080: Connection refused

Sources

.bazelrc

run --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64

main.go

package main

import (
        "fmt"
        "net/http"
)

func main() {
        http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello, world!") })
        http.ListenAndServe(":8080", nil)
}

WORKSPACE

git_repository(
    name = "io_bazel_rules_go",
    remote = "https://github.com/bazelbuild/rules_go.git",
    tag = "0.10.1",
)
load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains")
go_rules_dependencies()
go_register_toolchains()

git_repository(
    name = "io_bazel_rules_docker",
    remote = "https://github.com/bazelbuild/rules_docker.git",
    tag = "v0.4.0",
)
load(
    "@io_bazel_rules_docker//container:container.bzl",
    "container_pull",
    container_repositories = "repositories",
)
container_repositories()
load(
    "@io_bazel_rules_docker//go:image.bzl",
    _go_image_repos = "repositories",
)
_go_image_repos()

BUILD

load("@io_bazel_rules_go//go:def.bzl", "go_binary")

go_binary(
    name = "main",
    srcs = ["main.go"],
    pure = "on",
)

load("@io_bazel_rules_docker//go:image.bzl", "go_image")

go_image(
    name = "go_image",
    srcs = ["main.go"],
    pure = "on",
)

Most helpful comment

I succeeded to run docker in bridge mode on mac!

$ docker run -i --rm --network=bridge -p 8080:8080 bazel/cmd/json-rest-api:go_image
26/Mar/2018:02:33:27 +0000 200 351渭s "GET / HTTP/1.1" - "curl/7.54.0"
$ curl localhost:8080
{
  "Body": "Hello World!"
}

All 7 comments

Does this fail at HEAD of this repo? We pass --network=host to the docker CLI as configured here.

Can confirm this is also a problem for me. I've tried every possible solution that I found in the github issues threads relating to this problem but none of them works.

the build file in question:

load(
    "@io_bazel_rules_go//go:def.bzl",
    "go_binary",
    "go_library",
)

load(
    "@io_bazel_rules_docker//go:image.bzl",
    "go_image",
)

load(
    "@io_bazel_rules_docker//container:container.bzl",
    "container_image",
)

go_library(
    name = "go_default_library",
    srcs = ["main.go"],
    importpath = "github.com/gerardmrk/shipper/lib/consignment-service",
    visibility = ["//visibility:public"],
    deps = [
        "//lib/consignment-service/proto/consignment:go_default_library",
        "@org_golang_google_grpc//:go_default_library",
        "@org_golang_google_grpc//reflection:go_default_library",
        "@org_golang_x_net//context:go_default_library",
    ],
)

go_binary(
    name = "consignment-service",
    pure = "on",
    goos = "linux",
    embed = [":go_default_library"],
    visibility = ["//visibility:public"],
)

go_image(
    name = "container",
    pure = "on",
    goos = "linux",
    binary = ":consignment-service",
)

I think you are hitting this?

@mattmoor Thanks for response.
Yes, this fail at HEAD of this repo.
Yes, I'm hitting this.

@gerardmrk Thanks for response.

I succeeded to run docker in bridge mode on mac!

$ docker run -i --rm --network=bridge -p 8080:8080 bazel/cmd/json-rest-api:go_image
26/Mar/2018:02:33:27 +0000 200 351渭s "GET / HTTP/1.1" - "curl/7.54.0"
$ curl localhost:8080
{
  "Body": "Hello World!"
}

OS X doesn't support host networking (https://docs.docker.com/docker-for-mac/networking/#known-limitations-use-cases-and-workarounds).

https://github.com/bazelbuild/rules_docker/pull/312 added --network=host as an argument before $@.

docker run doesn't support overriding the initial --network argument by adding another, such as bridge. This means -p <mapping> or -P are just ignored even if using bazel run //path/to:container_image_rule -- --network bridge -P.

Was this page helpful?
0 / 5 - 0 ratings