This project guides you through setting up Docker and launching an Apache web server on an Amazon Web Services (AWS) EC2 instance. By the end of this guide, you will have a functional Apache web server running inside a Docker container.
- AWS account with access to EC2 instances.
- SSH key pair for secure access to EC2 instance.
- Basic knowledge of AWS EC2 and Docker.
Log in to the AWS Management Console using your AWS credentials.
Create an EC2 instance with your desired specifications, e.g., Amazon Linux AMI, t2.micro. Be sure to associate your SSH key pair with the instance during launch.
Use the private key of your SSH key pair to securely connect to the EC2 instance.
ssh -i <path-to-private-key> ec2-user@<instance-public-ip>
Update the package manager:
sudo yum update -y
Install Docker for Linux Platform and screenshot output
sudo yum install docker -y
Start the Docker service:
sudo service docker start
Enable Docker to start on boot:
sudo chkconfig docker on
Check the status of the Docker service:
sudo service docker status
Get information about Docker:
docker info
Pull a Docker image from a registry, for example:
docker pull public.ecr.aws/docker/library/centos:latest
Navigate to the directory where you want to create the Dockerfile, e.g., /opt/docker/.
cd /opt
mkdir docker
cd docker
Create a Dockerfile using a text editor (e.g., vi) and add the following content: (screenshot editor with code pasted)
make sure Dockerfile has an uppercase D
FROM public.ecr.aws/docker/library/centos:latest
MAINTAINER QuantumCode
RUN cd /etc/yum.repos.d/
RUN sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
RUN sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
RUN yum -y update
RUN yum -y install httpd
COPY index.html /var/www/html/
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
EXPOSE 80
save and exit the editor
Create the index.html file using a text editor (e.g., vi) and paste the content index.html file:
Build the Docker image with the Dockerfile in the current directory (.) and tag it as "quantum_apache_image":
docker build -t quantum_apache_image .
List the Docker images to verify that the "quantum_apache_image" has been successfully built:
docker images
Run the Docker container in detached mode (-d) and map port 80 from the container to port 80 on the host:
docker run -itd -p 80:80 quantum_apache_image
List the running Docker containers to verify that the "quantum_apache_image" container is running:
docker ps
To check if the Apache server is running and serving the content, open a web browser and enter the IP address of the EC2 instance.
You should be able to see the content you pasted in the index.html file displayed in the browser.
Congratulations! You have successfully set up an Apache web server inside a Docker container on an AWS EC2 instance...