The env_file field in the docker-compose.yml causes the .devcontainer to fail on creation

So here’s the most important part of the devcontainer.json

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

This came from using the Existing Docker Compose (Extend) option when creating remote container files.

This is part of my existing docker-compose.yml

env_file: .env

However, as .env files are not committed to source, attempting to create a codespaces will actually cause a failure on build, resulting in an error about no .env found

A possible solution would be to detect if this field is present somehow, and automatically have an empty temporary .env file

There’s an initializeCommand that can run before the docker build/compose. In that command, you can seed an empty .env file if needed. You can also just remove the env file config if it’s always going to be empty. Codespaces will soon support user secrets that can you inject as environment variables into your codespaces, which will accomplish the same thing as the env file with providing the container access to secrets as environment variables that are not committed to source.

Hope that helps!

Hi, thank you for your response! Where can I find the documentation about initializeCommand?

@LeptoFlare you can find the docs at devcontainer.json reference.

Here’s the description for the initializeCommand

initializeCommand string, array
A command string or list of command arguments to run on the local machine before the container is created. This runs either when the container image is being built and when the running container is created or started. The commands execute from the workspaceFolder locally. For example, "yarn install" . The array syntax ["yarn", "install"] will invoke the command (in this case yarn ) directly without using a shell.
:warning: The command is run wherever the source code is located. For Codespaces this is in the cloud.

I’ve set the initializeCommand to ["touch", ".env"] Obviously this doesn’t work because it is at the root directory. But how can I make sure to create the file in the proper directory without resorting to hard-coding /var/lib/docker/vsonlinemount/workspace/radia/.env
image

Looks like this is a bug. Sorry about that! The command is supposed to be executed from the repo root. We’ll work on a fix. In the mean time, you can hardcode it to the path you’ve referenced, but I agree that’s not a good long-term solution.

1 Like

Hardcoding it has worked, but the environment variables in the file are not being exported

I tried looking in the documentation of .devcontainer but I could not figure out how to get it to properly export

@LeptoFlare, can you share the repo you are working on (or a different public repo that simulates the same issue)? What variables are you expecting to show up? Are you writing other values in the env file after touching it? If not, since it’s empty, I wouldn’t expect it to provide any additional values.

Sorry for all the questions, just trying to understand the scenario better and see what could be going on and how to fix it.

Solution found, Basically you have to set up a debug profile, running the program using the integrated terminal will not source the .env file