Error trying to connect to nexus repo during Docker package build

During docker package build in my pipeline, I get an error when it tries to connect to our nexus repo to download some packages. The same command works fine when the same build happens in a Jenkins pipeline.

RUN pip3 install --user -r requirements.txt --extra-index-url=https://$username:$password@nexus.ourcompanyurl/repository/pathto/package

The error is -

EOFError: EOF when reading a line

The command ‘/bin/sh -c pip3 install --user -r requirements.txt --extra-index-url=https://$username:$password@nexus.ourcompanyurl/repository/pathto/package’ returned a non-zero code: 2

Any idea why it is having this issue ? I have been searching online since yesterday but have not found any resolution why this is happening in gitlab.