Using .env files with Docker Swarm

I've been in love with Docker Swarm for the last few months. It's incomparably easier than Kubernetes and fits my (and probably most people's) needs perfectly.

I don't run it locally very often, but when I did, I found that it doesn't support the .env file out of the box. In Docker Compose, you can use .env files to store secrets locally so you don't commit them to the repository. I was surprised, but found a quick fix that works wonders if you ever find yourself running Docker Swarm locally:

export $(cat .env) > /dev/null 2>&1; docker stack deploy yourappname --with-registry-auth --compose-file=docker-compose.yml

Of course, --with-registry-auth and --compose-file aren't strictly necessary for this fix to work, but you're probably using them anyway! :)


Published on January 14, 2025 1 min read