Docker: Building an Apache2 Web Server on Ubuntu

Hafsah Robleh
Women in Technology
4 min readJul 22, 2023

Objectives

1. To Run a Docker Ubuntu container running detached and on port 80

2. Then use BASH shell to update all packages within the container.

3. Additionally use BASH to install Apache2 on the Ubuntu container.

4. Add a custom webpage.

5. Lastly to verify that we can reach the web server from our browser.

Overview

In this project documentation, I will go through how I went about creating a Ubuntu docker container in which I updated all packages, As well as installed Apache2. I also created a custom webpage and verified that I could access it.

Setting up

Before beginning we need to prep our docker environment, by ensuring we have docker installed. There are countless ways to install Docker as well as many different IDEs to choose from, and there's no right or wrong way. Personally, I am running docker on an ec2 instance as well as using Visual Studio Code.

If you’re interested in exactly how I created my Docker environment, Let me know!

Running the Ubuntu container

I started off in the VS Code terminal and ran this command to run a Docker Ubuntu container (the 20.04 version) running detached and on port 80,

sudo docker container run -it -d -p 80:80 ubuntu:20.04

and I got the container ID, success!

Note: During my first attempt, I kept on getting an error when trying to run my container. After a lot of troubleshooting, I realized that I did not specify the version and the -it option, which allows us to interact inside the container. I ended up deleting my container and recreating it!

Additional note: it's also really important to use the -p (publish) command! I originally did it without it and I again had to re-create the whole container.

Updating all packages

To begin, we need to use the command execto allow us to execute a command in a container, as well as the -itcommand. This command allows us to interact inside the container. I will also be using the ID of the container.

The cool thing about referencing your docker containers is that you never have to list the whole ID, Which can be very lengthy, only the first few, enough to be unique!

 sudo docker container exec -it 4d9 bash

and then I ran the code apt-get update -y

All updated!

Installing Apache2 on the Ubuntu container

Within these same root privileges, that we get from the last command, I then ran this command to install the Apache2 web server.

apt-get install apache2

I was then prompted to choose my country and time zones.

All Done.

Adding a custom webpage

To create a custom webpage, I will cd (change my directory), into var/www/html

cd /var/www/html

After successfully changing the directory, I then created a custom message.

Verifying the webpage can be reached

I first verified that the webserver was running, using this command

service apache2 status

and it returned a statement that apache2 was not in fact running. So I used the command to restart the webserver and tried again.

It's running!

Now for the moment of truth, I got the public IP address of my EC2 Instance and put it into the web browser,

Thank you so much for reading through my article, I really appreciate it! If you have any questions or comments feel free to comment them below. I post weekly DevOps project documentation so follow me for more!

I also have a LinkedIn where I post my projects and other DevOps-related things. Connect with me! https://www.linkedin.com/in/hafsahrobleh/

--

--