Linux container build an Apache HTTP server image and push to DockerHub registry
Test Environment
Fedora 32 installed
Podman installed
Podman is a opensource linux container management tool. We can use podman to search, build, run and push the images.
In this article we will see how we can prepare a docker file and use Podman to build an image from this file. We will try to install a basic Apache HTTP server package and run it as a container to fetch the static content requests. Also we will push this image to DockerHub registry using Podman. Here are the step by step procedure below.
If you are interested in watching video, here is the youtube video with the same procedure.
Procedure –
Step1: Download the Fedora base image
We will create a folder and in that we will prepare the Dockerfile which we will be using to build an impage with Podman. As a first step we will pull the Fedora base image from registry as shown below in the Dockerfile.
Pull Fedora base image |
---|
FROM fedora |
Step2: Install httpd package onto Fedora base image
In this step, we will install the Apache HTTP server package on top of the Fedora base image layer. We will use the below RUN instruction to download and install the httpd package from the Fedora repositories configured in the base image.
Install httpd package |
---|
RUN dnf -y install httpd |
Step3: Copy static file into Document Root
Now that we have the httpd package installed we will a basic hello.html file into the DocumentRoot of the Apache HTTP server. For Apache HTTP server that is installed via the Fedora repository the default DocumentRoot is /var/www/html. We will use the COPY instruction to copy the hello.html from current working directory into the container Apache HTTP server DocumentRoot as shown below.
Copy Static web page |
---|
COPY hello.html /var/www/html/hello.html |
Step4: Configure the command to execute
Currently we have our httpd installation completed and also copied our static web page which we will be trying to accessing using the http url. In order for our web page to be accessible first we need to get the Apache HTTP server instance started using the below CMD instruction and run the process in foregroup.
Configure command to execute |
---|
CMD [ "/usr/sbin/httpd" , "-DFOREGROUND" ] |
Step5: Expose http port 80 for the Apache instance
Right now we are all set with out Apache HTTP server installed and static web deployed to the DocumentRoot. Now we need to EXPOSE the default HTTP port 80 so that we can access the page using external URL.
Expose container HTTP port |
---|
EXPOSE 80 |
Step6: Build the image from Dockerfile
Until now, we have prepared all the instructions that are required to build the Apache HTTP server instance image in a Dockerfile. We can build this Dockerfile using the below podman build command with a tag name to create our image.
Build image |
---|
[root@fed32 DockerExample]# podman build -t hello:1.0 . |
Step7: Run the container using image hello
Now we can instantiate a container using the image name ‘localhost/hello:1.0’ that we created in previous step. Here we will launch our container in detached mode and do port forwarding from host port 80 to container port 80 so that we can access the static web page.
Run container from image |
---|
[root@fed32 DockerExample]# podman run -d -p 80:80 localhost/hello:1.0 |
Step8: Validate the Application
We can validate the static web page hello.html which we have deployed and also the default index.html that comes with Apache HTTP server using the below URL. You can update localhost with your FQDN or hostname as per convinence.
Validate application URL |
---|
URL - http://localhost/hello.html or http://localhost/index.html |
Step9: Rename the image
So, we have created an image and launched a container using the image. Now lets rename the image name such that it include the registry name, DockerHub user name and image name along with version as shown below. This is required to push our image from local registry to DockerHub registry.
Rename image |
---|
[root@fed32 DockerExample]# podman tag localhost/hello:1.0 docker.io/novicejava1/hello:1.0 |
Step10: Login to the registry
We need to authenticate with the Docker registry in order to push any images to DockerHub. So lets authenticate using podman login and provide the credentials as shown below.
Login to registry |
---|
[root@fed32 DockerExample]# podman login docker.io |
Step11: Push the image to Dockerhub registry
Now we are ready to push our hello image to DockerHub using podman push command as shown below. This will push the image to the DockerHub registry under the DockerHub username.
Push image to DockerHub |
---|
[root@fed32 DockerExample]# podman push docker.io/novicejava1/hello:1.0 |
Step12: Validate the image in DockerHub
We can validate whether the image has been pushed successfully or not by checking the DockerHub repositories section
Hope you learned something by reading this article or watching the video. Thank you..
Leave a Reply
You must be logged in to post a comment.