To migrate a Docker container from one Ubuntu 22.04 server to another, follow these steps. This process involves exporting the container as an image, transferring the image to the new server, and then creating a new container from that image.
On the Source Server
-
Export the Docker Container to an Image
- First, identify the container ID or name you wish to migrate with
docker ps
. - Then, commit the container to an image:
docker commit [CONTAINER_ID_OR_NAME] [new_image_name]
- Example:
docker commit my_container my_container_image
- First, identify the container ID or name you wish to migrate with
-
Save the Image to a Tarball
- Save the newly created image to a tarball file:
docker save -o [path_to_save]/[image_name].tar [image_name]
- Example:
docker save -o ~/my_container_image.tar my_container_image
- Save the newly created image to a tarball file:
-
Transfer the Tarball to the New Server
- Use
scp
,rsync
, or any file transfer method you prefer. Assuming you are usingscp
:scp ~/my_container_image.tar [user]@[destination_server_ip]:/path/to/destination
- Example:
scp ~/my_container_image.tar [email protected]:/home/ubuntu/
- Use
On the Destination Server
-
Load the Image from the Tarball
- Once the tarball is transferred, load the image into Docker:
docker load -i /path/to/[image_name].tar
- Example:
docker load -i /home/ubuntu/my_container_image.tar
- Once the tarball is transferred, load the image into Docker:
-
Create a New Container from the Image
- Now, create a new container from the image. You’ll need to replicate any port mappings, volume bindings, or environment variables manually. Here’s a basic example:
docker run -d [other_options] [image_name]
- Example (assuming you want to run it in detached mode):
docker run -d my_container_image
- Now, create a new container from the image. You’ll need to replicate any port mappings, volume bindings, or environment variables manually. Here’s a basic example:
This process does not migrate the volumes or networks the container may use. If your container uses named volumes or custom networks, you will need to manually create and configure these on the new server before running the new container.
This method is suitable for simple migrations. For more complex applications, consider using Docker Compose to manage your application stack, making migration easier by defining your application services, networks, and volumes in a docker-compose.yml
file.