- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
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.
<html> <body> Welcome to TutorialsPoint Docker tutorials </body> </html>
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.
- Running GUI applications on docker in linux
- How to execute Python CGI Script on Apache Server?
- Running a Docker image as a container
- How to Configure Nagios Server for Monitoring Apache Server
- Websockets Apache Server compatibility in HTML5
- How to Setup Virtual Hosts with Apache Web Server on Linux
- Running Docker Container as a Non Root User
- How to change the root directory of an Apache server on Linux?
- Publishing a Docker Image on Dockerhub
- How To Set Up Multiple SSL Host With A Single Apache Server
- How To Install Apache Maven on Ubuntu
- User defined bridge on Docker network
- How is Docker different from a Virtual Machine?
- How to install docker on centos 7
- How To Configure mod_rewrite for Apache on CentOS 7