Friday, January 15, 2016

Docker: nginx up and running

nginx

nginx ("Engine-X") is an open source reverse proxy server for multiple protocols, as well as a load balancer, cache and web server.
What is a reverse proxy server?
A reverse proxy server is a type of proxy server that typically sits behind the firewall in a private network and directs client requests to the appropriate back-end server. A reverse proxy provides an additional level of abstraction and control to ensure the smooth flow of network traffic between clients and servers.

What is an origin web server?
The server where web content originates. User requests that are satisfied by the origin server typically have the longest waiting times.  This term is used to differentiate content that is retrieved from a cache (and as a result, typically served faster).

nginx up and running with Docker


My Dockerfile extends from the official nginx docker image:
FROM nginx
MAINTAINER "Craig Trim <craigtrim@gmail.com>"

COPY static-html-directory /usr/share/nginx/html


Notice that I've copied a local directory on the same file path as my Dockerfile:
~/workspaces/public/nginx$ ls -lahR
total 8
drwxr-xr-x   4 craigtrim  staff   136B Jan 15 14:14 ./
drwxr-xr-x  15 craigtrim  staff   510B Jan 15 14:07 ../
-rw-r--r--   1 craigtrim  staff   107B Jan 15 14:14 Dockerfile
drwxr-xr-x   3 craigtrim  staff   102B Jan 15 14:14 static-html-directory/

./static-html-directory:
total 8
drwxr-xr-x  3 craigtrim  staff   102B Jan 15 14:14 ./
drwxr-xr-x  4 craigtrim  staff   136B Jan 15 14:14 ../
-rw-r--r--  1 craigtrim  staff    19B Jan 15 14:29 index.html
~/workspaces/public/nginx$ cat static-html-directory/index.html 
this is the index!

The HTML file contains a single line of text. Using docker, we'll build this image, launch a container, and view the HTML content in a browser.

The first step is building the image:
~/workspaces/public/nginx$ docker build -t tf/nginx .
Sending build context to Docker daemon 3.584 kB
Step 1 : FROM nginx
 ---> 407195ab8b07
Step 2 : MAINTAINER "Craig Trim <craigtrim@gmail.com>"
 ---> Using cache
 ---> c3b1042299cc
Step 3 : COPY static-html-directory /usr/share/nginx/html
 ---> b71b288606d3
Removing intermediate container 4a61cac3675b
Successfully built b71b288606d3


Launch the container:
~/workspaces/public/nginx$ docker run --name tfpmbl01 -d -p 8080:80 tf/nginx
2694b268f7a70307d0895dfbf02d1f04ee61f401b4cc0f10bbd4f0992e42ac4c

Note the naming convention.  I have multiple trained models in Tensorflow.  This container will eventually correspond to a model known internally as tfpmbl01.  This is a specific instance of the container, and known by this unique name.

At the moment, I only have one docker container running:
~/workspaces/public/nginx$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                           NAMES
2694b268f7a7        tf/nginx            "nginx -g 'daemon off"   About a minute ago   Up About a minute   443/tcp, 0.0.0.0:8080->80/tcp   tfpmbl01

In the future, there will likely be multiple containers using the tf/nginx image, but each one will potentially be configured with a different trained model; hence the naming.

Running containers can be stopped and removed using the first two digits of the container ID:
~/workspaces/public/nginx$ docker stop 26
26
~/workspaces/public/nginx$ docker rm 26
26

In order to to view the HTML content served up by nginx on my web browser, I need to know which IP the docker server is using.  The docker server is nothing more than my local Macbook, with a configured instance of Docker Machine.  For those still using boot2docker, it's time to switch.

~/workspaces/public/nginx$ docker-machine ls
NAME        ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
default     *        virtualbox   Running   tcp://192.168.99.100:2376           v1.9.1    
dev         -        virtualbox   Error                                         Unknown   machine does not exist
vbox-test   -        virtualbox   Error                                         Unknown   machine does not exist
~/workspaces/public/nginx$ docker-machine ip default
192.168.99.100

In my web browser, I get this:



Hello, World!

The next step is to replace our static HTML with a Hello, World! script in Python.


References

  1. [Docker Hub] How to use the Docker nginx Image
  2. [Blog] Installation and Configuration for Docker on OS X 
    1. Using Docker Machine rather than the deprecated boot2docker

No comments:

Post a Comment