How do I pass environment variables to Docker containers when running them?

I can’t stress enough how risky hardcoding sensitive data like DB credentials in your Dockerfile is. In my experience, it’s always safer to pass environment variables when launching a container. Here’s a quick example:

docker run -e DATABASE_URL="your_db_url" mycontainer

Alternatively, you can group all your variables into an .env file and load them with --env-file. This not only keeps your secrets secure but also makes it easy to switch configurations across different environments without modifying the code. Keeping it flexible, secure, and maintainable is the key here.