With plain docker cp you first need the full container name, which in a Compose project is something like myproject-web-1. Compose has its own version that takes the service name instead.
docker compose cp web:/etc/nginx/nginx.conf ./nginx.conf
That pulls the config file out of the web service into the current folder. It works the other way round too, so you can push a file into the container:
docker compose cp ./robots.txt web:/usr/share/nginx/html/robots.txt
I use this most for looking at the default config an image ships with before I mount my own version over it. Copy it out, edit it on the host, then mount the edited file in your compose file.
A few options from the docs are worth knowing. --index picks a container when the service runs several replicas. -a (--archive) keeps the uid and gid information of the files. -L follows a symlink in the source path, and --all also includes containers that were started with docker compose run.
Caveat: a file you copy into a container lives in that container’s writable layer. Recreate the container, for example after docker compose pull and up -d, and the change is gone. For anything you want to keep, use a volume or a bind mount and treat cp as a way to grab or test files.
Further reading: docker compose cp reference