A Lua client for the Docker Engine API. The library exposes Docker operations
as methods on a Docker class.
- Lua 5.1 or later
- A running Docker Engine
- The Lua dependencies
http,cjson, andbasexx
The examples also require serpent for formatted output. This dependency is
not required by the library itself:
luarocks install serpent
Install with LuaRocks from the repository directory:
luarocks make docker-scm.rockspec
After installation, the module can be loaded as docker. When running directly
from the repository, as in the examples, use code.docker:
local Docker = require 'code.docker'
The constructor accepts a Docker host, socket path, and API version. The API
version defaults to v1.38.
local Docker = require 'code.docker'
local docker = Docker:new(
'localhost',
'/var/run/docker.sock',
'v1.38'
)
local response, err = docker:ping_server()
if not response then
error(err)
end
print(response.body)
On success, methods return a response table (body, headers, and status).
On failure, they return nil and an error message.
In addition to Docker:new(...), Docker.new(...) and Docker(...) are
supported.
The default Docker Engine socket is /var/run/docker.sock. The user running
the program must have access to it. On many distributions, access is granted
through the docker group. Note that members of this group have extensive
privileges on the host.
local Docker = require 'code.docker'
local docker = Docker:new('localhost', '/var/run/docker.sock', 'v1.38')
Docker Desktop must be running. It usually provides the Docker socket at
/var/run/docker.sock:
local Docker = require 'code.docker'
local docker = Docker:new('localhost', '/var/run/docker.sock', 'v1.38')
If Docker Desktop uses a different socket path, pass that path instead.
The Docker Engine on Windows uses a named pipe by default
(//./pipe/docker_engine). This implementation uses lua-http with a socket
path and does not currently support Windows named pipes. A native connection
through this default endpoint is therefore unavailable.
You can run the project under WSL 2 with Docker Desktop integration enabled.
Docker is usually available at /var/run/docker.sock inside the WSL
distribution:
local Docker = require 'code.docker'
local docker = Docker:new('localhost', '/var/run/docker.sock', 'v1.38')
Docker Desktop must be running, and WSL integration must be enabled for the distribution you are using.
More examples are available in the example/ directory.
example.lua demonstrates basic container and image operations;
custom.lua demonstrates direct API calls with docker:custom(...).
example/utils.lua provides helper functions to print
values using serpent and to list HTTP headers.