from flask import Flask app = Flask(__name__) @app.route('/')
def hello_world(): return 'Hello from my containerized Python app!' if __name__ == '__main__': app.run(debug=True, host='0.0.0.0')
from flask import Flask app = Flask(__name__) @app.route('/')
def hello_world(): return 'Hello from my containerized Python app!' if __name__ == '__main__': app.run(debug=True, host='0.0.0.0')
from flask import Flask app = Flask(__name__) @app.route('/')
def hello_world(): return 'Hello from my containerized Python app!' if __name__ == '__main__': app.run(debug=True, host='0.0.0.0')
# Use an official Python runtime as a parent image
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 # Define environment variable
ENV NAME World # Run app.py when the container launches
CMD ["python", "app.py"]
# Use an official Python runtime as a parent image
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 # Define environment variable
ENV NAME World # Run app.py when the container launches
CMD ["python", "app.py"]
# Use an official Python runtime as a parent image
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 # Define environment variable
ENV NAME World # Run app.py when the container launches
CMD ["python", "app.py"]
Flask==2.0.1
Flask==2.0.1
Flask==2.0.1
docker build -t my-python-app .
docker build -t my-python-app .
docker build -t my-python-app .
docker run -p 5000:5000 my-python-app
docker run -p 5000:5000 my-python-app
docker run -p 5000:5000 my-python-app
sudo apt update
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker
sudo apt update
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker $USER
newgrp docker # Apply group changes to the current session
sudo usermod -aG docker $USER
newgrp docker # Apply group changes to the current session
# On your local machine
scp -r /path/to/your/app/directory user@your_vps_ip:/home/user/app
# On your local machine
scp -r /path/to/your/app/directory user@your_vps_ip:/home/user/app
# On your local machine
scp -r /path/to/your/app/directory user@your_vps_ip:/home/user/app
cd /home/user/app # Or wherever you copied your app
docker build -t my-python-app .
cd /home/user/app # Or wherever you copied your app
docker build -t my-python-app .
cd /home/user/app # Or wherever you copied your app
docker build -t my-python-app .
docker run -d -p 80:5000 my-python-app
docker run -d -p 80:5000 my-python-app
docker run -d -p 80:5000 my-python-app - FROM python:3.9-slim: This specifies the base image. We're using a lightweight official Python 3.9 image. Using -slim variants often results in smaller image sizes.
- WORKDIR /app: This sets the working directory inside the container. All subsequent commands will be executed from this directory.
- COPY . /app: This copies the files from your local project directory (where the Dockerfile is located) into the /app directory inside the container.
- RUN pip install --no-cache-dir -r requirements.txt: This command installs the Python dependencies. We assume you have a requirements.txt file. The --no-cache-dir flag helps reduce the image size by not storing the pip cache.
- EXPOSE 5000: This informs Docker that the container will listen on port 5000 at runtime. It's documentation and doesn't actually publish the port.
- ENV NAME World: This sets an environment variable named NAME with the value World. This is an example of how you can pass configuration into your container.
- CMD ["python", "app.py"]: This specifies the command to run when the container starts. It executes our Flask application. - docker build: This command initiates the image building process.
- -t my-python-app: The -t flag tags the image with a name (my-python-app). You can choose any name.
- .: This dot indicates that the Dockerfile is in the current directory. - docker run: This command creates and starts a new container from an image.
- -p 5000:5000: This maps port 5000 on your host machine to port 5000 inside the container. The format is host_port:container_port.
- my-python-app: This is the name of the image you want to run. - Provision a VPS: Choose a Linux distribution (like Ubuntu or Debian) and provision your VPS.
- Connect via SSH: Securely connect to your VPS using SSH.
- Install Docker: Install Docker on your VPS. The installation process will vary slightly depending on your VPS's operating system. For Ubuntu, you can typically use: sudo apt update
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker It's also a good idea to add your user to the docker group to run Docker commands without sudo: sudo usermod -aG docker $USER
newgrp docker # Apply group changes to the current session - -d: This runs the container in detached mode, meaning it will run in the background.
- -p 80:5000: This maps port 80 on your VPS to port 5000 inside the container.