How to Get docker-compose to Always Re-create Containers from Fresh Images
TLDR
To make docker-compose always re-create containers from fresh images, use docker-compose pull to get the latest images, then run docker-compose up --force-recreate --build. This ensures containers are rebuilt or replaced, not reused from cache.
Why Containers Might Not Be Re-created
By default, docker-compose up reuses existing containers if they already exist, even if the image has changed. This can lead to running old code or missing updates, especially in development or CI.
The Solution: Use the Right Flags
--force-recreate: Forces docker-compose to remove and re-create containers, even if nothing changed in the config.--build: Builds images before starting containers (useful if you have local Dockerfiles).--pull: Always attempt to pull newer images before building (Compose V2 only, or usedocker-compose pullfirst).
Recommended workflow:
docker-compose pull # Get the latest images from the registry
docker-compose up --build --force-recreate
Or, with Compose V2 (Docker CLI):
docker compose up --build --force-recreate --pull always
Example
Suppose you updated your app image in a registry. To make sure your containers use the new image:
docker-compose pull app
# Then:
docker-compose up --build --force-recreate app
This will:
- Pull the latest image for
app - Build any local images if needed
- Remove and re-create the
appcontainer
Cleaning Up Old Containers and Images
If you want to remove stopped containers and unused images:
docker-compose down --rmi all --volumes
--rmi all: Remove all images used by services--volumes: Remove named volumes declared in thevolumessection
Best Practices
- Use
--force-recreatein development to avoid stale containers - Use
--pullordocker-compose pullto always get the latest images - For CI/CD, script these steps to ensure clean deploys
- Document your workflow for your team
Conclusion
To always get fresh containers from the latest images, combine docker-compose pull with docker-compose up --build --force-recreate. This keeps your environment up to date and avoids surprises from cached or outdated containers.
Related Resources
- Docker Compose Environment Variables — manage config across environments
- Docker Compose: Ports vs Expose — container networking fundamentals
- Delete All Local Docker Images — clean up stale images
- Introduction to Docker Guide — comprehensive Docker learning path
- Docker Multi-Stage Build Exercise — hands-on build optimization
We earn commissions when you shop through the links below.
DigitalOcean
Cloud infrastructure for developers
Simple, reliable cloud computing designed for developers
DevDojo
Developer community & tools
Join a community of developers sharing knowledge and tools
SMTPfast
Developer-first email API
Send transactional and marketing email through a clean REST API. Detailed logs, webhooks, and embeddable signup forms in one dashboard.
QuizAPI
Developer-first quiz platform
Build, generate, and embed quizzes with a powerful REST API. AI-powered question generation and live multiplayer.
Want to support DevOps Daily and reach thousands of developers?
Become a SponsorFound an issue?
Related Posts
Also worth your time on this topic
How to Use docker-compose up for Only Certain Containers
Want to start just a few services from your docker-compose file? Learn how to use docker-compose up to run only specific containers, with practical examples and tips for multi-service projects.
Docker Security Hardening Checklist
Comprehensive security checklist for hardening Docker containers, images, and runtime environments.
60-90 minutes
Running Docker Containers on Your Linux Server
Install Docker and Docker Compose on Ubuntu, run your first container, deploy a WordPress stack with docker-compose, and set up Nginx as a reverse proxy in front of your containers.
60 minutes