Type something to search...
How to Run an Apache Web Server Using Docker on an AWS EC2 Instance

How to Run an Apache Web Server Using Docker on an AWS EC2 Instance

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.

Terminal window
sudo yum update -y

Then, we need to install Docker on the AWS EC2 instance.

Terminal window
sudo amazon-linux-extras install docker -y

Step 2: Start the Docker Service

After installing Docker on the AWS EC2 instance, we need to start the Docker service.

Terminal window
sudo service docker start

Step 3: Add the ec2-user to the Docker Group

Try to run the following command to check if the Docker service is running.

Terminal window
docker info

Explination:

  • docker info - This command will show the information about the Docker service.

If you get the following error:

Terminal window
Client:
Context: default
Debug Mode: false
Server:
ERROR: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
errors pretty printing info

Then, you need to add the ec2-user to the Docker group.

Terminal window
sudo usermod -a -G docker ec2-user

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.

Terminal window
docker info

If you get the following output:

Terminal window
Client:
Context: default
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 20.10.17
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
runc version: 1e7bb5b773162b57333d57f612fd72e3f8612d94
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 5.10.144-127.601.amzn2.x86_64
Operating System: Amazon Linux 2
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 964.8MiB
Name: ip-172-31-95-171.ec2.internal
ID: VWQG:27RB:ARKI:V77B:34E2:BRGC:6DDJ:EKSF:VUQP:TX5G:UXVF:Q444
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false

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.

Terminal window
docker pull httpd

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.

Terminal window
docker run -dit --name apache -p 80:80 httpd

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.

Terminal window
curl http://PUBLIC_IP_ADDRESS

Or, we can use the the private IP address of the AWS EC2 instance.

Terminal window
curl http://PRIVATE_IP_ADDRESS

If you get the following output:

Terminal window
<html><body><h1>It works!</h1></body></html>

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.

Terminal window
docker ps

Explination:

  • docker ps - List containers.

Then, we need to open the Docker container.

Terminal window
docker exec -it CONTAINER_ID /bin/bash

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.

Terminal window
vi /usr/local/apache2/htdocs/index.html

If you get the following output:

Terminal window
bash: vi: command not found

Then, you need to install the vi editor.

Terminal window
# update the package list
apt-get update -y
# upgrade the packages
apt-get upgrade -y
# install the vi editor
apt-get install -y vim

Then, we need to open the index.html file, in the /usr/local/apache2/htdocs directory.

Terminal window
vi /usr/local/apache2/htdocs/index.html

Then, we need to add the following content to the index.html file.

1
<html>
2
<body>
3
<h1>Hello World!</h1>
4
</body>
5
</html>

Then, we need to save the index.html file.

Terminal window
:wq

Then, we need to exit the Docker container.

Terminal window
exit

Then, we need to refresh the web page.

Terminal window
curl http://PUBLIC_IP_ADDRESS

If you get the following output:

Terminal window
<html>
<body>
<h1>Hello World!</h1>
</body>
</html>

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.

Terminal window
# To get the Docker container ID
docker ps
# To stop the Apache web server
docker stop CONTAINER_ID
# Or, you can use the container name
docker stop apache

Or, you can stop all the Docker containers using the following command.

Terminal window
docker stop $(docker ps -a -q)

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.

Terminal window
docker start apache

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.

Terminal window
# To get the Docker container ID
docker ps -a
# To remove the Apache web server
docker rm CONTAINER_ID
# Or, you can use the container name
docker rm apache

Or, you can remove all the Docker containers using the following command.

Terminal window
docker rm $(docker ps -a -q)

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.

Terminal window
# To get the Docker image ID
docker images
# To remove the Apache web server
docker rmi IMAGE_ID
# Or, you can use the image name
docker rmi httpd

Or, you can remove all the Docker images using the following command.

Terminal window
docker rmi $(docker images -q)

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.

References

Related Posts

Check out some of our other posts

How To Create A Custom VPC Using AWS CLI

How To Create A Custom VPC Using AWS CLI

Introduction In the sample that follows, an IPv4 CIDR block, a public subnet, and a private subnet are all created using AWS CLI instructions. You can run an instance in the public subnet and con

read more
How to Install and Setup FireWall on Amazon Linux 2

How to Install and Setup FireWall on Amazon Linux 2

Introduction We will learn how to install and setup FireWall on Amazon Linux 2 in this tutorial. We will also discover how to set up FireWall so that it functions with the Amazon Linux 2. Pre

read more
How to Install Apache Web Server on Amazon Linux 2

How to Install Apache Web Server on Amazon Linux 2

Introduction In this tutorial, we will learn how to install Apache web server on Amazon Linux 2. We will also learn how to configure Apache web server to run simple HTML web page. Prerequisit

read more
How to Install PHP and MariaDB on Amazon Linux 2

How to Install PHP and MariaDB on Amazon Linux 2

Introduction We will learn how to set up PHP and MariaDB on Amazon Linux 2 in this tutorial. We will also discover how to set up PHP so that it functions with the Apache web server. We will also

read more
How to Install WordPress on Amazon Linux 2

How to Install WordPress on Amazon Linux 2

Introduction We will learn how to install WordPress on Amazon Linux 2 in this tutorial. We will also discover how to set up WordPress so that it functions with the Apache web server. We will also

read more
How To Create An AWS EC2 Instance Using AWS CLI

How To Create An AWS EC2 Instance Using AWS CLI

Introduction We will learn how to create an AWS EC2 instance using AWS CLI in this tutorial. We will also discover how to set up an AWS EC2 instance so that it functions with the Apache web serve

read more