Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing env variables within docker-compose.yml #5559

Closed
bentcoder opened this issue Dec 18, 2017 · 2 comments
Closed

Accessing env variables within docker-compose.yml #5559

bentcoder opened this issue Dec 18, 2017 · 2 comments
Labels
area/compose Relates to docker-compose.yml spec or docker-compose binary lifecycle/locked

Comments

@bentcoder
Copy link

Hi,

I am getting errors below when trying to run docker-compose up. The errors are related to the docker-compose.yml file not the Dockerfile though. How can I make docker-compose.yml access env variables as well?

Thanks

Versions

docker-compose version 1.17.0, build ac53b73
Docker version 17.09.0-ce, build afdb6d4

Errors

WARNING: The MY_IP variable is not set. Defaulting to a blank string.
ERROR: The Compose file 'docker-compose.yml' is invalid because:
services.php.ports is invalid: Port ranges don't match in length

variables.env

MY_IP=192.168.0.11
MY_PORT=9000

Dockerfile (vars work here)

FROM php:7.1-fpm
ENV MY_IP
ENV MY_PORT

docker-compose.yml

version: '3'
services:
    php:
        build: ./php
        ports:
            - ${MY_PORT}:80
        networks:
            public_net:
                ipv4_address: ${MY_IP}
        env_file:
            - variables.env
networks:
    public_net:
        ...
@johndmulhausen johndmulhausen added area/compose Relates to docker-compose.yml spec or docker-compose binary issue/support-request labels Dec 19, 2017
@bentcoder bentcoder reopened this Dec 29, 2017
@thaJeztah
Copy link
Member

The env_file option only sets environment-variables for the docker container that is started, but environment variable interpolation in the docker-compose file itself is done from actual environment variables set in your shell session.

For example;

export MY_IP=192.168.0.11
export MY_PORT=9000

Then running docker-compose up should work

I notice you're "baking" those values into the container's image; given that these values may change, it's probably better to set them at runtime (unless you really need them during build)

Assuming your php image does not need other build-steps than the ones you showed, you can use the upstream php:7.1-fpm image directly, and set the environment variables for the container at runtime;

version: '3'
services:
    php:
        image: php:7.1-fpm
        ports:
            - ${MY_PORT}:80
        networks:
            public_net:
                ipv4_address: ${MY_IP}
        environment:
            - MY_PORT
            - MY_IP
networks:
    public_net:

Notice that you don't add an = or =some value to the environment variables; omitting the = tells docker-compose to set those environment variables from actual environment variables on the host (if set).

If you don't want to export those environment variables before running, you can also create a .env file in the same directory as the compose-file, and docker compose will read that file when running commands (but be aware that this feature is not supported when deploying your compose file using docker stack deploy)

@docker-robott
Copy link
Collaborator

Closed issues are locked after 30 days of inactivity.
This helps our team focus on active issues.

If you have found a problem that seems similar to this, please open a new issue.

/lifecycle locked

@docker docker locked and limited conversation to collaborators Mar 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/compose Relates to docker-compose.yml spec or docker-compose binary lifecycle/locked
Projects
None yet
Development

No branches or pull requests

4 participants