A Dockerized Python proxy server with automated deployment and service management using Docker Compose and systemd
on a Debian-based GCE instance.
Table of Contents
- ✅ Overview
- 📁 Project Structure
- 🧱 1. Docker Compose Setup
- 🛠️ 2. Makefile
- ⚙️ 3. systemd Service File
- 🚀 4. Enable and Start the Service
- 📄 5. Dockerfile
- 📄 6. app/main.py (Simple Example Proxy Server)
- 📄 7. systemd/proxy-server.service
- ✅ Final Steps
- Summary
✅ Overview
This guide walks you through setting up a Python server (e.g., a proxy or API) to run inside Docker on a Debian-based Google Compute Engine (GCE) instance. You’ll use:
-
Docker Compose for container orchestration.
-
Makefile for streamlined commands.
-
systemd
to ensure your app starts on boot and stays running.
📁 Project Structure
my-proxy-app/
├── Dockerfile
├── docker-compose.yml
├── Makefile
├── app/
│ └── main.py
└── systemd/
└── proxy-server.service
🧱 1. Docker Compose Setup
Create a docker-compose.yml
in your project directory:
services:
proxy:
build: .
container_name: proxy_server
restart: always
ports:
- '8000:8000'
volumes:
- ./logs:/app/logs
logging:
driver: 'json-file'
options:
max-size: '10m'
max-file: '5'
Key points:
-
restart
: always ensures the container restarts on failure or reboot. -
logging
: options prevent runaway disk usage via built-in Docker log rotation.
🛠️ 2. Makefile
Use a Makefile
to simplify build and runtime commands:
build:
docker-compose build
up:
docker-compose up -d
down:
docker-compose down
logs:
docker-compose logs -f
restart:
docker-compose down && docker-compose up -d
install:
sudo cp systemd/proxy-server.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable proxy-server
sudo systemctl start proxy-server
⚙️ 3. systemd Service File
Create a systemd
unit to manage your Docker Compose stack like a native system service.
Path: /etc/systemd/system/proxy-server.service
[Unit]
Description=Proxy Server via Docker Compose
After=network.target docker.service
Requires=docker.service
[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/home/youruser/app
ExecStart=/usr/local/bin/docker-compose up -d
ExecStop=/usr/local/bin/docker-compose down
TimeoutStartSec=0
[Install]
WantedBy=multi-user.target
Replace /home/youruser/app
with your actual project path.
🚀 4. Enable and Start the Service
Run the following to enable the service on boot and start it now:
make install
📄 5. Dockerfile
FROM python:3.13-slim
# Set working directory
WORKDIR /app
# Copy app code
COPY app /app
# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Expose port
EXPOSE 8000
# Run app
CMD ["python", "main.py"]
⚠️ Create a requirements.txt
in /app
with your needed Python packages, or modify the Dockerfile
to skip it if unnecessary.
📄 6. app/main.py (Simple Example Proxy Server)
import http.server
import socketserver
PORT = 8000
class Handler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(b"Proxy Server is running.")
if __name__ == "__main__":
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print(f"Serving on port {PORT}")
httpd.serve_forever()
📄 7. systemd/proxy-server.service
Move this to /etc/systemd/system/proxy-server.service on your GCE machine:
[Unit]
Description=Proxy Server via Docker Compose
After=network.target docker.service
Requires=docker.service
[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/home/youruser/my-proxy-app
ExecStart=/usr/local/bin/docker-compose up -d
ExecStop=/usr/local/bin/docker-compose down
TimeoutStartSec=0
[Install]
WantedBy=multi-user.target
Replace /home/youruser/my-proxy-app with your actual path.
✅ Final Steps
Build and run:
make build
make up
Enable and start systemd service:
Check status:
sudo systemctl status proxy-server
Summary
Your Python proxy server is now:
-
Dockerized for consistent builds.
-
Managed by
systemd
to auto-start and stay up. -
Configured via Docker Compose for simplicity and portability.