Docker-compose, as a single-node multi-container orchestrator, is able to build images for its services, Swarm, as a mulit-node multi-container orchestrator, does not support that feature, as the image would be potententialy required on each node of the cluster.
The clean approach: either use pre-existing images from dockerhub, or create images and push them to dockerhub or a private registry. Then use this image in your swarm stack docker compose file.
The manual approach without a container registry:
Build the image on one of the nodes, save the image to a .tar file and load it into the other nodes. In an air-gapped environment, you typicaly save the images to a .tar and just load them into the docker engines in the air-gapped environment.
While the clean solution is resilient when images get purged, the second is not.