can you write a devcontainer.json and a dockerfile for fastapi + react?
- User
can you write a devcontainer.json and a dockerfile for fastapi + react?
Assistant
Here's a devcontainer setup for FastAPI + React development:
devcontainer.json:
{ "name": "FastAPI + React Dev Environment", "build": { "dockerfile": "Dockerfile" }, "features": { "ghcr.io/devcontainers/features/git:1": {}, "ghcr.io/devcontainers/features/github-cli:1": {} }, "customizations": { "vscode": { "extensions": [ "ms-python.python", "ms-python.black-formatter", "ms-python.flake8", "ms-vscode.vscode-typescript-next", "bradlc.vscode-tailwindcss", "esbenp.prettier-vscode", "ms-vscode.vscode-json", "ms-python.autopep8" ], "settings": { "python.defaultInterpreterPath": "/usr/local/bin/python", "python.formatting.provider": "black", "editor.formatOnSave": true, "editor.codeActionsOnSave": { "source.organizeImports": true } } } }, "forwardPorts": [8000, 3000], "portsAttributes": { "8000": { "label": "FastAPI Backend", "onAutoForward": "notify" }, "3000": { "label": "React Frontend", "onAutoForward": "notify" } }, "postCreateCommand": "pip install -r requirements.txt", "remoteUser": "vscode" }Dockerfile:
FROM mcr.microsoft.com/devcontainers/python:1-3.11-bullseye # Install Node.js RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash - \ && apt-get install -y nodejs # Install additional system dependencies RUN apt-get update && apt-get install -y \ git \ curl \ vim \ && rm -rf /var/lib/apt/lists/* # Set working directory WORKDIR /workspace # Install Python dependencies (FastAPI) COPY requirements.txt* ./ RUN if [ -f requirements.txt ]; then pip install -r requirements.txt; fi # Install global npm packages for React development RUN npm install -g create-react-app @vitejs/create-vite # Create non-root user RUN groupadd --gid 1000 vscode \ && useradd --uid 1000 --gid vscode --shell /bin/bash --create-home vscode \ && chown -R vscode:vscode /workspace USER vscodeSample requirements.txt (create this in your project root):
fastapi==0.104.1 uvicorn[standard]==0.24.0 python-multipart==0.0.6 pydantic==2.5.0 python-jose[cryptography]==3.3.0 passlib[bcrypt]==1.7.4 python-dotenv==1.0.0 sqlalchemy==2.0.23 alembic==1.13.1Suggested project structure:
your-project/ ├── .devcontainer/ │ ├── devcontainer.json │ └── Dockerfile ├── backend/ │ ├── main.py │ ├── requirements.txt │ └── ... ├── frontend/ │ ├── package.json │ ├── src/ │ └── ... └── README.mdTo use this setup:
- Create a
.devcontainerfolder in your project root - Add both files to the
.devcontainerfolder - Create a
requirements.txtfile in your project root with your Python dependencies - Open your project in VS Code and select "Reopen in Container" when prompted
The container will expose port 8000 for your FastAPI backend and port 3000 for your React frontend. You can run your FastAPI server with
uvicorn main:app --host 0.0.0.0 --port 8000 --reloadand your React app withnpm startfrom their respective directories.- Create a
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.