Inside Dockerfile, using the official Nextcloud Docker image, how do I run occ commands as www-data?

First you must enter the container:

docker exec -it nextcloudpi /bin/bash

Now the commands will work. This is how containers literally function, by segregating different services in Docker from the host machine via containers. Volumes are actually folders on the host machine. Images are what you pull to define the containers and volumes you end up running.