How to make a custom docker image can define the ENV variable

I've created a custom homemade docker image containing the vhosts configuration, how can I define variables like ServerName, DocumentRoot in an ENV when running the container.

Thanks, really appreciate to all answers


Solution 1:

You can use the ${APACHE_DOCUMENT_ROOT} notation in the Apache config file.

I took the example from this unanswered question:

<VirtualHost _default_:80>
  ServerSignature Off

  ErrorLog ${APACHE_LOG_DIR}/000-default-error.log
  CustomLog ${APACHE_LOG_DIR}/000-default-access.log combined

  DocumentRoot ${APACHE_DOCUMENT_ROOT}

  <Directory ${APACHE_DOCUMENT_ROOT}>
    Options FollowSymLinks
    AllowOverride all
    Require all granted
  </Directory>
</VirtualHost>

A very basic Dockerfile:

FROM httpd:2.4
COPY *.conf /usr/local/apache2/conf/

Then I started the built container:

mkdir {log,htdocs}
echo "hi" > htdocs/index.html
docker run -d --rm 
  -e APACHE_LOG_DIR=/var/log/apache2 \
  -e APACHE_DOCUMENT_ROOT=/var/www/htdocs/ 
  -v $PWD/htdocs:/var/www/htdocs 
  -v $PWD/log:/var/log/apache2/ 
  -P httpd:test

And it works.

$ curl http://localhost:32768/
hi
$ ls -l log/
total 4
-rw-r--r-- 1 root root 85 Oct  7 15:06 000-default-access.log
-rw-r--r-- 1 root root  0 Oct  7 15:05 000-default-error.log