Save the following command in a file named mvnd in project’s root directory.
docker run -it --rm -u 1000:1000 -v ~/.m2:/tmp/.m2 -v $(pwd):/app -w /app -p 8080:8080 -e MAVEN_CONFIG=/tmp/.m2 maven:3-openjdk-8 mvn -Duser.home=/tmp $@
Then make the file executable: chmod +x mvnd
./mvnd spring-boot run./mvnd compile./mvnd clean package./mvnd archetype:generate./mvnd -versiondocker run -itRuns the docker container in interactive mode (tty and input attached).
--rmRemoves the container once it stops.
-u 1000:1000User id and group id to be used. If skipped, it uses root:root by default.
-v ~/.m2:/maven/.m2Mounts local ~/.m2 directory within docker container at /tmp/.m2. This is needed to avoid re-downloading of maven artifacts on each run.
-v $(pwd):/appMounts local working directorty within docker container at /app. This is needed so that local files are available to maven that is running within a container.
-w /appSets working directory within the container.
-p 8080:8080Maps port 8080 to accept network connections. This is needed when doing something like spring-boot:run or tomcat:run.
If no service is started, this can be omitted.
If service is running on a different port, adjust it accordingly.
If multiple services (e.g. Tomcat and LiveReload in a spring-boot MVC app) are started, then repeat the option multiple times for each port.
-e MAVEN_CONFIG=/tmp/.m2This is required when -u 1000:1000 option is used. Otherwise skip it.
maven:3-openjdk-8Docker container image that is being run. maven is the image name, 3-openjdk-8is the version. See maven page at docker hub for all available versions.
mvnThis is the actual command to start maven. Remaining options are argumnets for this command.
-Duser.home=/tmpThis is required when -u 1000:1000 option is used. Skip otherwise.
$@This takes commandline arguments of the mvnd script and passes on to mvn command.