PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, August 19, 2022

[FIXED] How to use environment variables with docker-compose from other directory

 August 19, 2022     docker, docker-compose, environment-variables     No comments   

Issue

I'm setting up a new docker-based server and I'm writing a script to run

$ docker-compose

for all my docker-compose.yml files at once.

I know about existence of .env files and I know about env_file option in docker-compose.yml, however the .env files are ignored when I launch

$ docker-compose up

from other directory and not even full path in env_file helps.

my_script.sh:

#!/bin/bash

docker-compose up -f /server1/docker-compose.yml -f /server2/docker-compose.yml -f /server3/docker-compose.yml

Docker-compose.yml:

version: '3'

services:
  service_name:
    build: /server1/apache
    env_file: /server1/.env
    volumes:
      - /web/${ROOT_DOMAIN}/web/:/var/www/:rw

.env

ROOT_DOMAIN=server1.domain.tld

I want to replace the ${ROOT_DOMAIN} variable in volumes for the variable defined in .env file. This works like a charm, when I run

$ docker-compose up

from the /server1/ folder. However when I run my script, which is in other directory, the enviroment variables are ignored and default to null.

How to fix this?


Solution

Sorry to say, but this cannot be fixed. The provided .env file must be in the same directory.

This is a requirement of docker compose and is described in the docs (first paragraph): https://docs.docker.com/compose/env-file/



Answered By - mbuechmann
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing