I’m trying to Dockerize my Apache/Django project locally. On my local machine (running Mac Sierra) I have this file (maps.conf) in my /etc/apache2/other/ directory …
<VirtualHost *:80>
ServerName maps.example.com
Alias /static /Library/WebServer/Documents/maps/maps/static
<Directory /Library/WebServer/Documents/maps/maps/static>
Require all granted
</Directory>
# Next, add the following directory block
<Directory /Library/WebServer/Documents/maps/maps_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess maps python-home=/Library/WebServer/Documents/maps/venv python-path=/Library/WebServer/Documents/maps
WSGIProcessGroup maps
WSGIScriptAlias / /Library/WebServer/Documents/maps/maps_project/wsgi.py
</VirtualHost>
I found this Apache docker file …
FROM httpd:2.4
COPY ./public-html/ /usr/local/apache2/htdocs/
Where am I supposed to copy my VirtualHost directive above so that my Apache docker image connects properly?
2
Answers
The short answer to your question is: your posted
VirtualHost directive
will not work inside the docker container because it is referencing your local machine directories:/Library/WebServer/Documents/maps/maps/static
and/Library/WebServer/Documents/maps/maps_project
You will need to create a custom VirtualHost directive that references the path inside your docker container but not in your local machine.
TL;DR
It’s at
/usr/local/apache2/conf/extra/httpd-vhosts.conf
, but you’ll need to do more than just copying yours there because a) Apache won’t know about it; and b) your macOS paths won’t work.Long version
The Docker docs for the official Apache image show how to extract the main Apache config:
Now you have a
my-httpd.conf
locally, outside the container. Open it up and have a look – it is just a standard Apache config, and towards the bottom you’ll see the usual:So now we can see both how to include a vhost config, and where to put it.
First, uncomment that
Include
line in your local copy ofmy-httpd.conf
so that the vhosts config will actually be included. We’ll copy that edited version into the container, overwriting the original, so it will be used at runtime.Now we need to update your existing vhosts config file (
maps.conf
) so it is suitable for use in the container. The macOS-specific paths you’re currently using in that file won’t exist inside the container, so the file won’t work as-is.You can see the main Docroot in the main
my-httpd.conf
file:For simplicity let’s put your content under that main Docroot, say in a dir called
maps/
. Copy yourmaps.conf
to a new file (saymy-vhosts.conf
in the same dir asmy-httpd.conf
), updating your macOS/Library/WebServer/Documents/maps
paths with the container path/usr/local/apache2/htdocs/maps
. I can’t replicate your app/config, but for the sake of demonstration I used this super simplemy-vhosts.conf
:Next put it all together in the Dockerfile. It needs to copy in the 2 new config files, as well as your actual content. Note that you can’t use a an absolute path for the source file in a
COPY
command, so you’ll need to move or copy your localmaps/
content into the current directory first.Now just build it and run it. Again using the commands shown in the Docker docs for Apache:
Finally, for the sake of completeness, for local development where you might not want to keep recreating and running the image as you work on your app or tune your config files, you could map those things into the container as volumes, rather than just copy them in once at build-time. That way you can work and see your changes live in the container.
Easiest approach for that would be to use
docker-compose
, and adocker-compose.yml
:Then
docker-compose up
to run it.