Running a static site on Apache Server from Docker


No doubt that Docker containers are now being widely used for various purposes in the development lifecycle right from building, distributing, testing to deployment of the products. It allows developers to work on different parts of the project, collaborate with other developers working on the same project, helps in version management of the project and in some cases, also allows to test the product by hosting on its own servers.

Let’s say you have built a static website inside the docker container and now you want to test that website by running it on a browser. Docker allows you to create an apache server inside the container and host the website locally and connect it via ports so that you can display it on your local machine.

In this article, we are going to see how we can run a static site on an apache server inside a docker container and access it on our local machine. To start with, we need to create a dockerfile so that it can build an environment which has an apache server installed in it. We also need a simple static website which we would run on that server. A static website is one which does not render data and information dynamically or on the go and mostly consists of html and css and without javascript.

Let’s create a dockerfile and include commands to pull a base image which would contain the apache server pre-installed. Httpd is the dominant http server which contains the web server application called Apache.

We use the FROM instruction to pull that base image. We then create a directory called mysite which contains an html file called index.html which would contain the html content of our static website. Hence, the directory structure would be the main folder inside which we would have our dockerfile and a folder called mysite and inside that folder would be our index.html file.

Index.html

<html>
<body>
Welcome to TutorialsPoint Docker tutorials
</body>
</html>

Dockerfile

FROM httpd:2.4
WORKDIR /usr/local/apache2/htdocs/
COPY ./mysite/

We set our working directory inside the container to be /usr/local/apache2/htdocs/ and then we copy the mysite folder which contains the html file to our working directory in the container.

After we have completed our index.html file and the dockerfile, we are set to build our docker image using the docker build command with the help of our dockerfile. To build the docker image, we use the following command −

sudo docker build −t static−image .

The above command builds an image called static-image. Now that we have built our image, we can start a container by using the docker run command. In order for the docker container to serve our website on the browser in our local machine, we need to connect the ports of our docker container and the local system. We can do so by using the −p flag with our docker run command.

sudo docker run −p 80:80 −−name static−image−1 static−image

The above run command connects the port number 80 of our container and local machine with each other. Using the −−name option, we have provided a name to our container called static−image−1. After executing this command, we have the docker container static−image−1 running and ready to serve our static website on the address http://localhost:80

You can now fire up your browser and browse the link http://localhost:80 . You will see the content “Welcome to TutorialsPoint Docker tutorials” being displayed there. This means that the site is being served on the apache server.

To conclude, you can use docker containers to serve your static website. When you make changes in the website html, you will see the changes being reflected in the website display as well. You can also specify a different port number if you have some other application being served on that container.

Updated on: 27-Oct-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements