Run Apache in Docker

This tutorial makes following assumptions:

  1. You are using Ubuntu. It might also work in other Debian based Linux systems, although not guaranteed.
  2. You have already set-up Docker.

Steps to run Apache inside Docker in following:

  1. Create a directory called apache-docker
    mkdir apache-docker
    cd apache-docker
    
  2. Create Dockerfile and open in text editor
    touch Dockerfile
    nano Dockerfile
    
  3. Use ubuntu:20.04 as base image. You can also use more recent version of ubuntu image.
    FROM ubuntu:20.04
    
  4. Prevent interactive dialogs like timezone selection
    ARG DEBIAN_FRONTEND=noninteractive
    
  5. Update package information and perform an upgrade
    RUN apt-get update -y && apt-get upgrade -y
    
  6. Install Apache
    RUN apt-get install -y apache2
    
  7. Start Apache server
    CMD /usr/sbin/apache2ctl -D FOREGROUND
    
  8. Expose port 80 (default port used by Apache server)
    EXPOSE 80
    
  9. The Dockerfile should look like this:
    FROM ubuntu:20.04
    ARG DEBIAN_FRONTEND=noninteractive
    RUN apt-get update -y && apt-get upgrade -y
    RUN apt-get install -y apache2
    CMD /usr/sbin/apache2ctl -D FOREGROUND
    EXPOSE 80
    
  10. Build image using the Dockerfile:
    docker build . -t apache-docker-image
    

    Output:

    Sending build context to Docker daemon 2.048kB
    Step 1/6 : FROM ubuntu:20.04
    ---> adafef2e596e
    Step 2/6 : ...
    ...
    Successfully built e824dca0aa93
    Successfully tagged apache-docker-image:latest
    
  11. Verify the creation of docker image:
    docker images
    

    Output:

    REPOSITORY            TAG      IMAGE ID       CREATED         SIZE
    apache-docker-image   latest   e824dca0aa93   2 minutes ago   210MB
    
  12. Create and start container using the newly created image
    docker run -it --name apache-docker-container -d -p 8080:80 apache-docker-image
    
  13. Verify the creation of container
    docker ps
    

    Output:

    CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS         PORTS                  NAMES
    1220c48d2f78   apache-docker-image   "/bin/sh -c 'apache2…"   7 seconds ago   Up 4 seconds   0.0.0.0:8080->80/tcp   apache-docker-container
    
  14. In web browser, open http://localhost:8080. The default page of Apache is displayed.

The next steps describes how to serve files from the host machine instead of default page of Apache.

  1. Create a folder called public and inside it, create index.html with some content
    mkdir public
    echo 'Hello, World!' > public/index.html
    
  2. Stop and remove running container
    docker stop apache-docker-container
    docker rm apache-docker-container
    
  3. Create and start container by passing volume flag
    docker run -it --name apache-docker-container -d -p 8080:80 -v ${PWD}/public/:/var/www/html/ apache-docker-image
    
  4. In web browser, open http://localhost:8080. “Hell, World!” text should be displayed.

The next steps shows how to use docker-composer to easily run docker container.

  1. Create docker-compose.yml file and open in text editor
    touch docker-compose.yml
    nano docker-compose.yml
    
  2. Copy and paste following code into the docker-compose.yml file
    version: '2.0'
    services:
        webapp:
            image: apache-docker
            build:
                context: .
            volumes:
                - ./public/:/var/www/html/
            ports:
                - 8081:80
    
  3. Start docker container:
    docker-compose up -d
    
  4. To stop running container:
    docker-compose down
    

Related Articles:

Tags