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:
sampleapp.tar
to 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.
docker ps
http://localhost:5000
on the destination machine to confirm the application is running.This guide should give you a clear path to follow for container migration using PowerShell.
Happy reading !!
Thejas K