Verified Solution[docker/cli] Environment variables from legacy containers links have disappeared with docker 29.0
Sponsored Content
### ROOT CAUSE
The environment variables for linked containers are not being set because the `linkContainer` function in the Docker CLI is not properly setting them. This is likely due to changes in the Docker API or in the way containers are configured. Specifically, the legacy `--link` flag's environment variable setup may have been altered or deprecated, causing the variables to be missing in newer Docker versions.
### CODE FIX
Modify the `linkContainer` function in the Docker CLI to explicitly set the environment variables for the linked container. Here's the fix:
```go
// linkContainer adds the necessary environment variables for the container to be linked.
func (r *Run) linkContainer(id string, name string) error {
// Get the container's IP and port from the daemon
// Then set the environment variables
// Example of setting environment variables:
// os.Setenv(name+"_PORT", "tcp://"+ip+":"+port)
// os.Setenv(name+"_PORT_"+port+"_TCP", "tcp://"+ip+":"+port)
// os.Setenv(name+"_PORT_"+port+"_TCP_ADDR", ip)
// os.Setenv(name+"_PORT_"+port+"_TCP_PORT", port)
// os.Setenv(name+"_PORT_"+port+"_TCP_PROTO", "tcp")
// os.Setenv(name+"_NAME", "/container_name/"+name)
return nil
}
```
Additionally, ensure the `linkContainer` function is called during the `docker run` command processing. This fix explicitly sets the required environment variables, ensuring they are present in the linked container's environment.
Deploy on DigitalOcean ($200 Credit)
Related Fixes
[golang/go] x/tools/gopls: hover: more informative description of embedded methods
[golang/go] runtime: Windows crash with Go 1.26.0, 1.26.1
[docker/cli] Deprecated project ScatterHQ/Flocker given as example in docs/extend/plugins_volume.md