Skip to main content

Transfer/Move Docker

Sample: osslab-dekiwiki
docker run -d -p 8880:80 \
-v /etc/localtime:/etc/localtime:ro \
-v $PWD/vol/data:/data \
-v $PWD/vol/var-lib-mysql:/var/lib/mysql \
-v $PWD/vol/var-log-httpd:/var/log/httpd \
-v $PWD/vol/var-log-dekiwiki:/var/log/dekiwiki \
-v $PWD/vol/var-www-dekiwiki-attachments:/var/www/dekiwiki/attachments \
-v $PWD/vol/var-www-dekiwiki-bin-cache:/var/www/dekiwiki/bin/cache \
--name $NAME \
osslab-dekiwiki \
/bin/bash
Using Save Command

Save Image

# osslab-dekiwiki is image name
docker save osslab-dekiwiki > osslab-dekiwiki.tar

ls -l
-rw-r--r-- 1 root root 2519701504 Feb 27 19:19 osslab-dekiwiki.tar

Load Image

docker load < osslab-dekiwiki.tar
Using Export Command

Run the instance in detach mode

docker run -it --detach --name osslab-mig osslab-dekiwiki

docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                            NAMES
673f45c27a8b        osslab-dekiwiki     "/bin/sh -c /startup…"   5 seconds ago       Up 4 seconds        80/tcp                           osslab-mig

Export Docker Container

docker export 673f45c27a8b > osslab-mig.tar

ls -l
-rw-r--r-- 1 root root 1143085568 Feb 27 14:21 osslab-mig.tar

Import Docker Container to the target machine

tar -c osslab-mig.tar | docker import - osslab-dekiwiki
Move Container
## Stop the container
docker stop <container-name>

## Save container image
docker commit <container-name> mycontainerimage
docker save mycontainerimage | gzip > mycontainerimage.tar.gz

## Load container image to destination host
gunzip -c mycontainerimage.tar.gz | docker load 

## Transfer image without creating a file
docker save mycontainerimage | gzip | ssh root@203.0.113.1 'gunzip | docker load'
Backup & Restore Volume

Backup volume

# Backup
cd /root/osslab
docker stop osslab-dekiwiki
docker rm osslab-dekiwiki
tar czf osslab-val.tar.gz vol

# Restore to target machine
cd /root/osslab
tar xpzf osslab-val.tar.gz
FAQ

Q: Import 完成,但無法啟動 container。

Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "/bin/sh": stat /bin/sh: no such file or directory: unknown
Error: failed to start containers: osslab

Solution: 不要使用 Export-Import 改成 Save-Load 方式移轉。

Q: 執行 docker load 發生錯誤。

open /var/lib/docker/tmp/docker-import-863867335/bin/json: no such file or directory 

Solution: 只有 Save 的 image 檔才能執行 docker load;Export 的 image 檔只能用 docker import