Swiftorial Logo
Home
Swift Lessons
Matchuup
CodeSnaps
Tutorials
Career
Resources

Docker Backup and Recovery

Backing up and recovering Docker containers, images, and volumes is crucial for ensuring data integrity and availability. This guide covers key concepts, steps to back up and recover Docker resources, examples, and best practices for backing up and recovering Dockerized Express.js applications.

Key Concepts of Docker Backup and Recovery

  • Container Backup: Creating a snapshot of a running container.
  • Image Backup: Saving a Docker image to a tarball file.
  • Volume Backup: Copying the data from a Docker volume to an external storage.
  • Automated Backup: Scheduling regular backups to ensure data is consistently saved.
  • Recovery: Restoring containers, images, and volumes from backups.

Setting Up the Project

Initialize a new Express.js project and create a Dockerfile:

// Initialize a new project
// npm init -y

// Install Express
// npm install express

// Create the project structure
// mkdir src
// touch src/index.js Dockerfile .dockerignore .gitignore

// .gitignore
node_modules
.env

// .dockerignore
node_modules
npm-debug.log

Creating an Express Application

Create a simple Express application:

Example: index.js

// src/index.js
const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
    res.send('Hello, Docker Backup and Recovery!');
});

app.listen(port, () => {
    console.log(`Server running at http://localhost:${port}/`);
});

Creating a Dockerfile

Create a Dockerfile to containerize your Express application:

Example: Dockerfile

// Dockerfile
FROM node:14

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package*.json ./
RUN npm install

# Bundle app source
COPY . .

# Expose port 3000 to the outside world
EXPOSE 3000

# Run app when the container launches
CMD ["node", "src/index.js"]

Building and Running the Docker Container

Build and run the Docker container for your Express application:

// Build the Docker image
docker build -t my-express-app .

// Run the Docker container
docker run -d -p 3000:3000 --name my-express-app my-express-app

// Open http://localhost:3000 in your browser to see the application running

Backing Up Docker Containers

Create a snapshot of a running container:

Step 1: Commit the Container

// Commit the running container to an image
docker commit my-express-app my-express-app-backup

Step 2: Save the Image to a Tarball

// Save the image to a tarball file
docker save -o my-express-app-backup.tar my-express-app-backup

Backing Up Docker Volumes

Copy the data from a Docker volume to an external storage:

Step 1: Create a Container to Access the Volume

// Create a temporary container to access the volume
docker run --rm -v my_volume:/volume -v $(pwd):/backup busybox tar cvf /backup/my_volume_backup.tar /volume

Automated Backup

Schedule regular backups using a cron job:

Example: cronjob.sh

// cronjob.sh
#!/bin/bash

# Commit the running container to an image
docker commit my-express-app my-express-app-backup

# Save the image to a tarball file
docker save -o /path/to/backup/my-express-app-backup-$(date +%F).tar my-express-app-backup

# Backup the volume
docker run --rm -v my_volume:/volume -v /path/to/backup:/backup busybox tar cvf /backup/my_volume_backup-$(date +%F).tar /volume

Restoring Docker Containers

Restore a container from a backup tarball:

Step 1: Load the Image from the Tarball

// Load the image from the tarball file
docker load -i my-express-app-backup.tar

Step 2: Run a Container from the Restored Image

// Run a container from the restored image
docker run -d -p 3000:3000 --name my-express-app my-express-app-backup

Restoring Docker Volumes

Restore a volume from a backup tarball:

Step 1: Create a New Volume

// Create a new volume
docker volume create my_volume

Step 2: Restore the Data to the New Volume

// Restore the data to the new volume
docker run --rm -v my_volume:/volume -v $(pwd):/backup busybox tar xvf /backup/my_volume_backup.tar -C /

Best Practices for Docker Backup and Recovery

  • Regular Backups: Schedule regular backups to ensure data is consistently saved.
  • Automate Backups: Use cron jobs or CI/CD pipelines to automate the backup process.
  • Test Restores: Regularly test the restore process to ensure backups are valid and can be restored successfully.
  • Store Backups Securely: Store backup files in a secure and accessible location.
  • Use Versioning: Maintain versioned backups to restore from different points in time.
  • Monitor Backups: Monitor the backup process to detect and resolve any issues promptly.

Testing Docker Backup and Recovery

Test your Docker backup and recovery processes to ensure they work correctly:

Example: Testing with Mocha and Chai

// Install Mocha and Chai
// npm install --save-dev mocha chai

// test/app.test.js
const chai = require('chai');
const expect = chai.expect;
const axios = require('axios');

describe('Express App', () => {
    it('should return Hello, Docker Backup and Recovery!', async () => {
        const response = await axios.get('http://localhost:3000');
        expect(response.data).to.equal('Hello, Docker Backup and Recovery!');
    });
});

// Add test script to package.json
// "scripts": {
//   "test": "mocha"
// }

// Run tests
// docker build -t my-express-app .
// docker run -p 3000:3000 my-express-app
// npm test

Key Points

  • Container Backup: Creating a snapshot of a running container.
  • Image Backup: Saving a Docker image to a tarball file.
  • Volume Backup: Copying the data from a Docker volume to an external storage.
  • Automated Backup: Scheduling regular backups to ensure data is consistently saved.
  • Recovery: Restoring containers, images, and volumes from backups.
  • Follow best practices for Docker backup and recovery, such as regular backups, automating backups, testing restores, storing backups securely, using versioning, and monitoring backups.

Conclusion

Backing up and recovering Docker containers, images, and volumes is crucial for ensuring data integrity and availability. By understanding and implementing the key concepts, steps, examples, and best practices covered in this guide, you can effectively manage Docker backup and recovery for your Express.js applications. Happy coding!