How do you create an APFS volume inside an ordinary file?
If you have a file that contains a raw image of a disk, you can use the following command in the Terminal:

hdiutil attach -imagekey diskimage-class=CRawDiskImage myimage.raw


If you haven't got an existing image, you can create a new, empty file named myimage.raw and then attach it without mounting:

hdiutil attach -imagekey diskimage-class=CRawDiskImage -nomount myimage.raw


Then you can create APFS volumes on there as you would have a physical disk and mount them. This can be done via the command line with a command such as:

diskutil apfs create disk5 MyNewVolumeName


where disk5 needs to be replaced with the device name, you've received from the attach command earlier.