Closed MAnfal closed 2 years ago
cc @chrmarti @bamurtaugh
Sorry, I forgot to provide an update on this. I am adding this just in case someone else faces this. The main idea here was to create a wrapper on laravel sail to create a deployable dev environment for Laravel 8. The above approach wasn't adding the environment variables to the container so ended up sharing the top level Laravel's .env
file. Docker easily picks the env from them. I am not sure how this will be solved for the containers but for now, I found a solution that works for myself.
This is what I had to do to make it happen.
Add "initializeCommand": "./.devcontainer/init $(pwd)",
in .devcontainer.json
.
#!/usr/bin/env bash
FILENAME="$1/.env"
if ! [ -f $FILENAME ]; then
touch $FILENAME
fi
# New env vars should be added here.
envVars=(
"WWWUSER=${WWWUSER:-$UID}"
"WWWGROUP=${WWWGROUP:-$(id -g)}"
"LARAVEL_SAIL=1"
)
is_addition_needed() {
for envVar in ${envVars[@]}; do
if ! grep -Fxq "$envVar" $FILENAME
then
return 1
fi
done
return 0
}
add_values() {
for envVar in ${envVars[@]}; do
if ! grep -Fxq "$envVar" $FILENAME
then
echo "$envVar" >> $FILENAME
fi
done
}
# Check if the comments should be added
is_addition_needed
should_add_warnings=$?
if [ $should_add_warnings -eq 1 ];
then
echo "" >> $FILENAME
echo "# Begin VSCode devcontainer Environment. DO NOT REMOVE" >> $FILENAME
fi
add_values
if [ $should_add_warnings -eq 1 ];
then
echo "# End VSCode devcontainer Environment. DO NOT REMOVE" >> $FILENAME
echo "" >> $FILENAME
fi
"remoteEnv"
is only passed to the server process inside the container, not to the docker-compose.yml
. Using .env
or env_file
on the service is probably the best approach at the moment. Also the devcontainer.json doesn't support inline command evaluation, so using "initializeCommand"
is the only way to run id
here.
Not sure what we would ideally do to improve this, but there are several feature requests in this area. Closing as resolved question. Thanks.
Hi @chrmarti I understand the resolution but I would suggest using the above information to reproduce it. The main purpose is to get the env in the container but the support is really not there if the container starts from docker-compose.
I tried every configuration for environment in dev container and none worked. Even passing a different env file via env_file command didn't work when I initialized the container using vscode.
I really wanted to avoid polluting the .env since it was used by laravel and I was more or less forced to use this option after a lot of trial and error. It was my last resort.
You're right: env_file
doesn't work for build args. We could add a way to set --env-file
for the docker-compose
command, although that overrides .env
, so there can be only one env file. That is a limitation of docker-compose.
It appears that docker-compose
is invoked from the root of the container which is where docker-compose
is looking for the .env
file.
My workaround is to add the following to the devcontainer.json: "initializeCommand": "cp $(pwd)/.env /"
There is a number of tickets dealing with this issue (e.g. #3061 #2323). Having the option to provide --project-directory
to docker-compose
would be ideal.
@k8n That sounds like a different problem. Could you open a new issue with steps to reproduce? Appending the config files if possible.
@chrmarti done: #4656
Hey @chrmarti, this issue might need further attention.
@MAnfal, you can help us out by closing this issue if the problem no longer exists, or adding more information.
I am getting this problem with Laravel. I am trying to set up
WWWGROUP
andWWWUSER
variables to dynamically fetch and persist in the env and theDockerfile
is unable to get them when passed viadevcontainer.json
throughdocker-compose.yml
toDockerfile
.I have tried everything
containerEnv
,remoteEnv
,build.args
but none pass the environment values down.This is how my current setup looks like.
docker-compose.yml
Dockerfile
devcontainer.json