SoFunction
Updated on 2025-03-09

Implementation of Docker service migration

1 Backup Docker data on the current server

1.1 Stop Docker Service

To ensure data consistency, stop the Docker service before backing up:

sudo systemctl stop docker

1.2 Backup Docker Data

Docker's data is usually located in the /var/lib/docker directory. You can use the tar command to compress the directory into an archive file:

sudo tar -czvf /root/ /var/lib/docker

This creates a backup file containing all Docker containers, images, volumes, and configuration files.

1.3 Backup Docker Compose files (if Docker Compose is used)

If you use Docker Compose, make sure to back up all files as well. They are usually located in your project directory:

cp /path/to/your/ /root/

If there are multiple files, remember to back up all of them.

2 Transfer backup data to the new server

2.1 Transfer backup files using SCP or Rsync

To transfer backup files to a new server, you can use the scp or rsync tools. For example, use scp:

scp /root/ user@new-server-ip:/root/
scp /root/ user@new-server-ip:/root/

Replace user@new-server-ip as the username and IP address of the new server.

3 Recover Docker data on a new server

3.1 Install Docker

On the new server, first install Docker. If you haven't installed Docker, you can follow these steps to install:

sudo apt update
sudo apt install docker-ce docker-ce-cli 

3.2 Stop Docker Service

Stop the Docker service to prepare for recovery of data:

sudo systemctl stop docker

3.3 Recover Docker Data

Restore the backup Docker data to the /var/lib/docker directory:

sudo tar -xzvf /root/ -C /

This command will unzip the backup and restore its contents to the /var/lib/docker directory.

3.4 Set permissions

Make sure the permissions of the /var/lib/docker directory are correct:

sudo chown -R root:root /var/lib/docker

3.5 Start Docker Service

sudo systemctl start docker

3.6 Verify Docker status

Check whether Docker starts successfully and all containers, images, and volumes have been restored:

sudo docker ps -a
sudo docker images
sudo docker volume ls

4 Recover Docker Compose (if used)

4.1 Move the Docker Compose file to the project directory

Move the previously backed up files to the corresponding project directory:

mv /root/ /path/to/your/project/

4.2 Start Docker Compose Service

In the project directory, start all services using the docker-compose up command:

cd /path/to/your/project/
docker-compose up -d

This starts all services based on Docker Compose definitions.

5 Clean old server data (optional)

If the migration is successful and you no longer need Docker data on the old server, you can clean up Docker files on the old server:

5.1 Delete Docker Data

sudo rm -rf /var/lib/docker

5.2 Uninstall Docker (if required)

sudo apt remove docker-ce docker-ce-cli 
sudo apt purge docker-ce docker-ce-cli 

6 Ending

Through the above steps, you can successfully migrate the Docker service and all related data from one server to another. Make sure to make a backup before migration in case of any problems. At the same time, it is also very important to verify whether the service on the new server is running normally.

This is the end of this article about the implementation of Docker service migration. For more related content on Docker service migration, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!