Do you try to use swarm services that create container tasks (=docker stack deploy), or do you want to simply create containers with docker-compose?
Officialy secrets are only supported with swarm services:
Note : Docker secrets are only available to swarm services, not to standalone containers. To use this feature, consider adapting your container to run as a service. Stateful containers can typically run with a scale of 1 without changing the container code.
Regardless of what the documentation claims, I have seen people using version: "3.x" versions of the compose schema that where able to use secrets with docker-compose deployments as well!
Try adding version: "3.8" in the first line of your compose file. This will influence which compose file schema version you use. I guess since you used no version at all, compose will assume a version: "1" schema.
Note1: the build: item you used is only valid for docker-compose. It is not supported for swarm stack deployments.
Note2: secrets are the only reason to not use version: "2.4" with a docker-compose deployment, as it does not allow secrets, but allows to configure every aspect that docker run offers, while version: "3.x" does not support every aspect. If you don’t care for ressource constraints (which productive deployments always should) then it shouldn’t matter if you use the latest 2.x schema version or latest 3.x schema version.
I want to simply create containers with docker-compose,
Thank you for your reply, your advice solved my problem. But finally, I used version 3.3 and replace some rows in the file according to comments from docker-compose.yaml.