# Container migration

### Docker Container Migration

#### On old host (example : Vaultwarden)

```
docker ps
docker commit <container_id> vaultwarden
docker save vaultwarden | gzip > vaultwarden.tar.gz
scp vaultwarden.tar.gz pleb@<new_host>:/mnt/data/docker/
```

#### On new host

```
gunzip -c /mnt/data/docker/vaultwarden/vaultwarden.tar.gz | docker load
docker run -d --name=vaultwarden vaultwarden/server
```

# Moving docker containers between hosts

Script : [https://github.com/ricardobranco777/docker-volumes.sh](https://github.com/ricardobranco777/docker-volumes.sh "https://github.com/ricardobranco777/docker-volumes.sh")

##### Usage

```bash
docker-volumes.sh [-v|--verbose] CONTAINER [save|load] TARBALL
```

##### Example

```bash
# Stop the container
docker stop $CONTAINER

# Create a new image
docker commit $CONTAINER $CONTAINER

# Save image
docker save -o $CONTAINER.tar $CONTAINER

# Save the volumes (use ".tar.gz" if you want compression)
docker-volumes.sh $CONTAINER save $CONTAINER-volumes.tar

# Copy image and volumes to another host
scp $CONTAINER.tar $CONTAINER-volumes.tar $USER@$HOST:

# On the other host:
docker load -i $CONTAINER.tar
docker create --name $CONTAINER [<PREVIOUS CONTAINER OPTIONS>] $CONTAINER

# Load the volumes
docker-volumes.sh $CONTAINER load $CONTAINER-volumes.tar

# Start container
docker start $CONTAINER
```

Script : [https://github.com/ricardobranco777/docker-volumes.sh](https://github.com/ricardobranco777/docker-volumes.sh "https://github.com/ricardobranco777/docker-volumes.sh")

##### Usage

```bash
docker-volumes.sh [-v|--verbose] CONTAINER [save|load] TARBALL
```

##### Example

```bash
# Stop the container
docker stop $CONTAINER

# Create a new image
docker commit $CONTAINER $CONTAINER

# Save image
docker save -o $CONTAINER.tar $CONTAINER

# Save the volumes (use ".tar.gz" if you want compression)
docker-volumes.sh $CONTAINER save $CONTAINER-volumes.tar

# Copy image and volumes to another host
scp $CONTAINER.tar $CONTAINER-volumes.tar $USER@$HOST:

# On the other host:
docker load -i $CONTAINER.tar
docker create --name $CONTAINER [<PREVIOUS CONTAINER OPTIONS>] $CONTAINER

# Load the volumes
docker-volumes.sh $CONTAINER load $CONTAINER-volumes.tar

# Start container
docker start $CONTAINER
```
