Verified Production Fix
[docker/cli] `docker run --env-from` option, analogous to the `--volumes-from`
GH-docker/cli#6592 • Mar 07, 2026
### ROOT CAUSE
The issue arises from the absence of a `--env-from` option in Docker's `docker run` command, which prevents containers from inheriting environment variables from other containers, necessitating manual redefinition.
### CODE FIX
To implement the `--env-from` option, we'll modify the Docker CLI to accept this new flag and adjust the container runtime to inherit the environment variables from the specified container.
**Step-by-Step Explanation:**
1. **Modify the CLI Option Parsing:**
- **docker/cli/command.go**:
- Add the `--env-from` flag to the `Run` command.
- Parse the flag value to capture the source container's name or ID.
2. **Update Container Creation:**
- **docker/container.(*ContainerCreateConfig).(*CreateOpts).EnvFrom**:
- Introduce a new field to store the `--env-from` value.
- Modify the `ContainerCreate` function to include this new field when creating the container.
3. **Retrieve Environment Variables from Source Container:**
- **docker/api/client.go**:
- Add a new API call to fetch environment variables from a specified container.
- Use this API to retrieve the environment variables from the source container.
4. **Apply Environment Variables to New Container:**
- **docker/container.(*Container).Create**:
- Merge the retrieved environment variables into the new container's environment variables.
- Ensure that if an environment variable exists in both the new container and the source, the new container's value takes precedence.
5. **Handle Multiple `--env-from` Flags:**
- **docker/cli/command.go**:
- Allow multiple `--env-from` flags to inherit variables from multiple containers.
- Process each flag sequentially, merging variables and allowing later containers to override earlier ones.
6. **Test the New Feature:**
- **docker/test/*_test.go**:
- Write unit tests to verify that `--env-from` correctly inherits environment variables.
- Ensure that existing functionality remains unaffected.
**Example Usage:**
bash
docker run --env-from container_name -it my_image
This command runs `my_image` and inherits environment variables from `container_name`, avoiding the need to manually list each variable.
Deploy with DigitalOcean
Use this fix in production instantly. Claim your $200 developer credit.
Get Started →
digital