Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New behavior of r.Run binds to localhost, not 0.0.0.0, issues in Docker #2292

Closed
DarthHater opened this issue Mar 23, 2020 · 30 comments · Fixed by #2294
Closed

New behavior of r.Run binds to localhost, not 0.0.0.0, issues in Docker #2292

DarthHater opened this issue Mar 23, 2020 · 30 comments · Fixed by #2294
Labels
Milestone

Comments

@DarthHater
Copy link

DarthHater commented Mar 23, 2020

Description

Hi! In gin 1.6.0 it appears starting a default gin instance will now bind to:

localhost:8080

Instead of previously it would bind to just :8080 (0.0.0.0)

This causes issues when used inside of Docker as it is binding to Docker's localhost rather than 0.0.0.0

How to reproduce

package main

import (
	"github.com/gin-gonic/gin"
)

func main() {
	g := gin.Default()
	g.GET("/hello/:name", func(c *gin.Context) {
		c.String(200, "Hello %s", c.Param("name"))
	})
	g.Run()
}

More or less the example above should show gin starting on localhost:8080 rather than :8080 like in gin 1.5.0

Expectations

The expectation is by default, gin should bind the way it previously did

A project that is setup to show this:

https://github.com/DarthHater/hello-world/tree/TestBranch

If you clone that repo, and run docker-compose up, you should see the project start on :8080 as it is using gin 1.5.0, if you switch the go.mod to use gin 1.6.0, you'll see localhost:8080, and if you attempt to hit:

http://locahost:8080/ping

You'll see empty response, as although Docker has exposed 8080, it's not making it through to the host application since it is bound to Docker's localhost rather than the machines 0.0.0.0

Let me know if there are any questions! I was teaching some friends how to use gin, etc... and ran into this while doing so.

@appleboy
Copy link
Member

ref: #2216

@appleboy
Copy link
Member

ping @keob Any thoughts about this?

@appleboy appleboy added the bug label Mar 23, 2020
@keob
Copy link
Contributor

keob commented Mar 23, 2020

Sorry.If you use 0.0.0.0 on windows or mac os it will trigger a firewall block.

@appleboy
Copy link
Member

@keob maybe you should pass the localhost:8080 as parameter not change the default behavior in v1.5 version.

@keob
Copy link
Contributor

keob commented Mar 23, 2020

@DarthHater In the archlinux i can run it.

@keob
Copy link
Contributor

keob commented Mar 23, 2020

user r.Run()

@keob
Copy link
Contributor

keob commented Mar 23, 2020

Screenshot_20200323_140350

@appleboy
Copy link
Member

appleboy commented Mar 23, 2020

@keob In my Linux server, I can reproduce this critical issue.

package main

import "github.com/gin-gonic/gin"

func main() {
        r := gin.Default()
        r.GET("/ping", func(c *gin.Context) {
                c.JSON(200, gin.H{
                        "message": "pong",
                })
        })
        r.Run()
}

Write Dockerfile and build as below:

FROM golang:1.14-alpine as build_base
RUN apk add bash ca-certificates git gcc g++ libc-dev
WORKDIR /app
# Force the go compiler to use modules
ENV GO111MODULE=on
# We want to populate the module cache based on the go.{mod,sum} files.
COPY go.mod .
COPY go.sum .
RUN go mod download

# This image builds the weavaite server
FROM build_base AS server_builder
# Here we copy the rest of the source code
COPY . .
ENV GOOS=linux
ENV GOARCH=amd64
RUN go build -o /line-login -tags netgo -ldflags '-w -extldflags "-static"' .

### Put the binary onto base image
FROM plugins/base:linux-amd64
LABEL maintainer="Bo-Yi Wu <appleboy.tw@gmail.com>"
EXPOSE 8080
COPY --from=server_builder /app/templates /templates
COPY --from=server_builder /app/images /images
COPY --from=server_builder /line-login /line-login
CMD ["/line-login"]

and run it.

$ docker run -p 8081:8080 appleboy/line-login
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /ping                     --> main.main.func1 (3 handlers)
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on localhost:8080

Can't connect to the server.

@appleboy appleboy added this to the 1.6.1 milestone Mar 23, 2020
@keob
Copy link
Contributor

keob commented Mar 23, 2020

@appleboy Can you run it?

@appleboy
Copy link
Member

I will bump new version v1.6.1 to fix this issue today.

@appleboy
Copy link
Member

@keob Can you post steps?

@keob
Copy link
Contributor

keob commented Mar 23, 2020

DarthHater's demo i can run it,your demo i don't run it

@keob
Copy link
Contributor

keob commented Mar 23, 2020

@appleboy

@appleboy
Copy link
Member

appleboy commented Mar 23, 2020

@keob Please update gin to v1.6.0 version and try it again.

@keob
Copy link
Contributor

keob commented Mar 23, 2020

@appleboy I use 1.6.0 run the DarthHater's demo

@keob
Copy link
Contributor

keob commented Mar 23, 2020

@appleboy I use your demo and gin v1.6.0 and user docker run --network -host can run it,but my pc 8080 port use it.I change port to r.Run(":9090").

@appleboy
Copy link
Member

@keob You can't use the --network host mode.

@keob
Copy link
Contributor

keob commented Mar 23, 2020

@appleboy not use --network host is run

@keob
Copy link
Contributor

keob commented Mar 23, 2020

But i use 9090,docker proxy process running at 8080,so i change to 9090;

@keob
Copy link
Contributor

keob commented Mar 23, 2020

@aadidenko

@appleboy
Copy link
Member

Can you post docker run command and show the output log?

@keob
Copy link
Contributor

keob commented Mar 23, 2020

please wait me,I delete it

@keob
Copy link
Contributor

keob commented Mar 23, 2020

image

@appleboy
Copy link
Member

@keob You change the default source code.

package main

import "github.com/gin-gonic/gin"

func main() {
        r := gin.Default()
        r.GET("/ping", func(c *gin.Context) {
                c.JSON(200, gin.H{
                        "message": "pong",
                })
        })
        r.Run()
}

You can't pass any value in r.Run()

@keob
Copy link
Contributor

keob commented Mar 23, 2020

cant't run it. But i don't now why DarthHater's demo can run it.🤔🤔

@keob
Copy link
Contributor

keob commented Mar 23, 2020

Sorry I write a bug

@appleboy
Copy link
Member

@keob His example uses gin v1.5 version.

@keob
Copy link
Contributor

keob commented Mar 23, 2020

oh!

@appleboy
Copy link
Member

See the new release https://github.com/gin-gonic/gin/releases/tag/v1.6.1

@DarthHater
Copy link
Author

Wow, went to sleep and woke up to a lot of feedback! Thanks y'all!

FWIW and I should have likely added this to my report I am using:

Docker on OS X (the two people seeing this issue were using High Sierra specifically). We are using Alpine linux as the host OS, and it MIGHT be related to Docker and how it set's up a network (we are using docker-compose to orchestrate). The app itself is mostly a joke, but the behavior caught me off guard, because if others were using gin in Docker, they'd likely sit and scratch their head for a while until they trip across it.

My suggestion if the 0.0.0.0 tripped OS X and Windows firewalls would be to detect the host OS and switch it based on that. That's easy enough with runtime.GOOS, from my perspective.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants