devcontainers / spec

Development Containers: Use a container as a full-featured development environment.
https://containers.dev
Creative Commons Attribution 4.0 International
3.56k stars 227 forks source link

Environment and pre-defined variables are not supported in `forwardPorts` and keys of `portsAttributes` #489

Open i-ky opened 2 months ago

i-ky commented 2 months ago

To avoid hardcoding hostnames I am using environment variables, e.g. ${POSTGRES_HOSTNAME}. This allows pointing application or any supplementary scripts to the correct host depending on the environment (e.g. in and out of dev container). Same variables are used as hostname: in Docker Compose files:

services:
  postgres:
    image: postgres:latest
    hostname: ${POSTGRES_HOSTNAME}
    ...
  ...

And same variables should be used in forwardPorts and portsAttributes of dev container configuration:

{
    ...
    "forwardPorts": [
        "${containerEnv:POSTGRES_HOSTNAME}:5432",
        ...
    ],
    "portsAttributes": {
        "${containerEnv:POSTGRES_HOSTNAME}:5432": {
            ...
        },
        ...
    },
    ...
}

Unfortunately, forwardPorts and portsAttributes don't support variables, so it needs to be hardcoded instead:

{
    ...
    "forwardPorts": [
        "postgres:5432",
        ...
    ],
    "portsAttributes": {
        "postgres:5432": {
            ...
        },
        ...
    },
    ...
}

...meaning that hostname needs to be changed manually in two more places if the environment variable changes.

chrmarti commented 2 months ago

Does ${localEnv:POSTGRES_HOSTNAME} work?

i-ky commented 1 month ago

Hi @chrmarti!

Thank you for looking into this.

I am not sure what "work" means if we are talking about the spec here. Do you have a specific implementation in mind?

https://containers.dev/implementors/json_reference/ seems to talk about pre-defined variables as a whole. Either

Environment and pre-defined variables may be referenced in the values.

...or nothing at all. So my expectation is that if containerEnv: does not work, neither will localEnv:.

chrmarti commented 1 month ago

containerEnv: only works with container env variables and I don't see from your report if you set POSTGRES_HOSTNAME in the container. I do see that you are using the local env variable POSTGRES_HOSTNAME in the docker-compose.yml, so localEnv: might work, depending on where you set POSTGRES_HOSTNAME locally.

i-ky commented 1 month ago

I don't see from your report if you set POSTGRES_HOSTNAME in the container. I do see that you are using the local env variable POSTGRES_HOSTNAME in the docker-compose.yml, so localEnv: might work, depending on where you set POSTGRES_HOSTNAME locally.

Sorry, I didn't mention it. POSTGRES_HOSTNAME is set in .env file (from there POSTGRES_HOSTNAME gets into Compose file) and .env file is also passed to the dev container in Compose file as env_file: (this is how POSTGRES_HOSTNAME gets into container environment).

Since POSTGRES_HOSTNAME is not set in host environment, I think that localEnv: will not work. Do you want me to try?

LabTopProductions commented 1 month ago

Please help me with this issue and report back on things that are not working properly in my dev containers for the account I have been using for the last two months I need to get a new one I don’t know what to do with this issue but please advise

Brecht Vrolix

On Tue, 17 Sep 2024 at 11:20, i-ky @.***> wrote:

I don't see from your report if you set POSTGRES_HOSTNAME in the container. I do see that you are using the local env variable POSTGRES_HOSTNAME in the docker-compose.yml, so localEnv: might work, depending on where you set POSTGRES_HOSTNAME locally.

Sorry, I didn't mention it. POSTGRES_HOSTNAME is set in .env file (from there POSTGRES_HOSTNAME gets into Compose file) and .env file is also passed to the dev container in Compose file as env_file: (this is how POSTGRES_HOSTNAME gets into container environment).

Since POSTGRES_HOSTNAME is not set in host environment, I think that localEnv: will not work. Do you want me to try?

— Reply to this email directly, view it on GitHub https://github.com/devcontainers/spec/issues/489#issuecomment-2354999102, or unsubscribe https://github.com/notifications/unsubscribe-auth/AS6S7WNXVZ4JHNQUURD6FU3ZW7X5NAVCNFSM6AAAAABMQRHLYCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNJUHE4TSMJQGI . You are receiving this because you are subscribed to this thread.Message ID: @.***>

chrmarti commented 1 month ago

Since POSTGRES_HOSTNAME is not set in host environment, I think that localEnv: will not work. Do you want me to try?

You're right. Support for env files has come up before. Related: https://github.com/microsoft/vscode-remote-release/issues/3007

i-ky commented 1 month ago

Since POSTGRES_HOSTNAME is not set in host environment, I think that localEnv: will not work. Do you want me to try?

You're right. Support for env files has come up before. Related: microsoft/vscode-remote-release#3007

Are you implying that if POSTGRES_HOSTNAME was set in host environment then localEnv: would work in forwardPorts?