František Šitner
František Šitner

Reputation: 51

docker-compose how to pass env variables into file

Is there any chance to pass variables from docker-compose into apache.conf file? I have Dockerfile with variables

ENV APACHE_SERVER_NAME localhost
ENV APACHE_DOCUMENT_ROOT /var/www/html

I have apache.conf which I copy into /etc/apache2/sites-available/ while building image

ServerName ${APACHE_SERVER_NAME}
DocumentRoot ${APACHE_DOCUMENT_ROOT}

I have docker-compose.yml

environment:
        - APACHE_SERVER_NAME=cms
        - APACHE_DOCUMENT_ROOT=/var/www/html/public

When I run docker-compose, nothing happened and apache.conf in container is unchanged. Am I completely wrong and is this imposible or am I missing any step or point? Thank you

Upvotes: 1

Views: 1610

Answers (1)

Alejandro Galera
Alejandro Galera

Reputation: 3691

Let me explain some little differences among ways to pass environment variables:

Environment variables for building time and for entrypoint in running time

  • ENV (Dockerfile): Once specified in Dockerfile before building, containers, will have environment variables for entrypoints.
  • environment: The same than ENV but for docker-compose.
  • docker run -e VAR=value... The same than before but for cli.

Environment variables only for building time

  • ARG (Dockerfile): They won't appear in deployed containers.

Environment variables accesibled for every container and builds

  • .env file defined in your working dir where executes docker run or docker-compose.
  • env_file: section in docker-compose.yml file to define another .env file.

As you're trying to define variables for apache conf file, maybe you should try to use apache as entrypoint, or just define them in .env file.

Upvotes: 1

Related Questions