问题
Im trying to build a docker volume directly from a JS image through a bash command.
The difficulty here mostly lays on the fact that I want the contents of the volume to be the eval of a string.
The string itself is JS code that reads from a Kafka stream.
I know how to execute the bash command directly from a js file, However im a bit lost when it comes to what the command should look like:
TO clarify: - I want to specify the eval(string) as the code to run inside the container - I want to specify three environment files - I want to run everything directly from a js file . - This JS file is already dockerised in case it matters
I understand Stack overflow is not meant to give me answers, but I have looked everyone and im having a real hard time figuring out how to proceed. SO any pointers would be very appreciated
回答1:
From my understanding you can proceed like this:
1) create a docker image that contains everything is needed to run the js file (OS, node etc)
2) once you receive the js file, create a temporary directory on the local drive and copy that file to it
3) run the container, mount your local dir and tell the container to execute the file from there. The command can look like this
docker run \
--mount type=bind,src=/your-temp-dir,dst=/temp-dir,readonly \
--detach \
your-docker-image-name \
node /temp-dir/your-javascript-file.js
See https://docs.docker.com/engine/reference/run/ for more details.
回答2:
I am not sure if you really need to dockerize the JS file. Rather what you can do is write the incoming stream (which is JS) to another file e.g. volume-creator.js and then call that file.
In this case your volume-creator.js should also include dependencies like node-docker which will make your docker commands easy and OS agnostic.
The reason I would avoid dockerizing the invoker JS is you may need some extra privileges to run docker commands within the container e.g you may need to mount the docker socket which can make your host OS and docker system vulnerable .
来源:https://stackoverflow.com/questions/57773548/build-a-docker-volume-based-of-an-image