vscode-remote-release icon indicating copy to clipboard operation
vscode-remote-release copied to clipboard

Docker-Compose: Support --env-file option

Open Waelder opened this issue 4 years ago • 18 comments

Hi,

want have a multi container app ( db, python, react). I want to run it locally in docker, and connect to the python as well as to the react container. The last obstacle is the automatically generated "compose project name" which is different.

Following the documentation i created a .env file to create a name for docker compose. otherwise the 2nd. open folder in container would fire up a complete new compose stack.

Unfortunately when i follow this documentation, i had to create copy of .env file in the folder wich i opened with the 2nd windows in the container. Probably because then the subfolder is then considered as project root.

Kind regards

Waelder avatar Apr 08 '21 13:04 Waelder

Adding support for --env-file would solve your request from what I see. (Updating the issue's title.)

chrmarti avatar Apr 15 '21 06:04 chrmarti

yes, sounds good, however the documentation suggest that it "just somehow magically works" but with the --env-file at least we can achieve the goal.but it would require a documenation update as well, for other newbies like me.

Waelder avatar Apr 15 '21 15:04 Waelder

I have a similar problem.

I'm using Docker Desktop, when using Docker Compose V2, I had to change the location of .env. It is possible to load .env in the container with docker-compose.yml, but it cannot load when docker-compose is ran.

So, I would like to be able to load .env at build in the devcontainer.json.

PONzu-0529 avatar Jul 15 '21 23:07 PONzu-0529

I want to load an .env file at build time without docker-compose, this seems not possible atm ?

fyyyyy avatar Aug 12 '21 17:08 fyyyyy

@fyyyyy Correct, we don't have the env-file option yet and, unlike docker-compose, the docker CLI does not read the .env file from the current folder by default.

chrmarti avatar Aug 13 '21 07:08 chrmarti

Ok, nevermind. So env-file is only supported at runtime by docker cli, not at build time.

~There are 2 options to pass env variables in the docker cli. One is via --env, the other via --env-file Passing those via --env seems to work with devContainers at BUILD time. However passing an --env-file in runArgs makes them available AFTER the build. This difference of outcomes confuses me.~

docker run -e MYVAR1 --env MYVAR2=foo --env-file ./env.list ubuntu bash

fyyyyy avatar Aug 13 '21 11:08 fyyyyy

❓ It would be nice if VScode devcontainer.json supports reading build args from a file. So we won't need this workaround

As this is off-topic ( I'm not using docker-compose), i've created a new feature request #5475

fyyyyy avatar Aug 18 '21 10:08 fyyyyy

https://github.com/orgs/devcontainers/discussions/36

bgloss avatar Mar 28 '23 14:03 bgloss

vscode executes docker compose from the folder of the first docker-compose.yml file in the list.

When you place an empty docker-compose.yml into the root folder of your projects and list it as the first docker-compose.yml to use, you get what you want.

Two nice side-effects:

  • The .env file is loaded from the root of the projects (which you cannot control otherwise)
  • The namespace that docker uses is that of the root folder and thus same for all sub-projects

my devontainer.json looks like this:

	"dockerComposeFile": [
		"../../docker-compose.yml",
		"../../environments/dev_local/postgres/docker-compose.yml",
		"./docker-compose.yml"
	],

The docker-compose.yml in the root folder is this:

version: '3.8'
# intentionally empty

What makes it a bit quirky is that the paths of the docker-compose files are relative to the project and the paths inside the docker-compose.yml files are relative to the root of the projects.

It is just a work-around. I think a proper solution is needed in devcontainer.json to mange it.

bgloss avatar Mar 28 '23 14:03 bgloss

To add to the feature request ... I think just adding a method to provide the --env-file option into the docker compose scenario is not enough.

As it is already established for the "Image and Dockerfile Scenario" a similar approach to control and externalize key settings in the "Docker Compose Scenario" would be needed.

Just for comparison: This is how a devcontainer.json looks like when using the Dockerfile scenario:

	"build": {
		"dockerfile": "Dockerfile",
		"context": "..",
		"args": { 
			"VARIANT": "3.10-bullseye",
			"NODE_VERSION": "lts/*"
		},
	},
        "runArgs": [ "--env-file=../.env" ]

similar methods to specifiy context, args and runArgs would be needed.

bgloss avatar Apr 10 '23 07:04 bgloss

So my needs align more with (https://github.com/microsoft/vscode-remote-release/issues/5324) which was closed as a duplicate and now this issues title "using --env-file" may not. Let me explain.

I would like our base .devcontainer folder to work for Codespaces and our CI/CD via GitHub Actions (using devcontainers/ci). As such, the devcontainer.json would leverage a "dockerComposeFile": ["docker-compose.yml"] and a key part of the compose service would be an Oracle DB. This DB does not work on arm64 (Apple Silicon) and there is a need to run our devcontainers locally. I would like a way for users to configure a non git committed file to remove the Oracle DB service. My hope was something like this:

  1. Commit a .devcontainer/docker-compose-local.yml file. (contents below)
  2. Git ignore the .devcontiner/.env file.
  3. Have Mac users create the .devcontiner/.env with a COMPOSE_FILE env variable (seen below)
# .devcontainer/docker-compose-local.yml
services:
  oracle:
    image: hello-world
    restart: "no"
    volumes: []
# .devcontiner/.env
COMPOSE_FILE=docker-compose.yml:docker-compose-local.yml

Would the solutions proposed here help me with that or should we open this back up?

  • https://github.com/microsoft/vscode-remote-release/issues/5324

metaskills avatar Jun 05 '23 15:06 metaskills

I'd suggest to extend the devcontainer compose spec to include a dockerComposeArgs option, which are then passed to the docker compose command:

{
    "dockerComposeFile": ["a.compose.yaml", "b.compose.yaml"],
    "dockerComposeArgs": ["--env-file custom.env"]
}

should result in docker compose -f a.compose.yaml -f b.compose.yaml --env-file custom.env

So the docker compose files will be interpolated based on custom.env.

I guess some compose args may interfere with how vscode sets up the container or just don't make sense (--dry-run for example). So allowing a subset of the args would be ideal

gislerro avatar Oct 12 '24 13:10 gislerro

Any updates on this?

TeaDrinkingProgrammer avatar Feb 25 '25 13:02 TeaDrinkingProgrammer

ANy updates?

luketych avatar Apr 03 '25 20:04 luketych

Any updates?

SKairinos avatar Apr 22 '25 12:04 SKairinos

Any updates?

thomasbival avatar Jun 02 '25 07:06 thomasbival

Please implement --env-file support for docker compose. I've just spent hours trying to make my project run in dev container that needs a different .env file for docker compose than when run outside dev container

mrmachine avatar Jun 05 '25 13:06 mrmachine

One possible workaround is to add another compose file, e.g., docker-compose.dev.yml to include the actual compose file docker-compose.yml setting the env file and use this file in devcontainer.json.

# docker-compose.dev.yml
include:
  - path: docker-compose.yml
    env_file: .env

RaphvK avatar Jun 14 '25 14:06 RaphvK

Hi, Any updates on this feature?

galshi avatar Jun 30 '25 12:06 galshi

yeah any updates on this! this would be amazing!

Xentox-Phil avatar Dec 18 '25 02:12 Xentox-Phil