Turns out the problem was not in the environment variables after all...
I was able to check weather the settings.xml file was reading the env variables correctly by running:
mvn help:effective-settings
and noticed that they were being correctly parsed.
The actual problem was that, in my settings file, I only had the credentials set up for the snapshots directory and not for the central one.
Since I had changed the version name in the pom.xml to something that did not include -SNAPSHOT, maven was trying to deploy it on the central repo and was not able to find the credentials.
The issue was solved by adding:
<server>
<id>central</id>
<username>${env.M2_USERNAME}</username>
<password>${env.M2_PASSWORD}</password>
</server>
to the settings.xml file
In the end there was nothing wrong with either docker nor maven. It was just my poor understanding of the maven build lifecycle :)