Skip to content

Transfer Docker Containers

Purpose: If you find that you need to migrate a container, along with any supporting files, permissions, etc from an old server to a new server, rsync helps make this as painless as possible. Be sure to perform the following steps to make sure that you can copy the container's files.

Warning

You need to stop the running containers on the old server before copying their data over, otherwise the state of the data may be unstable. Once you have migrated the data, you can spin up the containers on the new server and confirm they work before deleting the data on the old server.

On the destination (new) server, the directory needs to exist and be writable via the person copying the data over SSH:

Copying Data Between the Old and New Servers

sudo mkdir -p /srv/containers/example
sudo chmod 740 /srv/containers/example
sudo chown nicole:nicole /srv/containers/example
sudo mkdir -p /srv/containers
sudo chmod 777 /srv/containers

On the source (old) server, perform an rsync over to the new server, authenticating yourself as you will be prompted to do so:

rsync -avz -e ssh --progress /srv/containers/example/* [email protected]:/srv/containers/example
rsync -avz -e ssh --progress /srv/containers/example [email protected]:/srv/containers

Spinning Up Docker / Portainer Stack

Once everything has been moved over, copy the docker-compose and .env (environment variables) from the old server to the new one, pointing to the same location since we maintained the same folder structure, and the container should spin up like nothing ever happened.