Movatterモバイル変換


[0]ホーム

URL:


Version 1.107 is now available! Read about the new features and fixes from November.

Dismiss this update

Environment variables

You can set environment variables in your container without altering the container image by using one of the options below.

You should verifyTerminal > Integrated: Inherit Env is checked in settings or the variables you set may not appear in the Integrated Terminal. This setting is checked by default.

Option 1: Add individual variables

Depending on what you reference indevcontainer.json:

  • Dockerfile or image: Add thecontainerEnv property todevcontainer.json to set variables that should apply to the entire container orremoteEnv to set variables for VS Code and related sub-processes (terminals, tasks, debugging, etc.):

    "containerEnv": {    "MY_CONTAINER_VAR":"some-value-here",    "MY_CONTAINER_VAR2":"${localEnv:SOME_LOCAL_VAR}"},"remoteEnv": {    "PATH":"${containerEnv:PATH}:/some/other/path",    "MY_REMOTE_VARIABLE":"some-other-value-here",    "MY_REMOTE_VARIABLE2":"${localEnv:SOME_LOCAL_VAR}"}

    As this example illustrates,containerEnv can reference local variables andremoteEnv can reference both local and existing container variables.

Video: Modify PATH in a dev container



  • Docker Compose: Since Docker Compose has built-in support for updating container-wide variables, onlyremoteEnv is supported indevcontainer.json:

    "remoteEnv": {    "PATH":"${containerEnv:PATH}:/some/other/path",    "MY_REMOTE_VARIABLE":"some-other-value-here",    "MY_REMOTE_VARIABLE2":"${localEnv:SOME_LOCAL_VAR}"}

    As this example illustrates,remoteEnv can reference both local and existing container variables.

    To update variables that apply to the entire container, update (orextend) yourdocker-compose.yml with the following for the appropriate service:

    version:'3'services:  your-service-name-here:    environment:      -YOUR_ENV_VAR_NAME=your-value-goes-here      -ANOTHER_VAR=another-value     # ...

If you've already built the container and connected to it, runDev Containers: Rebuild Container from the Command Palette (F1) to pick up the change. Otherwise runDev Containers: Open Folder in Container... to connect to the container.

Option 2: Use an env file

If you have a large number of environment variables that you need to set, you can use a.env file instead.

First, create an environment file somewhere in your source tree. Consider this.devcontainer/devcontainer.env file:

YOUR_ENV_VAR_NAME=your-value-goes-hereANOTHER_ENV_VAR_NAME=your-value-goes-here

Next, depending on what you reference indevcontainer.json:

  • Dockerfile or image: Editdevcontainer.json and add a path to thedevcontainer.env :

    "runArgs": ["--env-file",".devcontainer/devcontainer.env"]
  • Docker Compose: Editdocker-compose.yml and add a path to thedevcontainer.env file relative to the Docker Compose file:

    version:'3'services:  your-service-name-here:    env_file:devcontainer.env    # ...

docker compose will automatically pick up a file called.env in the folder containing thedocker-compose.yml, but you can also create one in another location.

If you've already built the container and connected to it, runDev Containers: Rebuild Container from the Command Palette (F1) to pick up the change. Otherwise runDev Containers: Open Folder in Container... to connect to the container.

Video: Load variables from an .env file

12/10/2025

    [8]ページ先頭

    ©2009-2025 Movatter.jp