OpenResty

The base Python image uses Debian's apt package manager, and OpenResty (the nginx distribution that includes Lua and other good extensions) is not available directly in the stable Debian repository. However, it is quite simple to compile and install OpenResty from its source release.

In the Dockerfile, we first want to make sure the Debian environment has all the required packages to compile OpenResty.

The following instructions first update the packages list, then install everything needed:

RUN apt-get -y update &&  
apt-get -y install libreadline-dev libncurses5-dev &&
apt-get -y install libpcre3-dev libssl-dev perl make

Notice that the three commands in the preceding code are merged as a single RUN instruction to limit the number of instructions the Dockerfile has. By doing so, you can limit the final image size.

The next step is to download the OpenResty source code, and perform the compilation step. cURL is already available in the Python base image and you can pipe it with the ;tar ;module to decompress the OpenResty release tarball directly from its URL.

The following configure and make calls are straight from OpenResty's documentation, and compile and install everything:

 RUN curl -sSL https://openresty.org/download/openresty-1.11.2.3.tar.gz  
| tar -xz &&
cd openresty-1.11.2.3 &&
./configure -j2 &&
make -j2 &&
make install

Once the compilation is over, OpenResty is installed in /usr/local/openresty and you can add an ENV instruction to make sure that the nginx executable is available directly in the container's PATH variable:

ENV PATH "/usr/local/openresty/bin:/usr/local/openresty/nginx/sbin:$PATH" 

The last thing we need to do for OpenResty is to include an nginx configuration file to start the web server.

In the following minimal example, nginx proxies all calls made to the 8080 port, to the container 5000 port--as shown in the previous diagram:

    worker_processes  4; 
error_log /logs/nginx-error.log;
daemon off;

events {
worker_connections 1024;
}

http {
server {
listen 8080;

location / {
proxy_pass http://localhost:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}

Notice that the error_log path uses the /logs/ directory. It's the root directory within the container for our logs.

That directory needs to be created via a RUN instruction, and a mount point can be added thanks to the VOLUME instruction:

RUN mkdir /logs 
VOLUME /logs

By doing this, you will be able to mount the /logs directory on a local directory on the host at runtime, and keep the log files even if the container is killed.

A Docker container filesystem should always be considered as a volatile volume, which can be lost at any moment.
If the processes in a container produce anything precious, the resulting data should be copied to a directory that is mounted as a volume outside the container's filesystem.

This configuration file is a full ;nginx configuration and can be used directly with nginx's -c option with this call:

$ nginx -c nginx.conf 

Once nginx runs, it makes the assumption that Circus listens to incoming TCP connections on port 5000 and nginx listens itself on port 8080.

Let's now configure Circus so that it binds a socket on that port and spawns a few Flask processes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset