Per the docker-compose file documentation for build, specify build: and image: for the first service, and then only image: for subsequent services.
Here’s a modified version of your example that only builds the image once (for abc service) and reuses that image for xyz service.
version: '2'
services:
abc:
image: myimage
command: abc
build:
context: .
dockerfile: Dockerfile
xyz:
image: myimage
command: xyz