Home » DevOps » Docker » How to Setup a Simple Apache Web Server in a Docker Container

About Yatin

Working as a Technical Lead in the Information Technology sector where I'm primarily involved with projects based on Java/J2EE Technologies platform (such as Spring, Spring Boot, Hibernate, Redis, etc.), novel UI technologies (Angular & React), Docker, and AWS. Recently completed the AWS Solution Architect Associate Certification.

How to Setup a Simple Apache Web Server in a Docker Container

Welcome readers, in this tutorial, we will discuss how to host a static webpage running on Apache web server on Docker.

1. Introduction to Docker

In the present world, Docker is an important term,

  • Often used in CI/CD platform that packages and runs the application with its dependencies inside a container
  • Is a standard for Linux Containers
  • A Container is a runtime that runs under any Linux kernel and provides a private machine-like space under Linux

1.1 Docker Terminology

  • Image: Representation of Docker container i.e. a JAR or WAR file in Java
  • Container: Runtime of Docker i.e. a deployed and running Docker image. For example, an executable Spring Boot jar
  • Engine: The code that manages, creates and runs the Docker containers
  • Hub: A public developers registry to distribute their code
  • Repository: A collection of Docker related images i.e. different versions of the same application

1.2 Docker Command Basics

Here’s an example command.

Apache Docker - Basic structure
Fig. 1: Basic structure

1.3 Need for using Docker

  • For environment replication, while the code runs locally on the machine
  • For numerous deployment phases i.e. Dev/Test/QA
  • For version control and distributing the application’s OS within a team

1.4 Setting up Docker

If someone needs to go through the Docker installation, please watch this video.

To start with this tutorial, we are hoping that users at present know how to create a simple hello-world spring-boot application.

2. How to Setup a Simple Apache Web Server in a Docker Container

Here is a systematic guide for implementing this lab tutorial.

2.1 Application pre-requisite

To host a static site on Apache server, developers need to create a Dockerfile that will, in turn, create an image file (Containing the static webpage) for our application. To create an image file, we’ll add the Dockerfile in the project’s root directory as shown in the project structure and add the following code to it.

Apache Docker - Project structure
Fig. 2: Project structure

Dockerfile

# Pull the httpd image from DockerHub repository to build our application as a base
FROM httpd:2.4
# Copy the static page from the target directory to apache2 docs
COPY ./public-html/ /usr/local/apache2/htdocs/

2.2 Build the image

Open the terminal and navigate to the project directory and execute the below commands to build the application and the image.

# To prepare the image with a given name
> docker build -t my-apache2 .

If everything goes well, the image will be created. We can use the docker images command to list the newly created image as shown in Fig. 3.

Apache Docker - newly created Docker image
Fig. 3: Viewing the newly created Docker image

2.3 Starting the Docker container

Now we can run the below command to start the container and can hit the application URL (http://localhost:80/) as shown in Fig. 4.

docker run  -p 80:80 --name my-apache2-1  my-apache2
Apache Docker - Application’s index page
Fig. 4: Application’s index page

Try out these commands in your development environment to practice and learn.

3. Conclusion

That is all for this tutorial and I hope the tutorial will help you to set up an Apache web server on Docker. Happy learning and do not forget to share!

4. Download the source code

Download
You can download the full source code of this example here: How to Setup a Simple Apache Web Server in a Docker Container

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments