How to run Apache in a docker container

This article describes how to run the Apache HTTP Server in a docker container in Ubuntu 18. This is a useful approach when you don’t want to install Apache directly in Ubuntu. This article will also cover how to mount a local directory into the docker container, allowing you to create your website locally while having it served by Apache in a docker container.

These steps assume you already have docker installed in Ubuntu. If not, check this article for steps to install docker.

Create the docker container

  1. Create a new directory and change into it

    mkdir docker-apache
    cd docker-apache
  2. Create another directory within docker-apache/ to store your HTML, CSS, Javascript, and any other files you want accessible to the docker container.

    mkdir html

    When we run the container, this directory will be mounted so that changes to any files here will be reflected instantly by Apache.

  3. Now create a dockerfile to define the container

    gedit dockerfile &

    Paste the following into the dockerfile and save:

    FROM httpd:2.4
    COPY ./html/ /usr/local/apache2/htdocs/
    • FROM httpd:2.4 tells Docker we want to use the httpd image. Read more about it here:
    • COPY will copy our local /html/ directory into the container at location /usr/local/apache2/htdocs/
  4. Now build the docker container:

    sudo docker build -t apache .

    Since the httpd image does not exist locally, Docker will download it as part of the build. Subsequent builds of this container will be faster since you only need to download the base httpd image once.

  5. Ensure you do not get any errors and all files are successfully copied to the container. A clean build will look similar to the screenshot below.

    Docker container build

  6. List the available docker images to verify the build completed successfully:

    sudo docker images

    You should see httpd (the base image) and apache (the built container image) in the list.

  7. If you have hit an error and need to delete an image before rebuilding, use the below command:

    sudo docker rmi apache

Run the docker contanier

  1. Now you can run the docker container. We’ll also map local port 80 to container port 80 so you can test your site in a browser. We’ll also mount the local /html directory into the container. This is to enable us to edit any website files within Ubuntu and have the changes immediately served by apache in the docker container.

    sudo docker run -dit --name myapache -p 80:80 --mount src="$(pwd)/html",target=/usr/local/apache2/htdocs,type=bind apache
    • -p maps container port 80 to local port 80
    • --mount will mount the html directory we created previously into the docker container at location /usr/local/apache2/htdocs

    Note: I have used $(pwd)/html to refer to my local html file. This assumes html exists in the current directory where you are running the command. This path must be absolute.

  2. If you get any errors running the container, you may have to remove it before you can try running it again:

    sudo docker rm -f myapache

    You can tell Docker to automatically remove the container for you by specifying the --rm option when you run the container, for example:

    sudo docker run -dit --rm --name myapache -p 80:80 --mount src="$(pwd)/html",target=/usr/local/apache2/htdocs,type=bind apache
  3. Open a web browser and naviate to http://localhost/. Since we have no index.html file, you should see something like this:

    Firefox apache index

Editing your website

  1. You can start creating files in the local html directory and testing them in your browser. For example, create html/index.html:

    cd html
    gedit index.html &

    And paste the following and save:

    Hello from apache docker container!
  2. Refesh the page in your browser and now you will see the text Hello from apache docker container!

    Firefox apache index

Stopping the docker container

The docker container will contine to run in the background. You can stop the container with the following command:

sudo docker rm -f myapache