Apache httpd in a Docker container

I have a ‘getting started‘ post on Docker already, so I thought it would be useful to pull together some of the simplest possible examples of building and running Docker containers, as real examples.

On the Docker Hub there’s many prebuilt images already. As a quick example, let’s use the Apache Httpd image and build a container that runs httpd and serves an example webpage.

This is pretty much the instructions that are on the httpd image page, so no surprises here.

In a new folder, create a Dockerfile:

FROM httpd:2.4
COPY ./public-html/ /usr/local/apache2/htdocs/

Create an index.html file in public-html and put some hello world content in it.

Next, from a Docker Quickstart Terminal, cd into the folder when you created the Dockerfile and public-html, and build your image:

docker build -t imagename .

Now to start up a container based on your image called imagename (name it whatever you like), and expose port 80 on the container to port 80 to the outside world:

docker run -it -p 80:80 imagename

Now point a browser at the IP of your docker machine (displayed when Docker Quickstart starts up), and success, you should see your html being served from apache in your container!

If you’re on Windows, the equivalent to capture the tty output is:

winpty docker run -it -p 80:80 imagename

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.