How to Modify docker-compose.yml for Easy Backup and Restore
What is docker-compose.yml?
Think of docker-compose.yml as a recipe. You list all the ingredients you need, and Docker gathers everything and sets it up automatically. The example below has an app and a database that need to work together.
services:
app:
image: example.com/app:latest
ports:
- "80:80"
environment:
USERNAME: ****
PASSWORD: ****
depends_on:
- db
db:
image: postgres:16
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
Bind mounts
By default, Docker stores data in a hidden directory on your computer (/var/lib/docker/volumes). Instead, you can store your data in a folder next to the docker-compose.yml file.
- Comment out the top-level volumes section:
# volumes:
# postgres_data:
- Update the database volume section to use
./:
db:
volumes:
- ./postgres_data:/var/lib/postgresql/data
The ./ tells Docker to store data locally in your project folder, not in the hidden system directory.
- To backup, just copy your project folder. To restore, paste the folder back and run
docker compose up -d
Pin Your Image Versions
app:
image: example.com/app:latest
The part after the colon (:) specifies which version of the image Docker should pull. A Docker image contains everything your app needs, such as the code and configuration. Think of it as a snapshot.
The latest tag downloads whatever the current version is, and that’s risky. Your app might break after a major update.
This happened to me with Lychee. When I restored it on a new VPS, Docker automatically pulled the newest version that wasn’t compatible with my backup. The app broke.
Avoid latest. Instead, visit Docker Hub, search for your app, and use the most recent version tag:
app:
image: example.com/app:v1.0.0
Organize Multiple Docker Applications for Easy Backups
Create a docker folder:
mkdir docker; cd dockerInside the docker folder, create app folders.
mkdir app1 app2 app3In each app folder, create docker-compose.yml.
docker/
app1/
docker-compose.yml
app2/
docker-compose.yml
app3/
docker-compose.yml
- When you backup, just backup the docker folder:
tar -cvf docker.tar docker/. When you restore, just paste the docker folder back.
