How to Update Images in docker-compose Projects Without Losing Container Data
This post was translated from Chinese by AI. If anything reads oddly, the Chinese original is authoritative. 中文原文

If your container data is mounted using Docker volumes or bind mounts, updating the image should not affect it. Here are the steps to update Docker images:
- First, pull the new image using this command:
docker-compose pull [service]
Here, [service] is the service name defined in your docker-compose.yml file. To update the images for all services, omit the service name.
- After pulling the new image, recreate the service using this command:
docker-compose up -d --no-deps [service]
This command recreates the service using the new image. The --no-deps option prevents other services related to this service from being restarted automatically. The -d option runs it in the background.
Note that [service] is the service name defined in your docker-compose.yml file. To recreate all services, omit the service name.
- If you only want to update the image without recreating the service, you can use the following command:
docker-compose pull [service] && docker-compose up -d --no-deps --build [service]
This command first pulls the new image, then recreates the service without restarting other related services. The --build option builds the service using the new image.
Remember that any data written to the container (for example, data generated by the application at runtime) will be lost when the container is deleted, unless you use volumes or bind mounts to persist it. Before performing these steps, make sure your data is safe.
If your application writes data directly into the container, you may need to migrate it. For example, export the data from the old container before creating a new one, then import it into the new container.
Last updated 2025-03-17
Comments 0