This is very easy with YAML:
version: "3"
services:
myservice: &myservice
build:
context: ./myservice
command: ./something
environment: &myservice_environment
VAR1: "val1"
VAR2: "val2"
VAR3: "val3"
myotherservice:
<<: *myservice
environment:
<<: *myservice_environment
VAR1: "val1-bis"
See the doc regarding extension-fields