Hosting Apache2 in a Ubuntu Container
Hey All,
The purpose of this blog is to demonstrate how to host a web server service inside a container using a docker file. The application can be accessed from the browser by exposing the port.
Let's get into the content and learn.....
Table of Content :
- Prerequisites.
- Setting up the Environment.
- Creating Dockerfile and explanation.
- Build a docker image and run the docker container.
- Pushing the docker image to the repository.
Prerequisites:
- Ubuntu Instance with the inbound rule of exposing port 80 to host the application.
- Docker engine should be installed in the instance.
Setting up the Environment:
- Launch an EC2 instance from AWS console management with the inbound rule exposing port 80 of the host machine.
- After launching the EC2 instance, log into the host machine and follow the below commands to update and install the required services.
Creating the Dockerfile and explanation:
Dockerfile used to containerize the application
Explanation of the Dockerfile
- FROM ubuntu: From command gives the base image to be used in our container. In this, I have used ubuntu for my container.
- RUN apt update: Updates the package list available inside the container.
- ARG DEBIAN_FRONTEND=noninteractive: this skips the interactions while apache2 is getting installed.
- RUN apt install apache2 -y && apt-get clean: Clears the local repository of retrieved package files that are left in /var/cache.
- EXPOSE 80: Exposes port for the application to run.
- CMD ["apachectl", "-D", "FOREGROUND"]: This command line starts the service in the foreground.
Build a docker image and run the container:
- Build the docker image from the Dockerfile using the command
The above command builds the docker image from the Dockerfile and tags it with the name web.
Once the build is over, we can able see the image with all the dependencies being installed
Now run the container from the image using the below command
The above command run the container, -d makes the container to run in detached mode and give the container id. -p 80:80 is used to expose and map the port 80 of the container with the port of the host machine. web - image name.
After the execution, docker will run the container with apache2 service running inside the container. Verify the same by hitting the public IP of the EC2 instance followed by the port number in the browser.
<public IP of EC2 instance>:80
Below is the output of the Apache2 service running inside the container.
Pushing the docker image to the repository:
The docker image that was built can be pushed to the docker hub repository. The same image can be pulled from the docker hub repository for future use.
Login to your docker hub account and go to the repositories section. Click on Create Repository.
Give the repository name, and the docker hub will provide you the desired name that needs to be tagged with our image which will be like <username>/<repo name entered by user>
Make sure to change the tag name with your desired image repository tag.
docker tag local-image:tagname new-repo:tagname
In this case, the command will be
docker tag web <username>/apche2_web:v1
Before pushing the image to the repository, login to the docker hub using the below command
docker login -u <username> -p <password>
Push the image to the repository using the below command,
docker push <username>/apache2_web:v1
Once it got pushed, check the docker hub repository. Now our image is ready to be pulled and used anywhere at anytime.

Comments
Post a Comment