A database only needs to talk to its app. On an internal network it can do exactly that, and nothing on it can open connections to the internet or be reached through a published port.
docker network create --internal backend
docker network create frontend
Put the database only on backend, and the app on both networks. The app reaches the database by name on backend and talks to the world through frontend. The database has no outgoing route, so even a compromised database container cannot download anything or phone home.
In Compose it is one line on the network:
networks:
backend:
internal: true
I would use this for anything that has no reason to go online: databases, Redis, internal APIs. It costs nothing and removes a whole class of surprises.
Some images do need the internet more than you think. They check for updates, fetch plugins on first start or sync time, and fail in odd ways when they cannot. Watch the logs after the switch. A database admin tool that you want to open in a browser needs a normal network as well, next to the internal one.
Further reading: docker network create: internal, Compose file reference: networks internal