Export docker volume from one host to another

usage: ./script.sh sentry-postgres
#!/bin/bash

# Environment check

SOURCE_HOST_ADDRESS=localhost
SOURCE_HOST_USER=$USER
TARGET_HOST_ADDRESS=192.168.126.168
TARGET_HOST_USER=targetuser

# If migrating from localhost, set this to localhost
if [[ ! $SOURCE_HOST_ADDRESS ]]
then
  echo "Please set the current host address in SOURCE_HOST_ADDRESS"
  exit 1
fi

# If migrating from localhost, set this to $USER
if [[ ! $SOURCE_HOST_USER ]]
then
  echo "Please set the current host user in SOURCE_HOST"
  exit 1
fi

# If migrating to localhost, set this to localhost
if [[ ! $TARGET_HOST_ADDRESS ]]
then
  echo "Please set the new host address in TARGET_HOST_ADDRESS"
  exit 1
fi

# If migrating to localhost, set this to $USER
if [[ ! $TARGET_HOST_USER ]]
then
  echo "Please set the new host user in TARGET_HOST_USER"
  exit 1
fi

# Argument check

if [[ ! $1 ]]
then
  echo "Please supply a docker volume name, as displayed by the command 'docker volume ls'"
  exit 1
fi
volume_name=$1

# Export the volume from the current instance

echo "Exporting volume $volume_name on $SOURCE_HOST_ADDRESS"
ssh "$SOURCE_HOST_USER"@"$SOURCE_HOST_ADDRESS" "\
mkdir -p \$HOME/docker-volume-backup
docker run \
  --rm \
  -v $volume_name:/volume-backup-source \
  -v \$HOME/docker-volume-backup:/volume-backup-target \
  busybox \
  sh -c 'cd /volume-backup-source && tar cf /volume-backup-target/backup.tar .' \
"


# Transfer the exported volume to the new address

echo "Transferring exported volume $volume_name from $SOURCE_HOST_ADDRESS to $TARGET_HOST_ADDRESS"
ssh "$TARGET_HOST_USER"@"$TARGET_HOST_ADDRESS" "mkdir -p \$HOME/docker-volume-backup"
scp -3 "$SOURCE_HOST_USER"@"$SOURCE_HOST_ADDRESS":./docker-volume-backup/backup.tar \
  "$TARGET_HOST_USER"@"$TARGET_HOST_ADDRESS":./docker-volume-backup/backup.tar


# Restore the backup

echo "Creating volume $volume_name on $TARGET_HOST_ADDRESS"
ssh "$TARGET_HOST_USER"@"$TARGET_HOST_ADDRESS" "\
docker volume create $volume_name \
&& docker run \
  --rm \
  -v $volume_name:/volume-backup-target \
  -v \$HOME/docker-volume-backup/:/volume-backup-source \
  busybox \
  sh -c 'cd /volume-backup-target && tar xf /volume-backup-source/backup.tar .' \
"

# Clean up residual files

echo "Cleaning up unnecessary files"
ssh "$SOURCE_HOST_USER"@"$SOURCE_HOST_ADDRESS" "rm -rf \$HOME/docker-volume-backup"
ssh "$TARGET_HOST_USER"@"$TARGET_HOST_ADDRESS" "rm -rf \$HOME/docker-volume-backup"

echo "Successfully migrated docker volume $volume_name from $SOURCE_HOST_ADDRESS to $TARGET_HOST_ADDRESS"
29 октября 2024, 11:04

Worpress in docker: wrong file permission

In wp-config.pgp
define('FS_CHMOD_DIR', 0777);
8 августа 2024, 14:01

Install docker on ubuntu 24

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
23 мая 2024, 13:35

docker.errors.DockerException: Error while fetching server API version: HTTPConnection.request() got an unexpected keyword argument 'chunked'

If docker-compose doesn't work use
docker compose up -d
17 мая 2024, 22:06

Copy files from/to docker containers

docker cp <source_path>  <container_id>:<destination_path>
docker cp <container_id>:<source_path> <destination_path>
8 февраля 2024, 21:23

Docker install on ubuntu 20.04

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get --assume-yes install docker-ce docker-ce-cli containerd.io
16 января 2023, 11:15

Docker removes volumes

docker-compose down -v
26 января 2022, 22:22

List of docker container volumes

docker inspect -f '{{ .Mounts }}' containerid
1 ноября 2021, 16:52

Install docker on Ubuntu

sudo apt-get remove -y docker docker-engine docker.io containerd runc

sudo apt-get update

sudo apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

sudo apt-get update

sudo apt-get install -y docker-ce docker-ce-cli containerd.io

14 февраля 2021, 12:59

Run nodejs application in docker

docker run \
  -e "NODE_ENV=production" \
  -u "node" \
  -m "300M" --memory-swap "1G" \
  -w "/home/node/app" \
  --name "my-nodejs-app" \
  node [script]
19 июля 2020, 18:59

Restart all docker containers

docker restart $(docker ps -a -q)
25 июня 2020, 16:23

Install JIRA 8 on docker

version: '3.1'
services:
  jira:
    image: zhangguanzhang/atlassian-jira-software:8.0.0
    container_name: jira8
    restart: always
    environment:
      - "CATALINA_OPTS= -Xms1024m -Xmx4096m"
    links:
      - database
    volumes:
      - /home/jira8.0/data:/var/atlassian/jira
      - /home/jira8.0/logs:/opt/atlassian/jira/logs
      - /etc/localtime:/etc/localtime:ro
    ports:
      - '80:8080'

  database:
    restart: always
    container_name: jira8db
    image: percona:5.7
    environment:
      - MYSQL_DATABASE=jira
      - MYSQL_USER=jira
      - MYSQL_PASSWORD=jira
      - MYSQL_ROOT_PASSWORD=jira
    volumes:
      - /home/jira8.0/db:/var/lib/mysql
    ports:
      - "127.0.0.1:3306:3306"
    command:
      - '--collation-server=utf8_bin'
      - '--character-set-server=utf8'
      - '--max_allowed_packet=34M'
      - '--innodb_log_file_size=256M'
13 сентября 2019, 14:39

Rebuild all containers in docker-compose

 docker-compose up -d --force-recreate --build
11 сентября 2019, 11:43

Get docker host internal ip

docker inspect <container-id-or-name> | grep Gateway
7 сентября 2019, 10:08

Install docker on Ubuntu

sudo apt-get update

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

sudo apt-get update

sudo apt-get install docker-ce docker-ce-cli containerd.io

17 июня 2019, 16:31