Below is a “from-zero-to-running” guide that will let you containerise a single-file Python application (main.py) and launch it on a typical Linux server.
I’ll assume:
• You already have main.py and (optionally) a requirements.txt with your 3rd-party dependencies.
• Your server runs a Debian/Ubuntu-like distro. (CentOS/Alma etc. are almost identical – only the Docker install changes).
• You have SSH access with sudo privileges.
Feel free to skim/skip sections you already know; all the commands are copy-paste-ready. 😊
────────────────────
- Install Docker & Docker Compose
────────────────────
1-A) Update and install packages
sudo apt update && sudo apt install -y
ca-certificates curl gnupg lsb-release
1-B) Add Docker’s official GPG key & repo
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg |
sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo
"deb [arch=(dpkg−−print−architecture) signed−by=/etc/apt/key(lsb_release -cs) stable" |
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
1-C) Install the engine + CLI + compose plugin
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
1-D) (Optional) Run docker as your user instead of root
sudo usermod -aG docker $USER
→ Log out & back in (or new SSH session) to apply group change.
────────────────────
2. Prepare your project folder
────────────────────
Assume you have:
/home/ubuntu/myapp/
│
├─ main.py
├─ requirements.txt # (create if you haven’t)
└─ (any other .py, data files...)
2-A) Minimal requirements.txt example
If you don't have one, add libs like:
requests==2.31.0
or leave the file blank if you only use stdlib.
2-B) OPTIONAL .dockerignore
prevents copying venvs, caches, etc. into the image
pycache/
*.pyc
*.pyo
*.pyd
env/
venv/
.git
.gitignore
Dockerfile
────────────────────
3. Create a Dockerfile
────────────────────
myapp/Dockerfile
-----------------
FROM python:3.12-slim
3-A) (Optional) Set non-root user for security
ENV PYTHONUNBUFFERED=1
WORKDIR /app
RUN adduser --disabled-password --gecos '' appuser
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
RUN chown -R appuser:appuser /app
USER appuser
3-B) Default command (modify if your entry point differs)
CMD ["python", "main.py"]
That’s it—only 10 lines.
────────────────────
4. Build the image
────────────────────
cd /home/ubuntu/myapp
docker build -t my-python-app:1.0 .
Useful flags:
• ‑-platform linux/amd64 or arm64 if you’re on Apple Silicon.
• -f Dockerfile.dev etc., if you later introduce multi-stage builds.
────────────────────
5. Run the container
────────────────────
Detach (-d), give it a name, and map ports if your app listens.
docker run -d --name my-python-app
-p 8000:8000 \ # change/delete if CLI app
my-python-app:1.0
Watching logs:
docker logs -f my-python-app
Stopping / starting:
docker stop my-python-app
docker start my-python-app
────────────────────
6. (Optional) docker-compose.yml
────────────────────
Handy when you later add Postgres, Redis, etc.
version: "3.9"
services:
app:
build: .
container_name: my-python-app
ports:
- "8000:8000"
volumes: # hot-reloading during dev
- ./:/app
environment:
- PYTHONUNBUFFERED=1
restart: unless-stopped
Then:
docker compose up --build -d
docker compose logs -f
docker compose down
────────────────────
7. Automate updates
────────────────────
A tiny bash script (update.sh) in the project root:
#!/usr/bin/env bash
git pull # get latest code
docker compose build --pull # refresh base image
docker compose up -d # recreate
docker image prune -f # remove dangling layers
Add execute bit (chmod +x update.sh) and a cron entry if you like.
────────────────────
8. That’s it! 🎉
────────────────────
Recap:
- Install Docker → 2. Write Dockerfile → 3. Build → 4. Run.
You now have an isolated, reproducible environment for main.py on your server.
Need extras (private env vars, logging driver, multi-stage builds, slim/Alpine vs Debian images, healthchecks)? Just ask!