Install Apache in Docker Container on Ubuntu 22.04

In this guide, we want to teach you to Install and Run an Apache Web Server in a Docker Container on Ubuntu 22.04 via Docker Hub and Dockerfile. You can deploy an application in a dedicated docker container to make it easy to set up and configure. You can follow this guide to start Apache installation in a Docker Container on an Ubuntu server.

How To Install and Run Apache in Docker Container on Ubuntu 22.04?

You need some requirements before you start your Apache installation in a Docker container. Let’s see what we need.

Requirements

You must have access to your server as a non-root user with sudo privileges. To do this, you can follow this guide on Initial Server Setup with Ubuntu 22.04.

Also, you must have Docker running on your server. For this purpose, you can check this guide on Install Docker on Ubuntu 22.04.

When you are done, follow the steps below to complete this guide.

You can use two methods to install Apache in a Docker container:

  • Via Docker Hub
  • Via Dockerfile

Step 1 – Install Apache from Docker Hub on Ubuntu 22.04

At this point, you can easily pull the Apache image from Docker Hub on your server. The official image that is available for Apache in Docker Hub is httpd. To download the Apache image on Ubuntu 22.04, run the command below:

docker pull httpd

When your download is completed, you will get the following output:

Output
Using default tag: latest
latest: Pulling from library/httpd
52d2b7f179e3: Pull complete
5bfaffbad7bf: Pull complete
460cd5c32012: Pull complete
ba29f61f6139: Pull complete
92baf798eff7: Pull complete
Digest: sha256:333f7bca9fb72248f301fd2ae4892b86d36cc2fdf4c6aa49a6700f27c8e06daf
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest

How To Run Apache Container on Ubuntu 22.04?

At this point, you must create a container for your Apache image and start your container on Ubuntu 22.04.

You can use the docker run command with the –name flag to create your container and run it:

docker run -d --name [container-name] -p 80:[host-port] httpd

Here our command is as follows:

docker run -d --name apache -p 80:80 httpd

You will get something similar to this in your output:

Output
1b44c8c1070748d39d50567c14c9ab4987002f54395903d7161d136c17864679

Verify Apache Container is Running on Ubuntu 22.04

At this point, you can verify your Apache container is active and running on your server. To do this, type your server’s IP address in your web browser:

http://your-server-ip

If everything is fine, you will see the It Works massage on your web browser.

Verify Apache Docker Container

Tips: If you plan to stop your container, you can use the command below:

docker stop [container-name-or-id]

To remove the container, you can use the command below:

docker remove [container-name-or-id]

Also, you can use the Docker installation guide from the requirements to get more information about Docker commands.

Step 2 – How To Install Apache with Dockerfile on Ubuntu 22.04

Another way that you can set up your Apache container is to use the Dockerfile. If you are interested in this way, you can follow the steps below.

Create an Apache Image Directory

First, you need to create a directory for your Apache and switch to it with the commands below:

# mkdir apache 
# cd apache

At this point, you need to create the files you want to include in the image. For example, we create a default landing page with the vi editor, you can choose your desired one.

vi index.html

Add the following sample code to the file:

<h1>Test</h1>
<p>This is a orcacore.com test page for the Apache deployment in Docker</p>

When you are done, save and close the file.

Build a Dockerfile for Apache Image

Now you need to create the Dockerfile for your Apache image on Ubuntu. To do this, you can run the command below:

vi Dockerfile

Then, you must write the Apache image configuration inside the file on Ubuntu 22.04. Here as an example, we use the latest httpd image as a template. Then, modify the template with index.html from the local directory to the /usr/local/apache2/htdocs directory in the image. And container port 80 is exposed to make it available for mapping to a host port.

FROM httpd:latest
COPY index.html /usr/local/apache2/htdocs
EXPOSE 80

When you are done, save and close the file.

Run Apache Dockerfile as a Container

Now you can build your Dockerfile image name with the command below:

docker build -t apache:v1 .

At this point, you can run Apache as a Dockerfile with the following command:

docker run -d --name [container-name] -p 80:[host-port] [image-name]

Here we use the Apache container as apache2 and the new Docker file:

docker run -d --name apache2 -p 80:80 apache:v1
Output
748b9792fe223efd381cc2f6ddba33986bc271ac9133fc8e5ec6119326d31345

Verify Apache Dockerfile Container is Running

At this point, you can verify your Apache is running as a Dockerfile by typing your server’s IP address in your web browser:

http://your-server-ip

In your web browser, you should see the text file message:

Apache Dockerfile container

That’s it, you are done.

Conclusion

At this point, you have learned to Install and Run Apache in a Docker Container via Docker Hub and Dockerfile on Ubuntu 22.04. Hope you enjoy using it. If you looking for any help, you can comment for us.

You may be interested in these articles:

Install ManageEngine OpManager on Ubuntu 22.04

Powerline For VIM and Bash on Ubuntu 22.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!