Close modal

Blog Post

Docker I - Serving a simple static site from NGINX

Development
Thu 27 October 2016
0 Comments


The Basics

For this document we'll run through creating a docker image by specifying our Dockerfile and then buolding it. Our starting point is a minimal docker image for Debian Jessie by monsantoco. (Perhaps later we'll cover Alpine, but Debian is definitely more compatible (as it is based on glibc rather than musl), and I find apt easier to work & demonstrate with.

Prerequisite:

Make sure docker is installed, configuration of it is outside the scope of this article but usually easy from APT or YUM sources.

Create your directory and files

First create the basic structure.

[root@syd2 ~]# mkdir mydockerbuild
[root@syd2 ~]# cd mydockerbuild
[root@syd2 mydockerbuild]# touch Dockerfile
[root@syd2 mydockerbuild]# mkdir test_site
vim test_site/index.html

Enter the following sample text if you like:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Hello World</title>
    </head>
    <body>
        <h1>Hello World</h1>
        <p>
            We're a Docker guest..
        </p>
    </body>
</html>

You now have the basics of what you need.

[root@syd2 mydockerbuild]# ls -R
.:
Dockerfile  MY_SITE

./MY_SITE:
index.html
[root@syd2 mydockerbuild]# ls -lRh
.:
total 8.0K
-rw-r--r-- 1 root root  793 Oct 27 02:19 Dockerfile
drwxr-xr-x 2 root root 4.0K Oct 27 02:24 MY_SITE

./MY_SITE:
total 4.0K
-rw-r--r-- 1 root root 242 Oct 27 02:24 index.html

The Docker File

Getting a minimal NGINX container (for static sites)

Here we use dotdeb to get the latest and also greatest version of NGINX easily for apt/deb.

::: dockerfile
FROM monsantoco/min-jessie:latest
RUN apt-get update && \
    apt-get install -y --no-install-recommends wget && \
    echo "deb http://packages.dotdeb.org jessie all" >> /etc/apt/sources.list && \
    echo "deb-src http://packages.dotdeb.org jessie all" >> /etc/apt/sources.list && \
    wget http://www.dotdeb.org/dotdeb.gpg && \
    apt-key add dotdeb.gpg && \
    apt-get update && \
    apt-get install -y --no-install-recommends nginx && \
    rm -rf /var/lib/apt/lists/* && \
    echo "daemon off;" >> /etc/nginx/nginx.conf && \
    apt-get autoclean -y && \
    apt-get autoremove -y && \
    rm -rf /usr/share/locale/* && \
    rm -rf /var/cache/debconf/*-old && \
    rm -rf /var/lib/apt/lists/* && \
    rm -rf /usr/share/doc/*

ADD MY_SITE /var/www/html/
Now we can build the image
[root@syd2 mydockerbuild]# docker build -no-cache -t MY_SERVER .
...
Successfully built 109945347e1d
[root@syd2 mydockerbuild]# docker images
REPOSITORY                              TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
MY_SERVER                           latest              109945347e1d        27 minutes ago      144.3 MB

Running Your Image

Now let's run the image.

[root@syd2 mydockerbuild]# docker run -d -p 80:80 MY_SERVER nginx
65fd3c6f5356eeb3c734781983c367e739b56678c12b6a87c7602200ed9cb409

Verify that it's running successfully.

[root@syd2 mydockerbuild]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                NAMES
65fd3c6f5356        MY_SERVER       "nginx"             4 minutes ago      Up 1 minute       0.0.0.0:80->80/tcp   stupefied_lumiere

If you wish to connect to it, attach a bash as so.

[root@syd2 mydockerbuild]# docker exec -it 65fd3c6f5356eeb3c734781983c367e739b56678c12b6a87c7602200ed9cb409 /bin/bash
root@65fd3c6f5356:/# ps -x
  PID TTY      STAT   TIME COMMAND
    1 ?        Ss     0:00 nginx: master process nginx
   11 ?        Ss     0:00 /bin/bash
   20 ?        R+     0:00 ps -x

So you can see that indeed nginx is running. We haven't done any fancy-pants routing and reverse-proxy with nginx yet but if you connect to the IP address of your docker host (on port 80) you can see that it's serving up the content!.

[root@syd2 mydockerbuild]# lynx localhost:80

Low and behold:

                                                           Hello World
                             Hello World

   We're a Docker guest..













Commands: Use arrow keys to move, '?' for help, 'q' to quit, '<-' to
  Arrow keys: Up and Down to move.  Right to follow a link; Left to go
 H)elp O)ptions P)rint G)o M)ain screen Q)uit /=search [delete]=histo