How to get docker-compose to always re-create containers from fresh images?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Docker Compose is a powerful tool that allows developers to manage and run multi-container Docker applications. It's widely used for local development and testing environments, making it easy to define and control the environment's behavior. However, there may be scenarios where you need to ensure that Docker Compose always re-creates containers from fresh images when starting your application. This can be particularly important when testing changes or updates to images.
Understanding Docker Compose Re-creation
By default, Docker Compose attempts to use existing containers if they were previously created. This can result in using outdated or altered images that may not reflect the current state of your Dockerfiles or the repository from which the images are pulled. To ensure you always get fresh images, you need to explicitly manage this behavior.
Key Docker Compose Commands
Docker Compose provides several commands to manage images and containers:
docker-compose up: Builds, (re)creates, and starts containers for a service.docker-compose down: Stops and removes containers, networks, and optionally, volumes, and images created bydocker-compose up.docker-compose pull: Pulls images for all services defined in thedocker-compose.ymlfile.docker-compose build: Builds or rebuilds services specified in thedocker-compose.ymlfile.
Forcing Container Re-creation with docker-compose
You may force Docker Compose to always use fresh images with the following methods:
1. Use --build Option
When you run docker-compose up, adding the --build option ensures that images are built fresh based on your Dockerfile:
This command rebuilds all images from the ground up, ensuring that any new changes in the Dockerfiles are applied.
2. Use --force-recreate Option
Adding --force-recreate when bringing up services will always stop and destroy existing containers and create new ones from the latest images:
You can combine this with the --build option:
3. Remove Images with down
Before running Docker Compose, you can use the down command with the --rmi all option to remove all images defined in your configuration:
This two-step process ensures all images are re-downloaded or rebuilt from scratch.
Example Docker Compose Workflow
Let's consider an example scenario where you have a simple application with a web service defined in docker-compose.yml.
Re-create Containers from Fresh Images
To ensure you're always working with fresh images, a potential workflow might be:
- Clean Down EnvironmentUse the
docker-compose downcommand with image removal:
- Force Build and Recreate ContainersInitiate the up command with forced build and re-creation:
Summarizing the Strategies
To summarize the approaches:
| Strategy | Description |
--build | Forces Docker to rebuild images based on Dockerfile changes. |
--force-recreate | Stops and destroys existing containers, always creating new ones. |
down --rmi all | Stops containers and removes all related images, ensuring fresh image pulls or builds on the next up. |
pull + --build + --force-recreate | Combines image pulling, forced building, and re-creation for a comprehensive approach. |
Additional Best Practices
- Use Tags: Specify image tags instead of
latestto avoid inconsistencies due to cached images. - Continuous Integration: Integrate these commands into CI/CD pipelines to automate the image update process.
- Environment Parity: Match your development environment closely with production to avoid unexpected behaviors.
By following these practices and strategies, you can ensure that your Docker-based applications consistently utilize the most current images for development and testing, reducing bugs and improving reliability across deployments.
Related reading
- how to get docker-compose to use the latest image from repository
- How to get filebeat to ignore certain container logs
- How to get logs of deployment from Kubernetes?
- How to get mac host IP address from a docker container?
- How to get pipenv running in docker?
- How to get the IP address of the docker host from inside a docker container
- How to get the list of dependent child images in Docker?
- How to get the status of a particular pod or container kubectl get pods using jsonpath

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.