Effortless Container Migration: A Step-by-Step Guide Using PowerShell

Blogs

Python Data Validation across Multiple Databases
August 15, 2024
Understanding Recovery Models: How They Influence SQL Server Performance
August 24, 2024

Effortless Container Migration: A Step-by-Step Guide Using PowerShell

Introduction:

In today’s rapidly evolving IT landscape, containerization has become an essential tool for developers and IT professionals alike, enabling consistent and efficient deployment of applications across various environments. However, as projects grow and infrastructure needs change, the ability to seamlessly migrate containers from one machine to another becomes crucial. Whether you’re transitioning to a new server, balancing workloads, or simply moving to a different environment, knowing how to perform container migrations using PowerShell can greatly simplify the process. In this blog, we will walk you through a step-by-step guide to migrating containers using PowerShell, from creating a sample application to ensuring a smooth and successful transition to your new machine.

Step 1: Create a Sample Application

1. Create a Directory for the Project:

powershell

mkdir SampleApp
cd SampleApp

2. Create a Sample Python Application:

python 

# app.py
from flask import Flask

app = Flask(__name__)

@app.route(‘/’)
def hello():
return “Hello, World!”

if __name__ == ‘__main__’:
app.run(host=’0.0.0.0′, port=5000)

Step 2: Containerize the Application

1.Create a Dockerfile:

In the SampleApp directory, create a Dockerfile with the following content:

powershell

touch Dockerfile

insert the file content into created docker file

# Use the official Python image from the Docker Hub
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install –no-cache-dir -r requirements.txt

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Run app.py when the container launches
CMD [“python”, “app.py”]

2.Build the Docker Image:

Run the following command in the SampleApp directory:

powershell

docker build -t sampleapp .

Step 3: Run the Container on the Source Machine.

1.Run the Container:

docker run -d -p 5000:5000 –name sampleappcontainer sampleapp

You can verify the application is running by navigating to http://localhost:5000 in your web browser.

Step 4: Save the Docker Image

1.Save the Docker Image to a Tar File:

Run the following command to save the Docker image to a file:

docker save -o sampleapp.tar sampleapp

2. Transfer the Tar File to the Destination Machine:

  • Use any file transfer method (e.g., SCP, FTP, USB drive) to move sampleapp.tar to the destination machine.

Step 5: Load and run the container in the destination machine.

  1. Load the Docker Image on the Destination Machine:
    •  Run the following command on the destination machine

docker load -i sampleapp.tar

2.Run the Container on the Destination Machine:

docker run -d -p 5000:5000 –name sampleappcontainer sampleapp

The application should now be accessible at http://localhost:5000 on the destination machine.

Step 6: Verify the Migration

  • Check Running Containers:

docker ps

  • Access the Application:
    • Open a web browser and navigate to http://localhost:5000 on the destination machine to confirm the application is running.

Summary

  1. Create a Sample Application: Write a simple Flask app and requirements.txt.
  2. Containerize the Application: Create a Dockerfile, build the image, and run it.
  3. Save the Docker Image: Save the image to a tar file and transfer it.
  4. Load and Run on New Machine: Load the image and run the container on the new machine.
  5. Verify: Check that the application is running as expected.

This guide should give you a clear path to follow for container migration using PowerShell.

Happy reading !!

 


Thejas K

Leave a Reply

Your email address will not be published. Required fields are marked *