Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring environment variables prior to creation of dev container

I am trying to integrate the Remote - Containers with my docker-compose.yaml.

However, my compose file is making use of the variable substitution feature.

services:
    command: yarn server
    environment:
        MONGO_URI: mongodb://database:27017/todo
    ports:
        - ${SERVER_PORT}:3000
        - ${SERVER_DEBUG_PORT}:9320

Typically I set these variables from a Makefile that wraps the docker-compose commands I run to start my development environment.

How can I set these environment variables before the extension creates the Dev Container via docker-compose?

I've attempted to use the initializeCommand in the following ways.

devcontainer.json

"initializeCommand": ". ./env.sh"
"initializeCommand": "source env.sh"

I also attempted to use remoteEnv in the devcontainer.json.

I also attempted to set settings.terminal.integrated.env.linux.

like image 737
Joshua Barnett Avatar asked Dec 08 '25 17:12

Joshua Barnett


2 Answers

It doesn't seem possible to pass env created in the initializeCommand as env variable to the docker build command. My solution is to create an env file in the initializeCommand and pass it with the build context.

Here is the .devcontainer/devcontainer.json

{
  "build": {
    "dockerfile": "Dockerfile",
    "args": {
      "USERNAME": "${localEnv:USER}"
    }
  },
  "initializeCommand": "echo -e \"USERNAME=$USER\nUSER_UID=$(id -u $USER)\nGROUPNAME=$(id -gn $USER)\nGROUP_GID=$(id -g $USER)\" > .devcontainer/.env"
}

And in the Dockerfile

FROM xxx

ARG USERNAME=user
USER root

# Copy the .env file to the container
COPY .env /tmp/.env
# Source to use it
RUN source /tmp/.env && rm /tmp/.env && \
    if getent passwd $USERNAME; then userdel -f $USERNAME; fi && \
    if getent group $GROUPNAME; then groupdel $GROUPNAME; fi && \
    if getent group $GROUP_GID; then TMP_NAME=$(getent group $GROUP_GID | cut -d: -f1); groupdel $TMP_NAME; fi; \
    groupadd --gid $GROUP_GID $GROUPNAME && \
    useradd --uid $USER_UID --gid $GROUP_GID -m $USERNAME && \
    echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME && \
    chmod 0440 /etc/sudoers.d/$USERNAME && \
    chown $(eval echo ~$USERNAME) && \
    chmod 755 $(eval echo ~$USERNAME)

USER $USERNAME
like image 195
Tom Yu Avatar answered Dec 12 '25 01:12

Tom Yu


Browsing for an alternative to our current solution right now and stumbled across this question. It'll highlight as an error, but I promise it works if you put it at the end of your devcontainer.json:

  "runArgs": ["--env-file", "${localWorkspaceFolder}/.devcontainer/.env"]
}

I'll be back if I find something better/official

Edit, it's an alternative, but maybe not suitable for all scenarios:

include:
  - path:
    - some-path/some-compose.yaml
    - some-other-path/some-other-compose.yaml
    project_directory: ./.devcontainer
    env_file: ./.devcontainer/.env

Using this approach I've been able to interpolate env vars into multiple docker-files thanks to the Include mechanism: https://docs.docker.com/reference/compose-file/include/

like image 24
dot-notdot Avatar answered Dec 12 '25 00:12

dot-notdot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!