Building an Apache Web Server Using Docker

A web server is server software, or hardware dedicated to running this software, that can satisfy client requests on the World Wide Web

Environment Requirements

  1. Install Docker on your computer.
  2. Docker supports Mac, Linux and Windows.
  3. Pull an Ubuntu image from Dockerhub
  4. Here
  5. Install vim and apache2 within a docker container than save the container.
  6. Log in your Ubuntu in Docker container, then install apache2. Finally, save your modification.
  7. Share a directory between Docker and host.
  8. To read and save data in the host, the shared directory is needed for Docker containers.
  9. Use vi to create and edit an index.html file in the shared directory
  10. After logging into your Ubuntu, using vi to create and edit a simple index.html file, then open it using your host browser

Key Steps and Tutorial Resources


  1. Install Docker Engine
  2. Source 1 Source 2

    Key-step: After the installation, you can run “​docker run hello-world​” to test your installation. Below is a screenshot when you have ran this command successfully on Mac OS.

  3. Pull an Ubuntu image from Dockerhub
  4. Source 3

    Key-step 1: ​After logging into Docker, you can run “​docker pull ubuntu”​ in your command line window.

    Key-step 2: ​Use “docker images” to check existing Docker images on your computer.

  5. How To Commit Changes To A Docker Image With Examples
  6. Key-step 1: ​Run the Ubuntu image to start a Docker container, then log into this Ubuntu container:

    Command:

    docker run -it ubuntu:latest /bin/bash


    The container ID is located to the right of the current user in linux (i.e Root@ “800bbded462a”)

    Key-step 2: Install applications in this Ubuntu container.

    Update system using:

    “​apt-get update -y”​

    Install apache2 using:

    “​apt-get install apache2 -y”​

    Install vim text editor using:

    “​apt-get install vim -y​"

    Key-step 3:​Exit Ubuntu container using the command

    exit

    Key-step 4:​ Commit container to create a new Docker image

    1) Check the container using:

    “​docker ps -a​”

    i.e make sure to copy the id of the given ubuntu container


  7. Docker Basics: How to Share Data Between a Docker Container and Host
  8. Source 4

    Using command:

    “docker run -it -p 127.0.0.1:80:80 -v /Users/lighthisels/Documents:/var/www/html ubuntu-my_apache2 /bin/bash”

    Parameter explanation:
    • 1) -it:​ log into an interactive shell
    • 2) -p:​ expose an port of Docker container.
    • 3) -v:​ bind a host directory to Docker container. “/Users/lighthisels/Documents” is a directory of the host computer, and “/var/www/htm” is the default location for the entry webpage of a website. Usually we put “index. Html” is this directory
    .

  9. VI Editor in UNIX
  10. Source 4

    Key step 1: check the present working directory using

    “pwd”

    then go the html directory using:

    “​cd /var/www/html”​

    Key step 2: create an “index.html” file using:

    “​vi index.html​”

    Press “i” key to edit the new “index. Html” file. You can see “-- INSERT --” in the bottom of the command window, which means you can type html source code now. Try the shortcuts of vi to edit your file.


    After finishing your “index.html” file, press the “ESC” key of your computer then type “:wq” to write the file and then quit vi.


    Meanwhile, you can find your “index.html” in your bind directory on your computer.

  11. How to use Docker to host a website.
  12. Key step 1: Start apache service using “​/etc/init.d/apache2 restart​” in your Docker container.



    Using a browser of you host computer to access “127.0.0.1”, then you can see your website!

    Congratulations !