在Docker中运行Lua脚本

我无法得到一个Lua脚本从Docker镜像运行。

我有一个非常简单的Lua脚本,我需要包含在图像中:

function main(...) print("hello world") end 

我创build了一个Dockerfile:

 FROM debian:latest RUN apt-get -y update && apt-get -y install lua5.1 lua-socket lua-sec ADD hello.lua /home/user/bin/hello.lua CMD ["/bin/sh", "-c", “lua /home/user/bin/hello.lua”] 

但是当我尝试运行Docker镜像时,出现以下错误:

 /bin/sh: 1: [/bin/sh,: not found 

有没有一个很好的解释,为什么我得到这个错误,以及如何运行Docker镜像时运行脚本。

你的Dockerfile的最后一行应该是

 CMD ["lua", "/home/user/bin/hello.lua"] 

请记住,你hello.lua将不会打印任何东西。 它定义了函数main,但是在这个例子中永远不会调用这个函数。

这不是一个Python,当你调用一个lua文件时,调用主要块。 如果你想从命令行传递参数:

 CMD ["lua", "/home/user/bin/hello.lua", "param1"] 

hello.lua:

 -- get all passed parameters into table local params = {...} -- print first parameters if any print(params[1]) 

你的最后的命令在lua命令周围有一个聪明的引号。 这些是无效的json字符:

 CMD ["/bin/sh", "-c", “lua /home/user/bin/hello.lua”] 

因此,Docker试图执行该string,并抛出关于丢失的错误[/bin/sh, 。 切换你的报价到正常的报价(并避免使用任何编辑器,添加这些):

 CMD ["/bin/sh", "-c", "lua /home/user/bin/hello.lua"] 

正如其他人所提到的,你可以完全跳过shell:

 CMD ["lua", "/home/user/bin/hello.lua"] 

而你的hello.lua主函数将不会被调用,所以你可以简化这个只是你想要运行的命令:

 print("hello world") 

最后,你应该看到像这样的东西:

 $ cat hello.lua print("hello world") $ cat Dockerfile FROM debian:latest RUN apt-get -y update && apt-get -y install lua5.1 lua-socket lua-sec ADD hello.lua /home/user/bin/hello.lua CMD ["lua", "/home/user/bin/hello.lua"] $ docker build -t luatest . Sending build context to Docker daemon 3.072 kB Step 1 : FROM debian:latest ---> 7b0a06c805e8 Step 2 : RUN apt-get -y update && apt-get -y install lua5.1 lua-socket lua-sec ---> Using cache ---> 0634e4608b04 Step 3 : ADD hello.lua /home/user/bin/hello.lua ---> Using cache ---> 35fd4ca7f0f0 Step 4 : CMD /bin/sh -c lua /home/user/bin/hello.lua ---> Using cache ---> 440098465ee4 Successfully built 440098465ee4 $ docker run -it luatest hello world 

您可以在Dockerfile中直接使用lua命令作为CMD:

 CMD ["lua", "/home/user/bin/hello.lua"] 
Interesting Posts