Docker volumes are essential for maintaining data persistence in containerized applications. This guide will explain how to configure Docker volumes for a PostgreSQL container, and how to migrate this data to another computer or cloud instance.
Configuring Docker Volumes in docker-compose.yml
Here's an example of a docker-compose.yml
file that sets up a PostgreSQL container with a Docker volume:
services:
db:
image: postgres:12
restart: always
volumes:
- app-db-data:/var/lib/postgresql/data/pgdata
env_file:
- .env
environment:
- PGDATA=/var/lib/postgresql/data/pgdata
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD?Variable not set}
- POSTGRES_USER=${POSTGRES_USER?Variable not set}
- POSTGRES_DB=${POSTGRES_DB?Variable not set}
volumes:
app-db-data:
Explanation
- image: Specifies the PostgreSQL image version.
- restart: Ensures the container always restarts on failure.
-
volumes: Mounts the
app-db-data
volume to the PostgreSQL data directory. -
env_file: Loads environment variables from a
.env
file. - environment: Sets environment variables for PostgreSQL configuration.
Volumes
The volumes
section defines a named volume app-db-data
:
volumes:
app-db-data:
This volume is managed by Docker and ensures data persists even if the container is removed or restarted.
Data Persistence
Docker volumes store data on the host system, typically under /var/lib/docker/volumes/
. The data in app-db-data
is mounted to /var/lib/postgresql/data/pgdata
inside the container, ensuring PostgreSQL data is stored persistently.
Migrating Docker Volumes
To move Docker volume data to another computer or cloud instance, follow these steps:
1. Backup Volume Data
Run the following command to back up the volume data to a tar file:
docker run --rm -v app-db-data:/volume -v $(pwd):/backup alpine tar czf /backup/app-db-data.tar.gz -C /volume .
2. Transfer Backup File
Use scp
, rsync
, or a cloud provider's file transfer tool to move the backup file to the destination:
scp app-db-data.tar.gz user@remote_host:/path/to/destination
3. Restore Volume Data
On the destination machine, create a Docker volume and restore the data:
- Create the volume:
docker volume create app-db-data
- Restore the data:
docker run --rm -v app-db-data:/volume -v $(pwd):/backup alpine sh -c "tar xzf /backup/app-db-data.tar.gz -C /volume"
4. Start the Container
Use docker-compose
to start the container with the restored volume:
docker-compose up
Conclusion
By following these steps, you can ensure your PostgreSQL data is persistent and can be migrated across different environments. Docker volumes provide a robust way to manage data in containerized applications, making it easier to maintain and transfer critical data.
Top comments (0)