---
# System prepended metadata

title: Migrate docker image data to another machine
tags: [NTUToolmenLab]

---

# Migrate docker image data to another machine
###### tags: `NTUToolmenLab`

Copy Docker Image to Target Machine from Source Machine

## Target Machine
Use docker registry as server.

create certificate
```
mkdir certs
openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -subj "/C=TW/CN=tmp.docker.local" -keyout certs/privkey.pem -out certs/cert.pem
```

start docker registry
``` sh
docker run  --rm  --name registry -p 443:443 \
    -v $PWD/certs:/certs \
    -e REGISTRY_HTTP_ADDR=0.0.0.0:443 \
    -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/cert.pem \
    -e REGISTRY_HTTP_TLS_KEY=/certs/privkey.pem \
    --name reg
    registry:2
```

## Source Machine
Client side
Copy certification to permit docker access.
``` sh
scp -r user@target:~/certs .
sudo mkdir -p /etc/docker/certs.d/tmp.docker.local/
sudo cp certs/cert.pem /etc/docker/certs.d/tmp.docker.local/client.cert
sudo cp certs/cert.pem /etc/docker/certs.d/tmp.docker.local/ca.crt
sudo cp certs/privkey.pem /etc/docker/certs.d/tmp.docker.local/client.key
```

Add temporary hostname
`echo "192.168.target.ip tmp.docker.local" | sudo tee -a /etc/hosts`

Push
```
# Pull from outside
docker pull hello-world
# Change name
docker tag hello-world tmp.docker.local/hello-world
# Push to target
docker push tmp.docker.local/hello-world
```

Pull(**For Target Machine**)
```
docker pull localhost/hello-world
```

## Cleanup
On Source Machine
``` sh
sudo sed -i "/tmp.docker.local/d" /etc/hosts
sudo rm -rf /etc/docker/certs.d/tmp.docker.local
rm certs/ -rf
```

On Target Machine
``` sh
docker stop reg
rm certs/ -rf
```