Introduction
The Apache HTTP Server, commonly known as Apache, is a widely used open-source web server software developed and maintained by the Apache Software Foundation. It plays a crucial role in serving web content over the internet. Apache is known for its stability, flexibility, and performance, making it one of the most popular choices for hosting websites.
Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define an entire application stack, including services, networks, and volumes, in a single file called docker-compose.yml. With a single command, you can then spin up your entire application stack, simplifying the process of managing and deploying multi-container Docker applications.
Prerequisites
- Up and running ubuntu 22.04 LTS machine.
- Basic knowledge in linux commands.
- Internet connectivity.
- Docker and docker compose should installed on your machine.
To install Docker and docker compose on ubuntu 22.04 LTS, We can use the given link.
In this post, We will deploy Apache web server container using Docker Compose on Ubuntu 22.04 LTS machine.
Step 1: Creating docker-compose.yml
To deploy a nginx container using Docker Compose, you’ll need to create a docker-compose.yml file that defines the service. Here’s a simple example:
To create a docker-compose.yml
sudo nano docker-compose.yml
Copy and paste the followings configuration.
version: '3'
services:
apache:
image: httpd:latest # Use the latest official Apache image from Docker Hub
container_name: my-apache-container
ports:
- "80:80" # Map host port 80 to container port 80
volumes:
- ./html:/usr/local/apache2/htdocs # Mount a local directory as the document root
In this example:
- We use the official httpd image from Docker Hub, which contains the Apache web server.
- We name the service apache and give the container a name (my-apache-container).
- We map port 80 on the host to port 80 on the container.
- We mount a local directory
(./html)
as the document root inside the container.
Create a directory named html in your project directory. This directory will contain your web content.
mkdir html
Inside the html directory, create an index.html
file with some content:
<!DOCTYPE html>
<html>
<head>
<title>My Apache Container</title>
</head>
<body>
<h1>Hello, Apache!</h1>
</body>
</html>
Save the index.html
file.
Step 2: Deploy Apache Container
To deploy this apache service, follow these steps, Run the following command to start the apache container.
sudo docker-compose up -d
This command provides a summary of the status of each service defined in your docker-compose.yml
file. It shows information such as the service name, container ID, status, ports, and names.
Here’s an example of what the output might look like:
Name Command State Ports
-------------------------------------------------------------------------
apache docker-entrypoint.sh apache Up 0.0.0.0:80->80/tcp
Step 4: Logging
Some time we need to also check the real time logs, So we can use the given commands.
sudo docker-compose logs -f apache
Step 5: Destroy Apache Container
To destroy (stop and remove) the apache container launched with Docker Compose, you can use the following command in the directory where your docker-compose.yml
file is located:
sudo docker-compose down
This command stops and removes all the containers, networks, and volumes defined in your docker-compose.yml file.
The down command stops and removes the containers but retains the data volumes by default. If you want to remove the volumes as well, you can use the -v
option:
sudo docker-compose down -v
Make sure you are in the correct directory containing your docker-compose.yml
file when running these commands. This ensures that Docker Compose identifies the correct configuration file.
After running the docker-compose down command, you can use the docker-compose ps
command to verify that the containers are no longer running. The output should be empty, indicating that no containers are currently running.
Remember that this command will stop and remove all the services defined in your docker-compose.yml
file, not just the apache service. If you only want to remove a specific service, you can specify the service name:
docker-compose down -v <service_name>
Replace <service_name>
with the actual name of the service you want to remove.
Conclusion
We have successfully deployed Apache web server on docker’s container using docker compose on ubuntu 22.04 LTS machine, If you still have questions, please post them in the comments section below.