有没有更有效的方法将Luarocks Docker化?

我正在尝试构建一个轻巧的alpine docker容器,用于在Google Cloud Build中单元测试Lua。

它运行良好,但需要约30-50秒才能构建。当我运行busted和luacheck时,每个命令只需要几秒钟的时间。您对如何优化此构建过程有何想法?

我曾经使用wget,然后切换到了git。我添加了curl和unzip,因为luarocks需要它,以及luacheck的一个依赖项openssl。是否可以/应该使用不同的依赖项?

FROM alpine

ENV LUA_VERSION 5.1

RUN apk update

RUN apk add lua${LUA_VERSION}
RUN apk add lua${LUA_VERSION}-dev

RUN apk add bash build-base curl git openssl unzip

RUN cd /tmp && \
    git clone https://github.com/keplerproject/luarocks.git && \
    cd luarocks && \
    sh ./configure && \
    make build install && \
    cd && \
    rm -rf /tmp/luarocks

RUN luarocks install busted
RUN luarocks install luacheck
RUN luarocks install luacov
点赞
用户1425240
用户1425240

你不需要构建_luarocks_。你可以直接使用以下命令安装软件包,

RUN apk add luarocks
2021-01-27 07:25:01
用户1753098
用户1753098

你可以尝试这个

Dockerfile

FROM alpine:3.12

# 设置环境变量
ENV LUA_VERSION=5.1.5 \
    LUAROCKS_VERSION=3.4.0

# 安装依赖包
RUN set -xe && \
        apk add --no-cache --virtual .build-deps \
            curl \
            gcc \
            g++ \
            libc-dev \
            make \
            readline-dev \
        && \
        apk add --no-cache \
            readline \
        && \
        # 安装 Lua
        wget http://www.lua.org/ftp/lua-${LUA_VERSION}.tar.gz && \
        tar zxf lua-${LUA_VERSION}.tar.gz && rm -f lua-${LUA_VERSION}.tar.gz && \
        cd lua-${LUA_VERSION} && \
        make -j $(getconf _NPROCESSORS_ONLN) linux && make install && \
        cd / && rm -rf lua-${LUA_VERSION} && \
        # 安装 LuaRocks
        wget https://luarocks.org/releases/luarocks-${LUAROCKS_VERSION}.tar.gz && \
        tar zxf luarocks-${LUAROCKS_VERSION}.tar.gz && rm -f luarocks-${LUAROCKS_VERSION}.tar.gz && \
        cd luarocks-${LUAROCKS_VERSION} && \
        ./configure && \
        make -j $(getconf _NPROCESSORS_ONLN) build && make install && \
        cd / && rm -rf luarocks-${LUAROCKS_VERSION} && \
        # 删除所有 build 依赖
        apk del .build-deps && \
        # 测试
        lua -v && luarocks

COPY docker-entrypoint.sh /usr/local/bin

docker-entrypoint.sh

#!/bin/sh

set -e

buildDepsApk="
curl
libc-dev
gcc
wget
"

pm='unknown'
if [ -e /lib/apk/db/installed ]; then
    pm='apk'
fi

if [ "$pm" = 'apk' ]; then
    apk add --no-cache ${buildDepsApk}
fi

luarocks install $@

if [ "$pm" = 'apk' ]; then
    apk del ${buildDepsApk}
fi
2021-01-27 07:39:53