8000 Merge pull request #561 from tucksaun/fix/560 · symfony-cli/symfony-cli@22df288 · GitHub
[go: up one dir, main page]

Skip to content

Commit 22df288

Browse files
authored
Merge pull request #561 from tucksaun/fix/560
fix: Fix Docker containers are not detected with Docker Desktop 4.38.0
2 parents 42ca8b3 + 9c3ccd1 commit 22df288

File tree

5 files changed

+76
-4
lines changed

5 files changed

+76
-4
lines changed

.github/workflows/releaser.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
if: startsWith(github.ref, 'refs/tags/v')
4343
-
4444
name: Prepare
45-
run: go generate ./
45+
run: go generate ./...
4646
-
4747
name: Check Git status
4848
id: git

commands/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/pkg/errors"
2727
"github.com/symfony-cli/console"
28+
"github.com/symfony-cli/symfony-cli/envs"
2829
"github.com/symfony-cli/symfony-cli/local/platformsh"
2930
"github.com/symfony-cli/symfony-cli/reexec"
3031
"github.com/symfony-cli/symfony-cli/updater"
@@ -99,6 +100,8 @@ func init() {
99100
}
100101

101102
func InitAppFunc(c *console.Context) error {
103+
envs.ComputeDockerUserAgent(c.App.Name, c.App.Version)
104+
102105
psh, err := platformsh.Get()
103106
if err != nil {
104107
return err

envs/docker.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
package envs
2121

22+
//go:generate sh generate_docker_version
23+
2224
import (
2325
"bytes"
2426
"context"
@@ -44,8 +46,13 @@ import (
4446
var (
4547
dockerComposeNormalizeRegexp = regexp.MustCompile("[^-_a-z0-9]")
4648
dockerComposeNormalizeRegexpLegacy = regexp.MustCompile("[^a-z0-9]")
49+
dockerUserAgent = "Docker-Client/unknown version"
4750
)
4851

52+
func ComputeDockerUserAgent(appName, appVersion string) {
53+
dockerUserAgent = fmt.Sprintf("Docker-Client/%s %s/%s", dockerClientVersion, appName, appVersion)
54+
}
55+
4956
type sortedPorts []types.Port
5057

5158
func (ps sortedPorts) Len() int { return len(ps) }
@@ -71,7 +78,17 @@ func (l *Local) RelationshipsFromDocker() Relationships {
7178
return nil
7279
}
7380

74-
client, err := docker.NewClientWithOpts(docker.WithTimeout(2*time.Second), docker.FromEnv, dockerUseDesktopSocketIfAvailable)
81+
client, err := docker.NewClientWithOpts(
82+
docker.FromEnv,
83+
dockerUseDesktopSocketIfAvailable,
84+
docker.WithAPIVersionNegotiation(),
85+
// we use a short timeout here because we don't want to impact
86+
// negatively performance when Docker is not reachable
87+
docker.WithTimeout(2*time.Second),
88+
// defining a User Agent to avoid having the Docker API being slow
89+
// see https://github.com/docker/for-mac/issues/7575
90+
docker.WithUserAgent(dockerUserAgent),
91+
)
7592
if err != nil {
7693
if l.Debug {
7794
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
@@ -80,8 +97,6 @@ func (l *Local) RelationshipsFromDocker() Relationships {
8097
}
8198
defer client.Close()
8299

83-
client.NegotiateAPIVersion(context.Background())
84-
85100
containers, err := client.ContainerList(context.Background(), container.ListOptions{})
86101
if err != nil {
87102
if docker.IsErrConnectionFailed(err) {

envs/docker_version.go

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

envs/generate_docker_version

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env sh
2+
3+
cat <<EOF > docker_version.go
4+
// Code generated by envs/generate_docker_version
5+
// DO NOT EDIT
6+
7+
/*
8+
* Copyright (c) 2021-present Fabien Potencier <fabien@symfony.com>
9+
*
10+
* This file is part of Symfony CLI project
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*/
25+
26+
package envs
27+
28+
const dockerClientVersion = "$(go list -m all | grep github.com/docker/docker | awk -F '[ +]' '{print $2}')"
29+
EOF

0 commit comments

Comments
 (0)
0