Docker Series Part 4: Volumes and Environment Variables for Local Development
Table of Contents
Docker Series Part 4: Volumes and Environment Variables for Local Development
In the last part, you learned how to use Docker Compose for multi-container apps. Now, let’s make your development workflow even smoother by using volumes and environment variables.
Why Use Volumes?
By default, changes you make to files on your host machine aren’t reflected inside your containers. Docker volumes let you share files between your host and containers, making it easy to develop and test code without rebuilding your image every time.
Example: Mounting Your Code
Update your docker-compose.yml:
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/app
environment:
- REDIS_HOST=redis
redis:
image: "redis:alpine"
Now, any changes you make to your code on your host are instantly available inside the container.
Using Environment Variables
Environment variables let you configure your app without changing code. You can set them in your docker-compose.yml or use a .env file.
Example .env file:
FLASK_ENV=development
REDIS_HOST=redis
And reference them in your docker-compose.yml:
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/app
env_file:
- .env
redis:
image: "redis:alpine"
Recap
- Volumes let you sync code between your host and containers for rapid development.
- Environment variables help you configure your app without hardcoding values.
In the next part, we’ll look at tips for debugging, logs, and best practices for Docker in development. Stay tuned!