Let’s dockerize a Lapis service

Mazhar Ahmed
The Devs Tech
Published in
2 min readJul 25, 2020
Photo Credit: Google Image Search

Lapis is a web framework specially for Lua language. It also supports Moon Script. It uses OpenResty (a customized Nginx which supports lua) to run the web service.

Lapis is a full fledged framework that supports PostgreSQL, SQLite etc. But maybe you will need to write many things on your own. That’s why I will always prefer to write small services using Lapis. I haven’t used it in production yet but I am planning to use it. Usually lapis apps are very fast.

Today I will guide you on installing lapis, creating a new project and dockerizing it. Current version of lapis is v1.8.1 which works on Lua 5.1. So, keep this in mind that latest version of Lua will not work with Lapis. Also we will need OpenResty to run a Lapis service.

I’m using Ubuntu 20.04 LTS. So, let’s install Lua 5.1 first.

sudo apt update
sudo apt install lua51

And now install openresty

sudo apt install openresty

To install Lapis we need luarocks

sudo apt install luarocks

Now install Lapis

sudo luarocks install lapis

Now let’s create a new lapis project

mkdir devstech
cd devstech
mkdir app
cd app
lapis new --lua --git

The --lua argument will generate the project files in lua lang. And the --git will generate a .gitignore file.

Let’s see if our app runs properly

lapis server

And now the lapis server is on port 8080. Hit your browser at http://localhost:8080 . You can change the port by creating a new file named config.lua with the following content

local config = require("lapis.config")

config("development", {
port = 9090
})

Now the lapis will start on port 9090.

So the project is running properly, let’s dockerize it. Create Dockerfile in the devstech/app folder and docker-compose.yml at the devstech folder. We will use lapis’s docker hub image. Let’s change the Dockerfile like:

# grab the image from docker hub
FROM mileschou/lapis:alpine
# make a project folder
RUN mkdir /code
WORKDIR /code
# copy the source files
COPY . .

Now let’s change the docker-compose.yml like:

version: "3"
services:
app:
image: lapis-demo:latest
container_name: app
restart: unless-stopped
build:
# build the image from Dockerfile
context: ./app
volumes:
- ./app:/code
command: lapis server
ports:
- "9090:9090"

Now let’s build the image and run the container

docker-compose build
docker-compose up

Now hit your browser at the 9090 port.

That’s the way how we dockerize a lapis app. The source code from this tutorial is here: https://github.com/mazhar266/dockerized-lapis-boilerplate

Thank you, see you next time.

--

--