Apache is one of the most widely used web servers, providing a robust platform for hosting websites and web applications. With the power of Docker, you can easily run Apache in an isolated container, making your setup portable and quick to deploy. In this blog post, we will walk you through the steps to install and run Apache on Ubuntu 22.04 using Docker. This setup is perfect for testing, development, or production environments.
What You'll Learn.
In this guide, you'll learn:
- How to install Docker on Ubuntu 22.04.
- How to pull and run Apache in a Docker container.
- Basic management of your Apache Docker container.
- How to troubleshoot any potential issues during the setup.
Step 1 Install Docker on Ubuntu 22.04
Before we begin setting up Apache, we need to ensure that Docker is installed on your system. Docker allows us to run Apache within a container, providing a lightweight and efficient environment. Follow these steps to get Docker up and running:
1.1 Update Your System
Start by updating your package lists to make sure you’re working with the latest versions of the software.
sudo apt update
sudo apt upgrade -y
1.2 Install Required Dependencies
Next, we’ll install some necessary dependencies to allow Docker to be added to the system.
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
1.3 Add Docker’s Official GPG Key
Now, you need to add Docker’s official GPG key to your system so it can verify the Docker package authenticity.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
1.4 Add Docker’s Repository
Add Docker’s official repository to your system’s list of sources, so you can install the latest version of Docker from it.
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
1.5 Install Docker
Now you can install Docker. After updating your package list, install Docker using the following command:
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
1.6 Verify Docker Installation
To confirm Docker was installed successfully, check the version of Docker installed:
sudo docker --version
Step 2 Run Apache in a Docker Container
Now that Docker is installed, it's time to pull the official Apache image and run it in a container. Docker makes it incredibly easy to run a fully functional Apache web server without the need to configure anything manually.
2.1 Pull the Official Apache Image
Docker Hub has a ready-to-use official Apache image, so you don't have to create one from scratch. Pull the image by running:
sudo docker pull httpd
2.2 Run Apache in a Docker Container
With the Apache image pulled, we can now run it in a container. This will start Apache and bind it to your machine's port 8080:
sudo docker run -d -p 8080:80 --name apache-server httpd
-d: Runs the container in detached mode (in the background). -p 8080:80: Maps port 80 inside the container (the default Apache port) to port 8080 on your host machine. --name apache-server: Names your container "apache-server" for easy reference.
2.3 Verify the Apache Container is Running
To check if your container is running properly, you can use:
sudo docker ps
This command will list all running containers, and you should see your apache-server container in the list.
2.4 Access Apache in Your Browser
Now, open your browser and navigate to http://localhost:8080 or http://
Step 3 Managing Your Apache Docker Container
Once your Apache server is running, you may need to perform some basic container management tasks. Here are some useful commands:
3.1 Stop the Apache Container
If you need to stop the Apache container, you can use the following command:
sudo docker stop apache-server
3.2 Start the Apache Container Again
To start the container again after stopping it:
sudo docker start apache-server
3.3 Remove the Apache Container
If you want to remove the container (for example, when cleaning up), use this command:
sudo docker rm apache-server
3.4 View Apache Logs
To view the logs generated by Apache inside the container:
sudo docker logs apache-server
Step 4 Customize Your Apache Setup
At this point, you have Apache running in Docker with its default configuration. However, Docker allows for greater flexibility, enabling you to configure Apache further. You can mount configuration files or a custom website directory into your container, or even create a custom Dockerfile to suit your specific needs.
4.1 Mounting Local Directories to Docker
If you want to serve your custom website, you can mount your local directory to the Apache container:
sudo docker run -d -p 8080:80 -v /path/to/your/website:/usr/local/apache2/htdocs/ --name apache-server httpd
This command mounts the website directory on your host machine to the htdocs folder in the Apache container, where Apache looks for files to serve.
Troubleshooting Common Issues
Docker Image Pull Failures: Ensure you have a stable internet connection. If you encounter issues while pulling the Apache image, try running docker system prune to clear unused data and try again. Port Conflicts: If port 8080 is already in use, you can change the host port (e.g., -p 8081:80) when running the container. Apache Errors: To check for any issues with Apache itself, use docker logs apache-server to view the container's log output.
Conclusion
Congratulations! You’ve successfully set up Apache on Ubuntu 22.04 using Docker. This method is a clean and efficient way to run a web server, with the added benefits of Docker’s portability and ease of use. You can now scale your Apache setup, customize it, or even deploy it in production with minimal effort. Docker makes managing your server environments a breeze, and using Apache in a containerized environment is a powerful choice for modern web hosting.
If you want to take it a step further, you can integrate Docker Compose to manage multi-container applications or even link Apache with a MySQL or PHP container for dynamic website hosting.
Happy coding, and enjoy your new Apache web server!