user1553846
user1553846

Reputation: 63

Running Lua script in Docker

I can not get a Lua script to run from/inside a Docker image.

I have a very simple Lua script that I need to included in the image:

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

I have created a 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”]

But when I try to run the Docker image I get following error:

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

Is there a good explanation why I get this error and how I can make the script run when I run the Docker image.

Upvotes: 0

Views: 5064

Answers (3)

BMitch
BMitch

Reputation: 264306

Your final command has smartquotes in it around the lua command. These are invalid json characters:

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

As a result, Docker is trying to execute that string and throwing the error about a missing [/bin/sh,. Switch your quotes to normal quotes (and avoid whatever editor you used that added those):

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

As others have mentioned, you can skip the shell entirely:

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

And your hello.lua main function won't be called, so you can simplify this down to just the command you want to run:

print("hello world")

In the end, you should see something like:

$ 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

Upvotes: 1

adebasi
adebasi

Reputation: 3945

You can directly use the lua command as CMD in your Dockerfile:

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

Upvotes: 0

Alexander Altshuler
Alexander Altshuler

Reputation: 3064

Last line of your Dockerfile should be

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

Keep in mind, you hello.lua will print nothing. It defines function main, but this function is never called in this example.

It is not a Python, with Lua when you call a lua file the main chunk is called. If you want to pass parameters from command line:

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])

Upvotes: 1

Related Questions