I'm trying to setup a Docker container to build my java project with maven
I have created my Docker file FROM maven:3.2-jdk-7 and built the image.
when I execute with:
docker run -it --rm --name my-maven-project -v "$PWD":/usr/src/app -v "$HOME"/.m2:/root/.m2 -w /usr/src/app -v "$HOME"/.ssh:/root/.ssh test mvn clean package -Dmaven.test.skip=true
It takes about 20 minute to complete.. but if I run the the same mvn command on my host it takes 2minutes
I have tried giving more memory to the container by using
-m 4gb
But it didn't change anything, looking at docker stats the container barely used more than 2G
I'm running all this from OSX
Is there anything I need to do to have a maven finish in a decent time? I very very surprised it takes THAT much when on the host it takes 2minutes..
This what docker stats says after maven has been building for 10 min
CPU: 201.13%
Mem usage / limit : 2.508GiB
MEM % : 62.69%
NET I/O: 3.01kB / 861B
BLOCK I/O: 57.7MB / 2.23MB
PIDS: 38
- EDIT - It turns out Docker for mac does not play well when using mounted volume. In order to avoid having to git clone the project inside the container I preferred using using -v "$PWD":/usr/src/app
To test I have directly git cloned the app form within the container and now the build takes a normal amount of time (4minutes)
Note that the git clone took... 6 min!!! instead (1min on host) so in total from git clone to final build it still takes 10min which is ridiculous.
So yea OSX and Docker is a big no no when using mounted volume...
A slow CPU or insufficient RAM can become a bottleneck, slowing down Docker's performance. When you experience slow Docker performance, check your CPU, memory usage, and available disk space. Consider upgrading your system if a component does not perform as expected.
The easiest way to increase the speed of your Docker image build is by specifying a cached image that can be used for subsequent builds. You can specify the cached image by adding the --cache-from argument in your build config file, which will instruct Docker to build using that image as a cache source.
CPU. By default, each container's access to the host machine's CPU cycles is unlimited. You can set various constraints to limit a given container's access to the host machine's CPU cycles. Most users use and configure the default CFS scheduler.
I ran into this same issue using the same docker run syntax as you (docker run -v src:dest). A Maven build that took ~30 seconds on my OSX host was taking ~4 minutes in my container. I didn't solve it entirely, but switching to explicitly use a bind mount took my builds from around 4 minutes down to about 1.5 minutes. This still isn't an acceptable increase in build time for my use case, but it may help someone else. Try switching your docker run command to this:
docker run --name=my-maven-project -it \
--mount type=bind,source="$(pwd)",destination=/usr/src/app,consistency=delegated <docker image name>
NOTE: The consistency option at the very end is only valid on OSX, and has two other values, either of which may be more appropriate for your situation. I tried all three out of curiosity and build times were comparable between the delegated and cached options, meanwhile the consistent option nearly as slow as the way I was doing it before (unsurprisingly). Here's the documentation:
https://docs.docker.com/storage/bind-mounts/
So, unfortunately, despite bind mounts being "very performant," they're still apparently at least twice as slow as a native filesystem when it comes to maven builds, at least on OSX. With luck that will improve as time goes on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With