Run Simple Project with Apache in Docker

Last modified on July 28, 2020 1 min read

Today we’re going to learn how to build and run a project with Apache web server in Docker. Let’s start:

You can take a look at basic docker commands.

Table of Contents

  1. Create a Project
  2. Create Dockerfile
  3. Build and Run as Container

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:

  • project
    • app
      • index.html
    • Dockerfile

  • So, the main project’s files will be stored in the app folder.

    Create Dockerfile

    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.

    Dockerfile
    # 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 ./

    Here, ./ defines the Dockerfile’s location.

    Now let’s run the container at our machine’s port 90:

    docker run -p 90:80 apacheserver

    Visit 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.

    The tutorial is over. Thank you. 🙂

    Author

    Hey, I'm Md Obydullah. I build open-source projects and write on Laravel, Linux server, modern JavaScript and more on web development.

    Follow

    Leave a Reply

    Your email address will not be published. Required fields are marked *