PowerShell: backup Docker containers


I have to reinstall my computer several times in a year, and I need to keep backup of my different stuffs so I was looking for some kind of a procedure to backup Docker containers and I found this link Docker backup – Easy steps to backup and restore your containers (bobcares.com) where Reeshma Mathews explained how to do it.

However, sometimes I need to perform the backup procedure over several containers at the same time, so I tried to enhance this routine to make this happen using PowerShell.

I adapted Reeshma’s steps and include them in a PowerShell script as follows:

Reeshma uses docker ps command to get the containers in the Docker environment.

However, the command do not get those containers not running at the time of command execution.

So, I use the command adding the -a parameter, which list all the containers.

The command returns the information in several columns, as space separated values.

The last column is the name of the container, so I get the information by looking of the last space (Docker do not admit spaces in the containers name, so the last space is the previous char of the container name).

I store the result in a PowerShell variable, which becomes a string array, including the titles row.

$Result=docker ps -a

So, starting at the row number one  of the array, (starting in zero), there are the values from the containers. Each row is used as an argument of a function, which performs the actions documented by Reeshma.

for($i=1;$i -lt $result.count;$i++)
{
 
    Backup-Container $result[$i]
}

However, we need to use variables instead of hardcoding the names.

Since docker commands do not interpret the parameters well when call them from PowerShell, it is needed to prepare them in a string variable and call them by Invoke-Expression PowerShell command.

	 $command="docker save -o $namebak $Name"
	 Invoke-Expression $command

This is the function where I adapted the original code:

function Backup-Container
{
[CmdletBinding()]
param
(
	[Parameter(Mandatory=$true,Position=0,HelpMessage="Must define the docker container name" )]
	[string]$Line
 
)
 
	 $SpacePos=$Line.IndexOf(' ') # Find the first space to retrieve the ContainerID
	 $ContainerID=$Line.Substring(0,$SpacePos)
	 $SpacePos=$Line.LastIndexOf(' ') # Find the last space, where starts the container's name
	 $name=$Line.Substring($SpacePos)
	 Write-host "Backing up $name"
	 $Name=$Name.ToLower()+"bkp" # Add 'bkp' the the container's name as file name for the backup
	 $command="docker commit  $ContainerID $name" # First, create a commited version of the container
	 Invoke-Expression $command
	 $namebak="$name.tar"
	 $command="docker save -o $namebak $Name" # Then, save the commited one to disk
	 Invoke-Expression $command
}

Just a final couple of comments:

  • The function is in a PowerShell module, because the idea is progressively building a library of common Docker actions. That’s why the first line is for include the library.
Import-Module -Name D:\Desarrollos\PowerShell\Docker\DockerLib.psm1
  • The script change drive and folder to establish the place where I use to store the backups. Please, adapt it to your needs.
d: cd D:\Desarrollos\Docker\Backup
  • By using the Get-Process command, the script ensures Docker Desktop is running until start the process. If it is not the case, the script starts Docker Desktop.
# Look for Docker Desktop instance
$DockerProcesses=Get-Process|Where-Object {$_.ProcessName -eq 'Docker Desktop'}
if($DockerProcesses.count -eq 0) #If there is no instance, try to start it
{
    $command="C:\Program Files\Docker\Docker\Docker Desktop.exe"
    Invoke-Expression "& '$command'"
    while($DockerProcesses.count -eq 0) #Whait for an instance
    {
        Start-Sleep -Seconds 120
        $DockerProcesses=Get-Process|Where-Object {$_.ProcessName -eq 'Docker Desktop'}
    }
}

As usual, here you have the Docker Full backup script, and here the Docker library.

HTH

Comentario

Este sitio utiliza Akismet para reducir el spam. Conoce cómo se procesan los datos de tus comentarios.