-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
info: improve handling of empty Info #3594
Conversation
Before this change, the function could print an error in some cases, for example; ```bash docker -H tcp://127.0.0.1:2375 info --format '{{.LoggingDriver}}' template: :1:2: executing "" at <.LoggingDriver>: reflect: indirection through nil pointer to embedded struct field Info ``` With this patch applied, the error is handled gracefully, and when failing to connect with the daemon, the error is logged; ```bash docker -H tcp://127.0.0.1:2375 info --format '{{.LoggingDriver}}' Cannot connect to the Docker daemon at tcp://127.0.0.1:2375. Is the docker daemon running? docker -H tcp://127.0.0.1:2375 info --format '{{json .}}' Cannot connect to the Docker daemon at tcp://127.0.0.1:2375. Is the docker daemon running? {"ID":"","Containers":0,"..."}} ``` Note that the connection error is also included in the JSON `ServerErrors` field, so that the information does not get lost, even if STDERR would be redirected; ```bash docker -H tcp://127.0.0.1:2375 info --format '{{json .ServerErrors}}' 2> /dev/null ["Cannot connect to the Docker daemon at tcp://127.0.0.1:2375. Is the docker daemon running?"] ``` Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Codecov Report
@@ Coverage Diff @@
## master #3594 +/- ##
==========================================
- Coverage 59.47% 59.46% -0.01%
==========================================
Files 287 287
Lines 24174 24176 +2
==========================================
Hits 14377 14377
- Misses 8931 8933 +2
Partials 866 866 |
cli/command/system/info.go
Outdated
@@ -84,6 +85,7 @@ func runInfo(cmd *cobra.Command, dockerCli command.Cli, opts *infoOptions) error | |||
if dinfo, err := dockerCli.Client().Info(ctx); err == nil { | |||
info.Info = &dinfo | |||
} else { | |||
fmt.Fprintln(dockerCli.Err(), err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we return on error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this case was to allow at least the Client info to be printed (Server info would not be printed, but "failed to connect") 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah right!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gave it a quick try; here's if we don't return an error
docker -H tcp://127.0.0.1:2375 info
Cannot connect to the Docker daemon at tcp://127.0.0.1:2375. Is the docker daemon running?
Client:
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc., v0.8.2)
compose: Docker Compose (Docker Inc., v2.4.1)
sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc., 0.6.0)
scan: Docker Scan (Docker Inc., v0.17.0)
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Plugins:
Volume:
Network:
Log:
Swarm:
NodeID:
Is Manager: false
Node Address:
CPUs: 0
Total Memory: 0B
Docker Root Dir:
Debug Mode: false
Experimental: false
Live Restore Enabled: false
WARNING: No memory limit support
WARNING: No swap limit support
WARNING: No oom kill disable support
WARNING: No cpu cfs quota support
WARNING: No cpu cfs period support
WARNING: No cpu shares support
WARNING: No cpuset support
WARNING: IPv4 forwarding is disabled
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
ERROR: Cannot connect to the Docker daemon at tcp://127.0.0.1:2375. Is the docker daemon running?
errors pretty printing info
And if we return the error;
docker -H tcp://127.0.0.1:2375 info
Cannot connect to the Docker daemon at tcp://127.0.0.1:2375. Is the docker daemon running?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From some perspective "either" can make sense, although I think it's useful to still have the Client information (even if we can't connect to the daemon), which is the same behavior as we have for docker version
.
That said; to further improve;
- We should not print the
WARNING
output if we can't connect to the daemon (as they're inaccurate in that case). Those warnings are printed byprintServerWarningsLegacy()
, which is only to be used for older daemons (and should not be called if we have no daemon to connect with). - Similar to the
docker version
output, we should consider not printing theServer:
output if we can't connect, and instead only print the error for that part. - ^^ perhaps an exception to the above would be if a
--format
flag is specified (?) (e.g.--format '{{.Containers}}'
) (not sure)
Before this patch, the Server output would be printed even if we failed to connect (including WARNINGS): ```bash docker -H tcp://127.0.0.1:2375 info Cannot connect to the Docker daemon at tcp://127.0.0.1:2375. Is the docker daemon running? Client: Context: default Debug Mode: false Plugins: buildx: Docker Buildx (Docker Inc., v0.8.2) compose: Docker Compose (Docker Inc., v2.4.1) sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc., 0.6.0) scan: Docker Scan (Docker Inc., v0.17.0) Server: Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Plugins: Volume: Network: Log: Swarm: NodeID: Is Manager: false Node Address: CPUs: 0 Total Memory: 0B Docker Root Dir: Debug Mode: false Experimental: false Live Restore Enabled: false WARNING: No memory limit support WARNING: No swap limit support WARNING: No oom kill disable support WARNING: No cpu cfs quota support WARNING: No cpu cfs period support WARNING: No cpu shares support WARNING: No cpuset support WARNING: IPv4 forwarding is disabled WARNING: bridge-nf-call-iptables is disabled WARNING: bridge-nf-call-ip6tables is disabled ERROR: Cannot connect to the Docker daemon at tcp://127.0.0.1:2375. Is the docker daemon running? errors pretty printing info ``` With this patch; ```bash docker -H tcp://127.0.0.1:2375 info Client: Context: default Debug Mode: false Plugins: buildx: Docker Buildx (Docker Inc., v0.8.2) compose: Docker Compose (Docker Inc., v2.4.1) sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc., 0.6.0) scan: Docker Scan (Docker Inc., v0.17.0) Server: ERROR: Cannot connect to the Docker daemon at tcp://127.0.0.1:2375. Is the docker daemon running? errors pretty printing info ``` And if a custom format is used: ```bash docker -H tcp://127.0.0.1:2375 info --format '{{.Containers}}' Cannot connect to the Docker daemon at tcp://127.0.0.1:2375. Is the docker daemon running? 0 ``` Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@rumpl I pushed a commit that improves the "failed to connect to server" situation; it now no longer prints the Server information (and instead only shows the error if no |
docker info --format
panic #3593Before this change, the function could print an error in some cases, for example;
With this patch applied, the error is handled gracefully, and when failing to
connect with the daemon, the error is logged;
Note that the connection error is also included in the JSON
ServerErrors
field,so that the information does not get lost, even if STDERR would be redirected;
Signed-off-by: Sebastiaan van Stijn github@gone.nl
- What I did
- How I did it
- How to verify it
- Description for the changelog
- A picture of a cute animal (not mandatory but encouraged)