A Dockerfile is a script that having the command and while deployment the docker image container We will get up and running service which configured with commands.
- Basic knowledge of Docker.
- Basic knowledge of Linux commands.
Step 1: Create a Apache Directory
To begin, we set up a directory just for Apache-related files by employing the mkdir command.
Step 2: Create a Dockerfile
Now that we have created a folder, we can use the nano editor to create a Dockerfile within that folder:
Paste the following commands.
FROM ubuntu:latest RUN apt-get update && apt-get upgrade -y RUN apt-get install -y apache2 EXPOSE 80 CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
Save and exit from the nano text editor.
This Dockerfile uses the latest version of Ubuntu as the base image. It then runs apt-get update and upgrade to make sure all packages are up to date. It then installs Apache2 using apt-get install. The EXPOSE command tells Docker that the container will listen on port 80. The CMD command starts the Apache2 service in the foreground.
Step 3: Building Apache Docker Image
The docker build command is now used to construct the Dockerfile. within which we give our image a custom name (such as apache_image) and tag it with the 1.0.0 suffix.
docker build -t apache_image:1.0.0 .
The docker images command should be used to verify the image’s existence after it has been constructed.
We can see a list of all the images that have been created or obtained from any public or private registry using the docker images command.
Step 4: Deploy Apache Container
Run the image locally as a container after it has been built:
We use detached mode to run the container continuously in the background. In the docker run command, include -d.
We provide port 80 (HTTP) for the Apache server in order to host it. To have the server run on localhost, use -p 80:80.
As a result, the docker run command also uses the image and the tag that goes with it as input to run the container.
docker run --name custom_apache -d -p 80:80 apache_image:1.0.0
Step 5: Testing Apache Test Page
Go to any local browser and type localhost to see if the Apache server is present.
We have successfully build apache custom image from scratch dockerfile on ubuntu 22.04 LTS, If you still have questions, please post them in the comments section below.