Dockerization of Static Websites

From D-Wiki

Dockerization of Static Websites

This is a short guide on how to dockerize any static website you might have laying around.

Prerequisites

  • Docker installed on your computer
  • A website with all of the HTML/CSS/JS/whatever files around.
  • Put the website as well as the following documents in a parent folder.

Preparing our project

Dockerfile

We are going to use nginx to serve our web content. In here we are basically just importing nginx (in this case the version based on Linux Alpine to have a smaller container). Name this file Dockerfile (No extensions.)

FROM nginx:1.15.8-alpine

#nginx config
copy ./nginx.conf /etc/nginx/nginx.conf

#Copy all (html) website files.
copy ./* /usr/share/nginx/html/


nginx Config

This is adding a basic configuration to our nginx server with a basic user. Name this file nginx.conf

user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    server {
        listen 80;
         
         location = /status {
             access_log off;
             default_type text/plain;
             add_header Content-Type text/plain;
             return 200 "alive";
        }
        
         location / {
            gzip off;
            root /usr/share/nginx/html/;
            index  index.html;
        }
        
        location ~* \.(js|jpg|png|css)$ {
            root /usr/share/nginx/html/;
        }
    } 
    sendfile        on;
    keepalive_timeout  65;
}

Building the Docker image

Open up the console in our parent folder and execute the command:

docker build . -t {YOUR_CONTAINER_NAME}

Running our Docker container

Inside the same console execute the following command:

docker run -it --rm -p {YOUR_PORT}:80 {YOUR_CONTAINER_NAME}

After that you can go to your Web-Browser under localhost:{YOUR_PORT} and your dockerized website should be present.