Railway will also automatically detect a Dockerfile if one is present.
Dockerfile
FROM python:3.12-slim
WORKDIR /app
# Dependencies in a separate layer for caching
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
requirements.txt
fastapi
uvicorn[standard]
sqlmodel
python-jose[cryptography]
passlib[bcrypt]
.dockerignore
.git
.env
__pycache__
*.pyc
.pytest_cache
docker-compose.yml
version: "3.9"
services:
api:
build: .
ports:
- "8000:8000"
environment:
- DATABASE_URL=postgresql://user:pass@db/mydb
- SECRET_KEY=${SECRET_KEY}
depends_on:
db:
condition: service_healthy
db:
image: postgres:16
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: mydb
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U user"]
interval: 5s
timeout: 5s
retries: 5
volumes:
postgres_data:
Running
docker compose up --build
docker compose up -d # run in background
docker compose logs -f api # follow logs
docker compose down # stop
Migrations in Docker
# Add a command in docker-compose.yml
services:
api:
command: >
sh -c "alembic upgrade head && uvicorn main:app --host 0.0.0.0 --port 8000"
Deploying to Railway
# railway.toml
[build]
builder = "dockerfile"
[deploy]
startCommand = "uvicorn main:app --host 0.0.0.0 --port $PORT"
Railway will also automatically detect a Dockerfile if one is present.
💬 Comments (0)
No comments yet
Be the first to share your opinion about this article!