This tutorial makes following assumptions:
- You are using Ubuntu. It might also work in other Debian based Linux systems, although not guaranteed.
- You have already set-up Docker.
Steps to run Apache inside Docker in following:
- Create a directory called
apache-docker
mkdir apache-docker cd apache-docker
- Create Dockerfile and open in text editor
touch Dockerfile nano Dockerfile
- Use
ubuntu:20.04
as base image. You can also use more recent version of ubuntu image.FROM ubuntu:20.04
- Prevent interactive dialogs like timezone selection
ARG DEBIAN_FRONTEND=noninteractive
- Update package information and perform an upgrade
RUN apt-get update -y && apt-get upgrade -y
- Install Apache
RUN apt-get install -y apache2
- Start Apache server
CMD /usr/sbin/apache2ctl -D FOREGROUND
- Expose port 80 (default port used by Apache server)
EXPOSE 80
- 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
- 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
- Verify the creation of docker image:
docker images
Output:
REPOSITORY TAG IMAGE ID CREATED SIZE apache-docker-image latest e824dca0aa93 2 minutes ago 210MB
- Create and start container using the newly created image
docker run -it --name apache-docker-container -d -p 8080:80 apache-docker-image
- 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
- 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.
- Create a folder called
public
and inside it, createindex.html
with some contentmkdir public echo 'Hello, World!' > public/index.html
- Stop and remove running container
docker stop apache-docker-container docker rm apache-docker-container
- 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
- 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.
- Create
docker-compose.yml
file and open in text editortouch docker-compose.yml nano docker-compose.yml
- 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
- Start docker container:
docker-compose up -d
- To stop running container:
docker-compose down