Cannot get virtual host to work in docker apache container

Hoping someone can advise… I have been trying to create a simple apache container that configures a virtual host in apache then displays its custom index.html. Outside of docker, this works fine, but when I attempt to replicate such config in a Dockerfile, it either fails with ‘can’t locate apache2’ error if I try a ubuntu base image or it just displays the default ‘It works!’ index.html if I try the httpd 2.4 base image. My latest attempt at Dockerfile is as follows:

FROM httpd:2.4
LABEL maintainer="someemail@gmail.com"
CMD mkdir /var/www/html/tutorial_docker/
COPY ./index.html /var/www/html/tutorial_docker/
COPY ./tutorialDocker.conf /etc/apache2/sites-available/
EXPOSE 80

Thanks if someone could advise?

Hello and welcome,

could you please provide the content of tutorialDocker.conf and the way you’ve started the container?

As apache is not part of the default-ubuntu-image the mentioned error-message is normal - you have to install it using apt-get update && apt-get install apache2 or use an image which is specialized for providing the apache-service (as you did using the httpd-image).
I would prefer the way using the httpd-image as it should be smaller and provide less additional and not needed software (which is good for security-reason).

Best regards
Matthias

Hi thanks

Well for what it’s worth, the tutorialDocker.conf is as follows:

<VirtualHost *:80>
	ServerAdmin admin@example.com
	ServerName example.com
	ServerAlias www.example.com
	DocumentRoot /var/www/html/tutorial_docker
	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

And the command to run the new docker image was as follows:

sudo docker run -d -p 9090:80 --name webserver tutorial_docker:1.0

However to your point, and after following the httpd base docker image documentation, I was able to get it to work rather aimply, i.e.

FROM httpd:2.4
COPY ./src_files/index.html /usr/local/apache2/htdocs/

Thanks for your help!

TLDR; config-file was placed at wrong location, was not loaded and configuration for document-root was missing

Hello,

I’m sorry for the bad experience you received from the tutorial (which tutorial?) - but with the unmodified Dockerfile and .conf-file it would not work.

I did some tests and modified the Dockerfile to

FROM httpd:2.4
LABEL maintainer="someemail@gmail.com"
RUN mkdir -p /var/www/html/tutorial_docker/ && sed -i -E 's:^#(Include conf/extra/httpd-default.conf):\1:' /usr/local/apache2/conf/httpd.conf
COPY ./index.html /var/www/html/tutorial_docker/
COPY ./tutorialDocker.conf /usr/local/apache2/conf/extra/httpd-default.conf
EXPOSE 80

to place your tutorialDocker.conf within the image as /usr/local/apache2/conf/extra/httpd-default.conf and to load it by modifying the /usr/local/apache2/conf/httpd.conf.

And I’ve modified the tutorialDocker.conf to

<Directory "/var/www/html/tutorial_docker">
  Options Indexes FollowSymLinks
  AllowOverride All
  Require all granted
</Directory>

<VirtualHost *:80>
  ServerAdmin admin@example.com
  ServerName example.com
  ServerAlias www.example.com
  DocumentRoot /var/www/html/tutorial_docker
  ErrorLog /proc/self/fd/2
  CustomLog /proc/self/fd/1 combined
</VirtualHost>

to send the log-information to the place where you can get them with docker logs webserver and to specify some settings about the directory you have specified as DocumentRoot. Without the <Directory ... apache will return a 403 - Forbidden.

Best regards
Matthias

Hi Matthias thanks for taking such effort to elaborate on my question. It’s greatly appreciated.

I just called this my own personal docker tutorial, I’d previously followed some tutorial on how to stand up a simple website in Apache and figured this could be replicated in docker. Seems however, I was not aware of the nuances of such a task.

I tried building your script but unfortunately it failed with the following error:

sed: -e expression #1, char 47: invalid reference \1 on `s’ command’s RHS

against the following line:

RUN mkdir -p /var/www/html/tutorial_docker/ && sed -i 's:^#(Include conf/extra/httpd-default.conf):\1:' /usr/local/apache2/conf/httpd.conf

Sounds like something rather trivial but I know about sed even less than I know docker :expressionless:

I’m sorry for this error. I forgot the -E-parameter for sed. Without this parameter the regular expressions and grouping will not work. The answer above is now updated.

Thanks again Matthias.

I’ve reviewed your fine work and believe I now understand how you scripted the Dockerfile. Basically you utilised stream editor linux command to find a string in httpd.conf so as to be able to uncomment / include httpd-default.conf in this Apache implementation. You then overwrite httpd-default.conf 's contents with tutorialDocker.conf, which you had previously defined to properly enable my virtual host as well as enable its logging for the docker logs [container name] command.

This has been a constructive exercise and I appreciate your time. Thanks heaps!