Apache is an opensource web server that is commonly used for webpage deployments. Apache does support almost all operating systems worldwide. Docker is an opensource virtualization tool in which users can create, run, and deploy applications or software in packages called containers. The containers can communicate with each other through proper channels

This blog will show the steps to create an Apache Docker image and deploy it. If you face any issues or got stuck with the process, kindly contact our experienced team to resolve it.

Run and Deploy Apache Docker Images in Centos 7

Install Docker on Centos 7

yum install docker docker-distribution
systemctl start docker docker-distribution

Create Dockerfile for apache and open it

mkdir -p dockerapache
cd dockerapache/
vim Dockerfile

Add the below content to the Docker File and Save the File

###########
FROM centos:centos7
RUN yum install httpd -y
RUN mkdir -p /var/www/html
WORKDIR /var/www/html
RUN chown -R apache:apache /var/www/html/
COPY $PWD/index.html /var/www/html/
COPY $PWD/httpd.conf  /etc/httpd/conf.d/httpd.conf
EXPOSE 80
CMD ["/usr/sbin/httpd","-D","FOREGROUND"]
###########

Whereas:
FROM –> Create a layer from the alpine:latest image
RUN –> It is used to execute the command within the image
MAINTAINER –> Information about who is maintaining the Dockerfile
WORKDIR –> It is the working directory of the docker image
ENV –> To set environment variable inside the docker image
COPY –> It is used to copy the files from the host (on which we create the Dockerfile) to the docker image.
ADD –> It is same as the COPY command but there is two difference from COPY command;
1. ADD instruction can copy and extract tar files from the docker host( Dockerfile creating host) to the docker image.
2. ADD instruction can download files via HTTP and copy it into the docker image.
  CMD –> Command specifies the command line command to be executed when a docker container is started up based on a docker image created from the Dockerfile.

Create an index.html with test Content

vim index.html
###########
<h1> This is my Apache Docker File </h1>
###########

Create an Apache default conf as httpd.conf

vim httpd.conf
###########
<VirtualHost *:80>
    DocumentRoot "/var/www/html/"
    ServerName example.com
    ServerAlias www.example.com
    ErrorLog "/var/log/httpd/error_log"
    CustomLog "/var/log/httpd/access_log" combined
</VirtualHost>
###########

Build the docker image

docker build -t apache:centos .

To check docker images

docker images

Run the container from the docker image

docker run -d  -p 80:80 apache:centos

Whereas:
apache= image name:tag (you can also use image id)
p = 80:80 ( binds port 80 of the running host to the TCP port 80 (apache) on the container.
d = Run container in the background and print the container ID

To check running docker containers

docker ps

Test the apache image using

http://SERVER-IP/ OR http://example.com

Then it will give the content same as in index.html page, it is because we copied the index.html to the apache docker container using the Dockerfile.

Are you facing any issues with the Docker setup?

CONTACT US NOW