Introduction
In this project, we will be going over how to install an Apache2 webserver on a Docker container.
The following steps will be taken to achieve this:
- Run a Docker Ubuntu container running detached and on port 80
- Using the BASH shell update all packages within the container
- Using BASH install Apache2 on the Ubuntu container
- Add a custom webpage
- Verify you can reach the web server from your browser.
Pre-Requisites
- An AWS account
- An EC2 instance with the Ubuntu AMI installed
- Remote-SSH in VS Code setup to access the EC2 instance. You can find more information on how to do that here.
Solution
- Launch and Remote SSH into your instance. If you do not know how to launch an instance, check out my article here, which will guide you. You need to select an Ubuntu AMI and allow ALL SSH traffic from only your IP.
- Once you are Remote SSH’d into your instance from VSCode, open a terminal. A shortcut for this is
CTRL+SHIFT+`
3. Create a directory by running the following command:
mkdir ubuntu_dir
.
Change directory into it by running cd ubuntu_dir
4. To Run a Docker Ubuntu container running detached and on port 80, run the commands below
sudo docker pull ubuntu
- The command above will pull the official Ubuntu image from the Docker Hub
sudo docker container run -d -p 80:80 -it -- name my_ubuntu ubuntu
NOTE: sudo allows you to execute commands with superuser privileges. You can choose to include ‘sudo’ before any commands shown in this article or simply switch to the sudo user by running sudo su,
and you can then execute commands without including sudo.
docker run
: This command is used to create and run a container-d
: -d is an option for the docker run command that runs the container in the background and prints the container ID.-p 80:80
: -p is another option for the docker run command that is used to publish a container’s port(s) to the host. In this case, it maps port 80 from the host to port 80 in the container.-it
: This is another option which runs the container in interactive mode with the container’s shell--name my_ubuntu
: -name is another option for the docker run command that enables a custom name to be given to a container. Otherwise, a random container name would be generated.ubuntu
: This is the name of the docker image which is being used for the container. It is the official Ubuntu image.
You can run sudo docker ps -a
to see a list of all containers.
To complete the rest of the project, a BASH shell is needed inside the container. This can be achieved by running the following command:
sudo docker exec -it my_ubuntu bash
4. To use the BASH shell to update all packages within the container, run apt update -y
5. To use the BASH shell to install Apache2 on the Ubuntu container, run apt install apache2 -y
Once the install is done, run service apache2 status
to see the status of Apache2.
If it is not running, run service apache2 start
and check the status once again.
6. To create a custom webpage, the index.html file has to be edited. This file is located in /var/www/html. Use the cd command to change into this directory. Once there, use the vi command to edit the index.html to what is seen below.
<html>
<head>
<style>
body {
background-color: #33342D;
}
h1 { color: white; }
h1 {
text-align:center
}
</style>
</head>
<body>
<h1>Welcome to LUIT - Silver Cohort</h1>
</body>
</html>
If you get an error stating the vi command is not found, you will need to install the vi editor or any other editor you prefer (such as nano). This can be done by running apt-get install vim -y
7. To verify the web server can be reached from a browser, copy the Public IP of your EC2 instance and paste it in a web browser. You should see the following web page
And that’s it for this project.
Happy learning!