Create a Docker File Which Installs Apache2 Automatically After Running the Container— Docker Assignment 4

Visal Tyagi
DevOps-Guides
Published in
5 min readApr 27, 2024

You have been asked to:

A. Create a Docker file with the following specs:

● Ubuntu container

● Apache2 installed

● Apache2 should automatically run once the container starts

B. Submit the Docker file, for assignment completion.

Before Checking this Assignment Solution, kindly check these assignment solutions before:

Create a Container on Ubuntu & Install Apache2 on It — Docker Assignment 1

Launch a Container Using a New Image & Start Apache 2 Sevice Here — Docker Assignment 2

Pull the Image from Docker Hub & Install Apache2 on a Separate Machine — Docker Assignment 3

Git Hub Repository Link for Copying the Commands

Dockerfile
Dockerfile

Problem (A) Solution: Create a Docker File Using the Given Specs

● Ubuntu container

● Apache2 installed

● Apache2 should automatically run once the container starts

A. First, Launch the New EC2 Instance as Assignment 4

Step 1: Go to the “Services” section & put the cursor over “EC2”. Click on “Instances”.

Search EC2
Search EC2

Step 2: Click on “Launch Instance”.

Launch instance
Launch instance

Step 3: Choose “Name” as “Assignment 4” in the “Name and tags” section.

Assignment 4 Instance
Assignment 4 Instance

Step 4: Choose “AMI” as “Ubuntu”.

Ubuntu AMI

Step 5: Choose “Instance type” as “t2.micro” & key pair(login) as “Docker”.

Instance type & keypair
Instance type & keypair

Step 6: Choose “Common security groups” as “launch-wizard-9”.

Choose Security Group
Choose Security Group

Step 7: Click on “Launch Instance”.

Launch Instance
Launch Instance

Step 8: Click on “hyperlink (i-041c3e5b59ce92d00)”.

Click Hyperlink
Click Hyperlink

Step 9: The instance will be in the “Running State”. Select the Instance & click on “Connect”.

Connect Instance
Connect Instance

Step 10: Again, click on “Connect”.

Connect Again
Connect Again

Step 11: Update the machine using this command:

sudo apt-get update
Update the machine
Update the machine

Step 12: Install the docker using this command:

sudo apt-get install docker.io -y
Install Docker
Install Docker

Step 13: Login as a root user to access “Docker” using this command:

sudo su -
Root Access
Root Access

Step 14: Check the “Docker” status using this command:

systemctl status docker
Docker Status
Docker Status

Docker is in an “Active” state.

B. Create a Docker File

Step 1: Create a docker file using the command:

vim Dockerfile
Docker File
Docker File

Step 2: Write the following commands in the Dockerfile to create the container & automatically start Apache2.

FROM ubuntu
RUN apt-get update
RUN apt-get install apache2 -y
RUN apt-get install apache2-utils -y
RUN apt-get clean
ENTRYPOINT apache2ctl -D FOREGROUND

Do :wq! To save and quit the file. Your Dockerfile will be saved with these instructions.

Dockerfile
Dockerfile

Step 3: Now, we will create a new image using this Docker file using this command: “docker build –t assignment4 .” We use . because the docker file is present in the same directory.

sudo docker build -t assignment4 .
Image Created
Image Created
Image Created Successfully
Image Created Successfully

Step 4: When we run the “docker images” command. It will show the created image as “assignment4”.

docker images
Images Created
Images Created

Step 5: Now, we will create a container using the command:

docker container run –itd –p 80:80 - name testcont assignment4
Ubuntu Container Created
Ubuntu Container Created

The container named “ubuntu” was created successfully. You can check the container creation details using the command:

docker container ls -a
Container Created
Container Created

Step 6: Go to Instance. Select the Instance & click on “open address” in “Public IPv4 address”.

Click IP Address
Click IP Address

Step 7: Put :80 behind the IP Address & you will get the by default Apache2 Web Page.

Web Page
Web Page

B. Submit the Dockerfile for assignment completion

Dockerfile Content

FROM ubuntu
RUN apt-get update
RUN apt-get install apache2 -y
RUN apt-get install apache2-utils -y
RUN apt-get clean
ENTRYPOINT apache2ctl -D FOREGROUND

Read these Docker Guides

Containerized an HTML Website using Docker on Production Environment — Docker Case Study

Replace the Apache Default Web Page With Sample HTML File Inside Container — Docker Assignment 5

--

--