~Installing Apache in a Container~

Asonti Ginn
5 min readJul 3, 2023

Today’s Goals

~ Create an Ubuntu container in Docker.

~ Update packages and install apache2.

~ Customize our webpage.

Key Terms

~ Docker: With docker you can build, update, and run containers.

~ Apache: Apache is an open-source web server software.

~ Container: A container is made from an image and it packages up code so it can run quickly in other environments.

~ Ubuntu: Ubuntu is a free Open-Source Linux distribution.

~ Network: This lets containers communicate with host and other containers.

~ HTML: This stands for Hyper Text Markup Language; you can create webpages using this language.

~ Sudo: Allows you to run commands as if you are the root user in a terminal.

Prerequisites

~ Have an environment using an EC2 instance. If you do not have one, check out my article to help set up one. setting-up-docker

~ Basic command line knowledge.

Container Creation

First, we need to create a container that uses an ubuntu image. We want to run this container detached on port 80. Let’s break down the parts of the command.

~ -it: This allows you to interact insides the container.

~ -d: This means that the container will run in the background.

~ -p 80:80: This specifies the port number; in this case it is port 80.

~ ubuntu:20.04: The specific image we are running.

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

Now that we have made our container, we need to list our containers so we can see our ID and that it is up running properly.

sudo docker container ls

You should see that the container is up running connected to the port we specified above.

Installing Apache

Next, we need to enter the container and run a few commands so we can install Apache. We need to start a bash shell inside the container.

~ exec: Allows you to execute a command inside the container.

~ bash: Bash is what we are executing inside the container.

sudo docker container exec -it <ID> bash

As you can see you now are inside the container and can run certain bash commands. Before we install Apache, we want to make sure that the shell is up to date. This command will run any updates that are needed.

apt-get update -y

Now run this command to install Apache2.

apt-get install apache2 -y

While Apache is installing, they will ask for your location and time zone. You can provide the information by typing in the number that corresponds with your correct answer.

Webpage

Once Apache is finished installing, we can now edit and create out webpage. The webpage is located in the /var/www/html directory, so we need to head there.

cd /var/www/html

Inside the directory we want to vim into our index.html file, this file holds our webpage details.

If you try to run the vim command, you will see an error saying that command cannot be found. Like I stated earlier we can only run certain commands. So, we must install vim to run the vim command.

apt-get install vim -y

Once vim has been successfully installed, vim into your index.html file.

vim index.html

When you enter the file you will see a lot of html, type ‘ggdG’, this will clear the entire file.

Now hit ‘i’ so we can begin entering our html for the webpage.

<!DOCTYPE html>
<html>
<head>
<title>TYPE YOU BROWSER PAGE NAME HERE</title>
<h1>TYPE WHAT YOU WANT TO DISPLAY</h1>
</head>
</html>

Once you are finished hit the ‘esc’ button on your keyboard.

Then type :wq to save your work and exit.

Run the following command, this will restart your Apache so your updates will be on your webpage.

apachectl restart

Test your webpage by connecting to your webpage using your IP Address.

Note: To exit your bash simply type “exit”

Congratulations, you have successfully made an Apache webpage inside a container.

Try something COMPLEX!

Let’s try something a little more complicated. We are going to create a new network and run this container we just made in the new network.

First, we need to create the network.

sudo docker network create <NETWORKNAME>

Let’s inspect the container we made above to see what network it is already a part of.

sudo docker inspect <CONTAINERID>

You should see the Networks and the name right below is the network it is connected to; in my case my container is connected to the ‘bridge’ network.

We want to add our container to our newly created network.

sudo docker network connect <NETWORKID> <CONTAINERID>

We need to disconnect from the default network we are still attached to. We need to list the networks so we can retrieve the network ID of the default attached network. Copy the Network ID.

sudo docker network ls

Now disconnect your container from that network and inspect after to be sure you disconnected successfully.

sudo docker network disconnect <PASTENETWORKID> <CONTAINERID>
sudo docker inspect <CONTAINERID>

You should now see that the only network your container is connected to is the one you just made.

You have successfully added your container to a new network.

For more, easy to follow articles, follow me on Medium!

--

--

Asonti Ginn

Hey! I am taking you on my Cloud engineering journey with easy to follow, how-to articles. All provided with an under 10-minute read and PLENTY of pictures!☁️