1

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

YonzLeon
  • 168
  • 5

1 Answers1

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
Gerald Schneider
  • 19,757
  • 8
  • 52
  • 79
  • Afterthoughts: `DocumentRoot` most probably doesn't need a variable, since it is going to be static. You are rather mounting different volumes in the same place in various containers. The `ServerName` doesn't really need a variable either, it will most probably be handled by a reverse proxy in front of the container. – Gerald Schneider Oct 07 '21 at 13:13
  • Thank you! actually im just try to know how to make ENV on image regardless of `ServerName` configuration and others – YonzLeon Oct 07 '21 at 13:55