Deploy your website with a Docker Apache httpd container volume

Use local volumes in Docker’s httpd container

You don’t need to copy all of your website’s files into a dockerized Apache httpd image to test your website with Docker, especially if all of your website’s files are on a local hard drive.

There’s a much simpler approach, one that is especially useful for testing on your local machine. Simply run the official Apache httpd Docker image locally and create a Docker volume that points to files on your local machine.

The benefits of this approach include:

  • You don’t need to update an custom image every time you change a file
  • You don’t need to code a Dockerfile
  • You don’t need to create a custom Apache httpd Docker image
  • You can update local website files on the fly
  • You can easily switch between versions of Apache to perform compatibility tests

Docker httpd volume mapping

To run an Apache httpd Docker container with a volume mapping that points to the local file system, simply issue a docker run command with these attributes:

  1. The -d switch to run the process as a daemon
  2. The --name switch to provide a friendly name for the container
  3. Mapping port 80 to an open port on your machine
  4. Volume mapping your website files into the httpd Docker images’ htdocs folder
  5. The name and optionally the version of the Docker httpd image to use

Docker run example

The command to run the dockerized Apache https service with local files is as follows:

docker@httpd /c/example/rock-paper-docker
docker run -d --rm --name my-apache-app -p 80:80 -v $(PWD)/website:/usr/local/apache2/htdocs/ httpd:latest

Invalid Reference Format Docker error

Note: you may run into an invalid reference format error when the above docker run command is run in PowerShell. You may have better luck with this version on Windows:

docker run -d --rm --name my-apache-app -p 80:80 -v ${PWD}:/usr/local/apache2/htdocs/ -d httpd:latest

The invalid reference format error happens when the Docker command does not properly process the $PWD variable. You may need to put the $PWD variable within curly braces to make the Docker Apache run example work on Windows.

In this example, a Dockerfile is not required to create a custom httpd Docker image. Files are read from the local file system through a mapped Docker volume.

 

Location of htdocs in Docker

When a Docker volume mapping isn’t performed, anything inside the Apache httpd container’s htdocs folder is served up over the web when the server runs.

Here is the full path to the Apache htdocs folder inside the Docker httpd image:

/usr/local/apache2/htdocs/ 

Docker volume mapping of $PWD

In the docker run command the /website/ folder, which is in a sub directory of the present working directory (PWD) in which the docker run command was issued, maps to the Apache container’s htdocs folder:

-v ${PWD}/website:/usr/local/apache2/htdocs/ 

When the Docker httpd container runs, Apache serves files from the present working directory’s /website directory. It uses the local file system of the host machine instead of the container’s internal htdocs folder.

When the Apache Docker httpd container runs, local files are loaded and accessible on the localhost address.

Apache Docker http image example

With this configuration, you can add, update and delete files locally, and the Docker httpd image updates your website immediately. There is no need to create a new image, redeploy your Docker hosted web application, or even stop and start the Docker httpd service.

Sometimes it is necessarily to build a custom Docker http image with all of your website filed copied into the container’s internal htdocs folder. But for local development and testing, a simple volume mapping is more than sufficient.

 

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close