Install Apache in Docker Container on Ubuntu 22.04 with 2 Easy Methods
This guide will walk you through the process of installing Apache in a Docker container on Ubuntu 22.04 using two straightforward methods: pulling the Apache image from Docker Hub and building a custom image using a Dockerfile. Deploying applications within dedicated Docker containers offers a streamlined setup and configuration process. You can follow this comprehensive guide to Install Apache in Docker Container on Ubuntu 22.04.
Before you begin to Install Apache in Docker Container on Ubuntu 22.04, ensure you meet the following prerequisites:
Requirements for Running Apache in a Docker Container
- Sudo Privileges: You need access to your Ubuntu 22.04 server as a non-root user with
sudo
privileges. If you haven’t already, refer to a guide on initial server setup for Ubuntu 22.04. - Docker Installation: Docker must be installed and running on your server. Refer to a guide to Install Apache in Docker Container on Ubuntu 22.04 to properly install and configure Docker on Ubuntu 22.04.
Once you’ve fulfilled these requirements, proceed with either of the methods outlined below to Install Apache in Docker Container on Ubuntu 22.04.
- Method 1: Install Apache from Docker Hub
- Method 2: Install Apache using a Dockerfile
Method 1. Install Apache from Docker Hub on Ubuntu 22.04
Docker Hub hosts a vast repository of pre-built images, including an official Apache image named httpd
. This method involves pulling the httpd
image from Docker Hub and then running it as a container.
To download the Apache image on Ubuntu 22.04, execute the following command:
docker pull httpd
Upon successful completion of the download, you’ll observe output similar to this:
**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?
With the Apache image downloaded, the next step is to create a container from it and start it.
Utilize the docker run
command along with the --name
flag to create and run your container:
docker run -d --name [<container-name>] -p 80:[host-port] httpd
Specifically, to run and Install Apache in Docker Container on Ubuntu 22.04, you can use the following command:
docker run -d --name apache -p 80:80 httpd
The output will resemble the following:
**Output**
1b44c8c1070748d39d50567c14c9ab4987002f54395903d7161d136c17864679
Verify Apache Container is Running on Ubuntu 22.04
To confirm that your Apache container is up and running, simply enter your server’s IP address into a web browser:
http://<your-server-ip>
If everything is configured correctly, you should see the default "It Works!" Apache welcome page.

Tips: To stop your container, use:
docker stop [<container-name-or-id>]
To remove the container, use:
docker rm [<container-name-or-id>]
Consult the Docker installation guide (mentioned in the requirements) for more detailed information on Docker commands.
Method 2. How To Install Apache with Dockerfile on Ubuntu 22.04
The second approach involves constructing a custom Apache image using a Dockerfile. This method grants you greater control over the image’s configuration.
Create an Apache Image Directory
Begin by creating a dedicated directory for your Apache image and navigating into it:
# mkdir apache
# cd apache
Next, create the files you wish to include in your image. For example, let’s create a simple default landing page using the vi
editor:
vi index.html
Add the following HTML code to the index.html
file:
<h1>Test</h1>
<p>This is a orcacore.com test page for the Apache deployment in Docker</p>
Save and close the file when you’re finished.
Build a Dockerfile for Apache Image
Now, create the Dockerfile that will define your Apache image:
vi Dockerfile
Within the Dockerfile, specify the configuration for your Apache image. In this example, we’ll use the latest httpd
image as a base and then customize it by copying our index.html
file into the /usr/local/apache2/htdocs
directory within the image. We also expose port 80 to allow mapping to a host port.
FROM httpd:latest
COPY index.html /usr/local/apache2/htdocs
EXPOSE 80
Save and close the file.
Run Apache Dockerfile as a Container
Build your Dockerfile into an image using the following command:
docker build -t apache:v1 .
With the image built, you can now run Apache as a container from the Dockerfile:
docker run -d --name [container-name] -p 80:[host-port] [image-name]
For instance, to create a container named apache2
using the apache:v1
image, execute:
docker run -d --name apache2 -p 80:80 apache:v1
**Output**
748b9792fe223efd381cc2f6ddba33986bc271ac9133fc8e5ec6119326d31345
Verify Apache Dockerfile Container is Running
To verify that your Apache container, built from the Dockerfile, is running correctly, access your server’s IP address in a web browser:
http://<your-server-ip>
You should see the content of your index.html
file displayed in the browser.

That concludes the process.
Conclusion
You have now successfully learned how to Install Apache in Docker Container on Ubuntu 22.04 using both Docker Hub and a Dockerfile. We hope you found this guide helpful. If you have any questions or require assistance, please feel free to leave a comment.
You may also be interested in these related articles:
- Install ManageEngine OpManager on Ubuntu 22.04
- Powerline For VIM and Bash on Ubuntu 22.04
Alternative Solutions for Running Apache in Docker
While the two methods outlined above are common and effective, here are two alternative approaches to running Apache in a Docker container:
1. Using Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define your application’s services, networks, and volumes in a single docker-compose.yml
file. This approach simplifies the management of more complex deployments, especially when your Apache server interacts with other services like databases or load balancers.
Explanation:
Instead of running individual docker run
commands, you define your Apache service in a docker-compose.yml
file. This file specifies the image to use, port mappings, volume mounts (for persistent storage), and any other configurations. Docker Compose then handles the creation and management of the container based on this configuration.
Example docker-compose.yml
:
version: "3.9"
services:
apache:
image: httpd:latest
ports:
- "80:80"
volumes:
- ./html:/usr/local/apache2/htdocs/
restart: always
In this example:
version
: Specifies the Docker Compose file version.services
: Defines the services that make up your application.apache
: The name of the Apache service.image
: The Docker image to use (in this case, the officialhttpd
image).ports
: Maps port 80 on the host to port 80 in the container.volumes
: Mounts the./html
directory on the host to the/usr/local/apache2/htdocs/
directory in the container, allowing you to easily update the website content.restart: always
: Ensures that the container restarts automatically if it crashes.
To use this, create a directory named html
in the same directory as your docker-compose.yml
file and place your index.html
file inside it.
To start the Apache container using Docker Compose, navigate to the directory containing the docker-compose.yml
file and run:
docker-compose up -d
This command builds and starts the Apache container in detached mode (-d). To stop the container, use:
docker-compose down
2. Using a Custom Base Image with a Package Manager
Instead of directly copying files, you can create a custom base image derived from a minimal Ubuntu image and use a package manager like apt
to install Apache and configure it. This approach provides more control over the underlying operating system and allows you to install additional dependencies if needed.
Explanation:
This method involves creating a Dockerfile that starts with a base Ubuntu image, updates the package list, installs Apache using apt
, and then configures Apache as needed. This allows you to customize the Apache installation process and include any specific configurations or modules.
Example Dockerfile:
FROM ubuntu:latest
# Update package list and install Apache
RUN apt-get update && apt-get install -y apache2
# Copy custom configuration file (optional)
# COPY apache2.conf /etc/apache2/apache2.conf
# Enable necessary modules (optional)
# RUN a2enmod rewrite
# Set the document root
RUN echo "DocumentRoot /var/www/html" >> /etc/apache2/apache2.conf
# Copy your website files
COPY html /var/www/html
# Expose port 80
EXPOSE 80
# Start Apache when the container starts
CMD ["apache2ctl", "-D", "FOREGROUND"]
In this example:
FROM ubuntu:latest
: Starts with the latest Ubuntu image.RUN apt-get update && apt-get install -y apache2
: Updates the package list and installs Apache.COPY html /var/www/html
: Copies your website files from thehtml
directory to the default Apache document root.EXPOSE 80
: Exposes port 80.CMD ["apache2ctl", "-D", "FOREGROUND"]
: Starts Apache in the foreground when the container starts.
To use this, create a directory named html
and place your website files inside it. Then, build the image:
docker build -t custom-apache .
Finally, run the container:
docker run -d -p 80:80 custom-apache
These alternative methods offer different levels of flexibility and control for running Apache in Docker. Docker Compose simplifies multi-container deployments, while using a custom base image with a package manager allows for greater customization of the Apache installation process. Choose the method that best suits your specific needs and requirements to Install Apache in Docker Container on Ubuntu 22.04.