Introduction
We often need a simple web server as a Docker container to serve up static content (HTML or an Angular app etc) or a minimal php web app. So in this post I’ll take you through the process of creating a minimal Docker container using lighttpd, then the same thing using nginx and finally php server. In each case, for demonstration purposes, we’ll just serve up a single html file.
lighttpd
$ mkdir lighttpd
$ cd lighttpd
$ echo "hello world from lighttpd" > index.html
$ more Dockerfile
FROM alpine
RUN apk update \
&& apk add lighttpd \
&& rm -rf /var/cache/apk/*
COPY ./index.html /var/www/localhost/htdocs
CMD ["lighttpd","-D","-f","/etc/lighttpd/lighttpd.conf"]
$ docker build . -t lighttpd
$ docker run -d --name lighttpd -p 8080:80 lighttpd
nginx
$ mkdir nginx
$ cd nginx
$ echo "hello world from nginx" > index.html
$ more Dockerfile
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
RUN mkdir -p /www
COPY ./index.html /www
$ more nginx.conf
events {
worker_connections 1024;
}
http {
server {
root /www/;
}
}
$ docker build . -t nginx
$ docker run -d --name nginx -p 8081:80 nginx
php (apache)
$ mkdir php
$ cd php
$ mkdir src
$ echo "hello world from <?php echo 'php'; ?>" > src/index.php
$ more Dockerfile
FROM php:7.2-apache
COPY src/ /var/www/html/
$ docker build . -t php-app
$ docker run -d --name php-app -p 8082:80 php-app