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

Error: "unexpected end of JSON input" from CreateNetwork() #845

Open
KIVagant opened this issue Oct 23, 2020 · 9 comments
Open

Error: "unexpected end of JSON input" from CreateNetwork() #845

KIVagant opened this issue Oct 23, 2020 · 9 comments

Comments

@KIVagant
Copy link

KIVagant commented Oct 23, 2020

Hello. I'm getting the error from network.CreateNetwork():

unexpected end of JSON input

I feel like it is produced by the line:
https://github.com/fsouza/go-dockerclient/blob/main/network.go#L181

       if err := json.NewDecoder(resp.Body).Decode(&cnr); err != nil {
		return nil, err
	}

I see how the network is being created in Docker, but I cannot get the proper response back from the function.

It would be nice to have a debug mode to check the actual response.

Package Version:

github.com/fsouza/go-dockerclient v0.0.0-20170929173952-199e3d903f17

Docker version:

Docker Desktop Community, OSX, 2.3.0.5 (48029) stable
Engine: 19.03.12

Update: I found what caused this specific issue (or more how to fix it):

echo '{}' > ~/.dockercfg

Pretty funny that an empty ~/.dockercfg prevents the network from being created. Anyway it would be nice to get a message printed to stderr (at least in "debug" mode).

@fsouza
Copy link
Owner

fsouza commented Oct 30, 2020

@KIVagant can you share a reproducer? Are you sure the error is on the CresteNetwork call and not on the client instantiation? 🤔

@fsouza
Copy link
Owner

fsouza commented Oct 30, 2020

About adding debug information, generally a library shouldn't touch stdout and stderr, what we need is a better error message with more context x)

@kivagant-ba
Copy link

kivagant-ba commented Oct 30, 2020

can you share a reproducer?

In my case the code is called by another tool, but the error appeared when I had an empty ~/.dockercfg. But you are right that it is not about this specific issue but about how to understand what's wrong when an issue appears. My workmate guessed what could cause this and saved a lot of time for me. Some debug info in any form would be a life saver.

@fsouza
Copy link
Owner

fsouza commented Oct 30, 2020

Gotcha. Yeah, CreateNetwork never tries to read .dockercfg, as that file contains auth configuration that's used to authenticate with the daemon or docker registries, something not required in the CreateNetwork call.

For example, this succeeds:

% :>~/.dockercfg
% cat cnetwork.go
package main

import (
        "fmt"

        docker "github.com/fsouza/go-dockerclient"
)

func main() {
        client, err := docker.NewClientFromEnv()
        if err != nil {
                panic(err)
        }
        network, err := client.CreateNetwork(docker.CreateNetworkOptions{
                Name: "somenetwork",
        })
        if err != nil {
                panic(err)
        }
        fmt.Printf("%#v", network)
}
% go run ./cnetwork.go
&docker.Network{Name:"somenetwork", ID:"2fdd4a546e6f3a501b7b7569e69bd75073c1e88399ccb1dc41ac00ce8e5bb879", Scope:"", Driver:"", IPAM:docker.IPAMOptions{Driver:"", Config:[]docker.IPAMConfig(nil), Options:map[string]string(nil)}, Containers:map[string]docker.Endpoint(nil), Options:map[string]string(nil), Internal:false, EnableIPv6:false, Labels:map[string]string(nil)}                                                                                                           
% docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
bf4523465f64        bridge              bridge              local
21e74eb3d047        host                host                local
38813c67793b        none                null                local
2fdd4a546e6f        somenetwork         bridge              local

But this would fail:

package main

import (
	"fmt"

	docker "github.com/fsouza/go-dockerclient"
)

func main() {
	authConfigs, err := docker.NewAuthConfigurationsFromDockerCfg()
	if err != nil {
		panic(err)
	}
	fmt.Println(authConfigs)
}

@kivagant-ba
Copy link

I saw the error coming from CreateNetwork, so I don't know what to say about the examples.

@fsouza
Copy link
Owner

fsouza commented Oct 30, 2020

How do you know the error was coming from CreateNetwork? Can you share the part of the code that calls CreateNetwork and the client creation?

@kivagant-ba
Copy link

I was able to configure a debugger the code and now I can confirm your assumption.

This is a "stack trace" that produces the error:

# somewhere in the tool code:
   authConfigs, err := docker.NewAuthConfigurationsFromDockerCfg()

# /Users/yevhen.hlotov/go/pkg/mod/github.com/fsouza/[email protected]/auth.go:82
# (NewAuthConfigurationsFromDockerCfg)
// It tries to open ~/.docker/config.json
        for _, path := range pathsToTry {
		auths, err = NewAuthConfigurationsFromFile(path) // HERE (1st try)
		if err == nil {
			return auths, nil
		}
	}

# go/pkg/mod/github.com/fsouza/[email protected]/auth.go:127
		userpass := strings.SplitN(string(data), ":", 2)
		if len(userpass) != 2 {
                       // First error appears here, but it does not caused any issues:
			return nil, ErrCannotParseDockercfg
		}

# go/pkg/mod/github.com/fsouza/[email protected]/auth.go:56 (NewAuthConfigurationsFromFile)

	return NewAuthConfigurations(r)

# go/pkg/mod/github.com/fsouza/[email protected]/auth.go:83

for _, path := range pathsToTry {
		auths, err = NewAuthConfigurationsFromFile(path)
		if err == nil { // THE CHECK cannot be passed and it goes to another try in the loop and opens the empty ~/.dockercfg
			return auths, nil
		}
	}

# go/pkg/mod/github.com/fsouza/[email protected]/auth.go:120 (parseDockerConfig)
// And finally it dies here
        if err := json.Unmarshal(byteData, &confs); err != nil {
		return nil, err
	}

@kivagant-ba
Copy link

This is the new latest version of the code:

https://github.com/fsouza/go-dockerclient/blob/main/auth.go#L198

var confs map[string]dockerConfig
	if err := json.Unmarshal(byteData, &confs); err != nil {
		return nil, err
	}

I guess a "if not empty" check could fix this.

@fsouza
Copy link
Owner

fsouza commented Oct 30, 2020

Yeah, I know the origin of the error, this is what I shared above. It's just not in the path of CreateNetwork, so I was wondering if something was off 🤔

Can definitely improve the error message.

@fsouza fsouza added the nostale label Nov 1, 2020
@fsouza fsouza added the bug label Feb 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants