Today we’re going to learn how to build and run a project with Apache web server in Docker. Let’s start:
Table of Contents
Create a Project
First of all, let’s create a simple project. I’m going to make a simple project. The project will contain only the index.html file. Your project may contain more files.
My docker project’s structure:
So, the main project’s files will be stored in the app folder.
Outside of the app folder, we need to create a file called Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. To know more about Dockerfile, please Dockerfile reference.
We’re going to write all commands in Dockerfile to create a Docker image for our project.
# pull latest CentoOS FROM centos:latest # the maintainer name MAINTAINER Obydul # install apache RUN dnf -y install httpd # copy our app's files to apache's public dorectory COPY ./app/ /var/www/html/ # keep apache server running CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"] # listen the port 80 for apache on CentOS EXPOSE 80
Build and Run as Container
We’ve created our project and wrote a Dockerfile to create an image. Let’s build the project:
docker build -t apacheserver ./
./ defines the Dockerfile’s location.
Now let’s run the container at our machine’s port 90:
docker run -p 90:80 apacheserver
http://localhost:90 and see the output. If you are testing on live server, you need to replace localhost with the server IP or hostname.
You're welcome to suggest any article to write!