Skip to main content

Command Palette

Search for a command to run...

how to migrate docker container to another server in ubuntu 22.04

Updated
2 min read
how to migrate docker container to another server in ubuntu 22.04

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

  1. 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
      
  2. 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
      
  3. Transfer the Tarball to the New Server

    • Use scp, rsync, or any file transfer method you prefer. Assuming you are using scp:

        scp ~/my_container_image.tar [user]@[destination_server_ip]:/path/to/destination
      
    • Example:

        scp ~/my_container_image.tar ubuntu@192.168.1.2:/home/ubuntu/
      

On the Destination Server

  1. 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
      
  2. 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
      

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.

More from this blog

T

Tenten - AI / ML Development

225 posts

🚀 Revolutionize your business with AI! 🤖 Trusted by tech giants since 2013, we're your go-to LLM experts. From startups to corporations, we bring ideas to life with custom AI solutions