Groovy in Docker
The official image of Groovy is on Docker Hub as groovy. A tag names a major version (groovy:5) or an exact one (groovy:5.0.8); a project pins the exact tag so two machines run the same language. This is the Docker page of the install overview.
A one-liner
Pull and run in one command, with the program as the argument of groovy -e:
docker run --rm groovy:5 groovy -e "println Groovy.version"
--rm deletes the container when the command ends. The output is the version of the image, which is why the tag and not a printed number is the promise: a 5 tag moves with the patches, an exact tag does not.
Mount a script
A real script lives on the host. Mount the folder at the work directory of the image, which is /home/groovy/scripts:
docker run --rm -v "$PWD":/home/groovy/scripts -w /home/groovy/scripts groovy:5
groovy hello.groovy
The container reads the file from the mount, so an edit on the host is visible at the next run. A build that writes into the folder writes as the user of the container; on Linux add --user "$(id -u):$(id -g)" when the files must stay yours.
Why this container has no Docker
The lessons of this course run in a container already, and a container cannot start another one without the Docker socket. The commands above are the ones a reader runs on a machine with Docker; they were not run here.
Related
- Install Groovy and check the version: the overview of the other installs.
- The first script and groovy -e: the program of the one-liner.
Article author: Arthur Isaev