前置要求
You need to have Docker Engine and Docker Compose on your machine. You can either:
- Install Docker Engine and Docker Compose as standalone binaries
- Install Docker Desktop which includes both Docker Engine and Docker Compose
第 1 步:写段 wlua 代码
- 创建一个工程目录:
$ mkdir composetest
$ cd composetest
- 在工程目录下新键文件
main.lua
,代码如下:
local wlua = require "wlua"
local redis = require "skynet.db.redis"
local g_db
local function getdb()
if g_db then return g_db end
local conf = { host = "redis", port = 6379, db = 0, }
g_db = redis.connect(conf)
return g_db
end
local app = wlua:default()
app:get("/", function (c)
local db = getdb()
local count = db:incr("hits")
c:send(string.format("Hello World! I have been seen %d times.\n", count))
end)
app:run()
在这个例子中, "redis"
是 redis 容器的网络名字,我们使用的默认的 Redis 端口 6379
和默认的数据库 0
。
第 2 步:创建一个 Dockerfile 文件
Dockerfile 用于构建一个 Docker 镜像,在你的 composetest
目录创建一个名为 Dockerfile
的文件,内容如下:
FROM hanxi/wlua
RUN cd / && wlua new code
WORKDIR /code
EXPOSE 8081
CMD [ "bash", "-c", "wlua start && tail -F log/wlua.log" ]
这几行告诉 Docker 干这些事:
- 从镜像
hanxi/wlua
的基础上构建一个新的镜像。 - 在根目录执行
wlua new code
命令,用于创建/code
工程目录。 - 设置工作目录为
/code
。 - 在镜像中添加元数据来描述容器正在监听 8081 端口。
- 使用
bash
执行命令:启动 wlua 和输出日志。
第 3 步:在 Compose 文件中定义服务
在 composetest
目录创建名为 docker-compose.yml
的文件,内容如下:
version: "3.9"
services:
web:
build: .
ports:
- "8000:8081"
volumes:
- ./main.lua:/code/app/main.lua
redis:
image: "redis:alpine"
这个 Compose 文件定义了 2个服务: web
和 redis
。
web
服务使用从当前目录中的 Dockerfile
构建的镜像。然后它将容器和主机绑定到公开的端口 8000
。此示例服务使用 wlua Web 服务器的默认端口 8081
。 还挂载了主机的 main.lua
文件到容器中。
redis
服务使用从 Docker Hub 镜像仓库中提取的公共 Redis
镜像。
第 4 步:使用 Compose 构建并运行您的应用
- 在你的
composetest
目录执行命令docker compose up
:
$ docker compose up
Creating network "composetest_default" with the default driver
Creating composetest_web_1 ... done
Creating composetest_redis_1 ... done
Attaching to composetest_redis_1, composetest_web_1
redis_1 | 1:C 07 Jan 2023 07:29:35.235 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 07 Jan 2023 07:29:35.235 # Redis version=7.0.7, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 07 Jan 2023 07:29:35.235 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | 1:M 07 Jan 2023 07:29:35.236 * monotonic clock: POSIX clock_gettime
redis_1 | 1:M 07 Jan 2023 07:29:35.236 * Running mode=standalone, port=6379.
redis_1 | 1:M 07 Jan 2023 07:29:35.236 # Server initialized
redis_1 | 1:M 07 Jan 2023 07:29:35.236 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1 | 1:M 07 Jan 2023 07:29:35.237 * Ready to accept connections
web_1 | start server
web_1 | tail: cannot open 'log/wlua.log' for reading: No such file or directory
web_1 | tail: 'log/wlua.log' has appeared; following new file
web_1 | [0000000a][2023-01-07 07:29:36] LAUNCH snlua main
web_1 | [0000000b][2023-01-07 07:29:36] LAUNCH snlua app/main http 1
web_1 | [0000000b][2023-01-07 07:29:36] [DEBUG] wlua new.
web_1 | [0000000b][2023-01-07 07:29:36] [DEBUG] routergroup new
web_1 | [0000000b][2023-01-07 07:29:36] [DEBUG] combine_handlers. n: 1
web_1 | [0000000b][2023-01-07 07:29:36] [DEBUG] calculate_absolute_path / /
web_1 | [0000000b][2023-01-07 07:29:36] [DEBUG] combine_handlers. n: 1
web_1 | [0000000b][2023-01-07 07:29:36] [DEBUG] add_route: GET / 2
web_1 | [0000000b][2023-01-07 07:29:36] [INFO] Open wlua agent. protocol: http , agent_id: 1
web_1 | [0000000a][2023-01-07 07:29:36] [INFO] Start web. host: 0.0.0.0 ,port: 8081
web_1 | [0000000a][2023-01-07 07:29:36] [WARN] Disable https server.
web_1 | [0000000a][2023-01-07 07:29:36] [INFO] Hello wlua.
web_1 | [00000002][2023-01-07 07:29:36] KILL self
- Enter http://localhost:8000/ in a browser to see the application running.
If this doesn’t resolve, you can also try http://127.0.0.1:8000.
You should see a message in your browser saying:
Hello World! I have been seen 1 times.
- Refresh the page.
The number should increment.
Hello World! I have been seen 2 times.
最后
composetest
工程地址: https://github.com/hanxi/docker-compose-wlua