0

I am confused, because it seems that an environment variable which is set in docker-compose.yml causes docker to rebuild my image on docker-compose up:

Dockerfile for apache_php_with_custom_docroot

FROM php:7.1.11-apache-jessie

# Set apache document root
ENV APACHE_DOCUMENT_ROOT /var/www/web
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

I'm building it with docker build -t apache_php_with_custom_docroot .

docker-compose.yml

version: "3"
services:
  web:    
    image: apache_php_with_custom_docroot
    environment:
      - APACHE_DOCUMENT_ROOT=/var/www/anotherfolder

So, the strange thing to me is: It works to set the APACHE_DOCUMENT_ROOT. When I run docker-compose up -d apache uses the docroot /var/www/anotherfolder.

I was sure, when my image was build, then I can't change it in my docker-compose file. I read something about build parameters e.g. here: Docker-compose: Set a variable in env file and use it in Dockerfile

But I did not set build params. How does docker know, that he has to rebuild my image? Where can I read about the principles behind this?

1
  • a bit rusty with docker! , as far as i know , every change in docker-compose.yml is need to make an up (because docker reads it and create the virtual filesystem and the container behavior), so i thing you need to attach this on the entrypoint , where you i.e can read a file with vars and load it every time containers start ... Commented Nov 7, 2017 at 15:23

1 Answer 1

-1

define a entry point in your dockerfile like:

ENTRYPOINT bash $APACHE_DOCUMENT_ROOT/entrypoint.sh

And then in your entry point every time containers starts, you will be able to read mapped volume with a file and inject in your container or declare the environments or related startup task you will need

enviroment=$(cat /mapped_config/enviroment.env)
if grep -Fxq "$enviroment" /home/.bashrc
  then 
      echo "Environment setted propertly"
  else
      echo "$enviroment" >> /home/.bashrc
      echo "enviorment setted!"
fi

It's just an idea , and probably not the best way , is how i deal it in past.

Sign up to request clarification or add additional context in comments.

2 Comments

-1, because this is not an answer to my question. I do not understand why my code works, i did not search for an answer how this could be done in another way...
i just trying to explain when you do docker-compose up, docker will read your docker-compose.yml file and build your image ( with config and vars you defined ), after if you will make any change on docker-compose file ( such as change env var etc..) you will need to build again ( not stop , start or restart ) if you wanna read something dynamically when containers starts you can do this staff in the entrypoint

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.