How to Run an Apache Web Server Using Docker on an AWS EC2 Instance
- Mohammad Abu Mattar
- Cloud Computing
- 31 Oct, 2022
- 04 Mins read
Introduction
In this post, we will learn how to run an Apache web server using Docker on an AWS EC2 instance. We will use the following tools:
Prerequisites
To follow this tutorial, you will need:
- An AWS account
- An AWS EC2 instance
Note: You can follow this tutorial to create an AWS EC2 instance Using AWS CLI wthout using user data, or you can create an AWS EC2 instance using the AWS console.
Setup and Configure Docker to Run an Apache Web Server on an AWS EC2 Instance
Step 1: Install Docker on the AWS EC2 Instance
Before we can install Docker on the AWS EC2 instance, we need to update the package list.
Then, we need to install Docker on the AWS EC2 instance.
Step 2: Start the Docker Service
After installing Docker on the AWS EC2 instance, we need to start the Docker service.
Step 3: Add the ec2-user
to the Docker Group
Try to run the following command to check if the Docker service is running.
Explination:
docker info
- This command will show the information about the Docker service.
If you get the following error:
Then, you need to add the ec2-user
to the Docker group.
Exit the SSH session and reconnect to the AWS EC2 instance.
After reconnecting to the AWS EC2 instance, try to run the following command again.
If you get the following output:
Then, you have successfully installed Docker on the AWS EC2 instance.
Step 4: Run the Apache Web Server Using Docker
First, we need to pull the Apache image from Docker Hub.
Explination:
docker pull
- Pull an image or a repository from a registry like Docker Hub, or from a private registry like AWS ECR.
Then, we need to run the Apache web server using Docker.
Explination:
docker run
- Run a command in a new container.-dit
- Run container in background and print container ID.--name
- Assign a name to the container.-p
- Publish a container’s port(s) to the host.httpd
- The image name.
Step 5: Test the Apache Web Server
To test the Apache web server, we need to get the public IP address of the AWS EC2 instance.
Or, we can use the the private IP address of the AWS EC2 instance.
If you get the following output:
Then, the Apache web server is running successfully.
Step 6: Edit The Web Page on Docker Container
To edit the web page, we need to open the index.html
file, in the /usr/local/apache2/htdocs
directory.
Before we can edit the index.html
file, we need to get the container ID of the Apache container.
Explination:
docker ps
- List containers.
Then, we need to open the Docker container.
Explination:
docker exec
- Run a command in a running container.-it
- Keep STDIN open even if not attached.CONTAINER_ID
- The container ID./bin/bash
- The command to run.
Then, we need to open the index.html
file, in the /usr/local/apache2/htdocs
directory.
If you get the following output:
Then, you need to install the vi
editor.
Then, we need to open the index.html
file, in the /usr/local/apache2/htdocs
directory.
Then, we need to add the following content to the index.html
file.
Then, we need to save the index.html
file.
Then, we need to exit the Docker container.
Then, we need to refresh the web page.
If you get the following output:
Then, the web page has been updated successfully.
Step 7: Stop the Docker Container
To stop the Docker container, we need to get the container ID of the Apache container.
Or, you can stop all the Docker containers using the following command.
Explination:
docker ps
- List containers.docker stop
- Stop one or more running containers.
Step 8: Restart the Docker Container
To restart the Docker container by using the container name, we need to run the following command.
Explination:
docker start
- Start one or more stopped containers.
Step 9: Remove the Docker Container
To remove the Docker container, we need to get the container ID of the Apache container.
Or, you can remove all the Docker containers using the following command.
Explination:
docker ps
- List containers.docker rm
- Remove one or more containers.
Step 10: Remove the Docker Image
To remove the Docker image, we need to get the image ID of the Apache image.
Or, you can remove all the Docker images using the following command.
Explination:
docker images
- List images.docker rmi
- Remove one or more images.
Conclusion
In this post, we learned how to run an Apache web server using Docker on an AWS EC2 instance. We also learned how to update and install packages on Docker containers.
NOTE: If you have any questions, please leave a comment below.