Starter Template Showing How To Configure SvelteKit with FastAPI All Running Inside of Docker Containers
This app shows how to configure a SvelteKit frontend with a FastAPI backend and have them run inside of Docker containers using Docker Compose.
You can clone this repo, but here are the instructions to replicate this stack yourself with the updated versions of SvelteKit, FastAPI, and Docker:
- Create a folder on your computer where you want to store your project code.
- Client-side Setup:
- From a terminal window,
cdinto your project folder. - In your terminal, type
npm init svelte@next. - When asked
Where should we create your project?typeclient. When prompted, select the options that you want for your project. (NOTE: This repo uses TypeScript, so some of the configs in this repo are specific to TypeScript.) This will create your SvelteKit project inside a directory namedclient. - Copy the configs from this repo's
svelte.config.js,tsconfig.jsonfiles. - In your
package.jsonfile, add--hostto the end of yourdevscript so your Docker containers can communicate with each other:"dev": "svelte-kit dev --host". - Rename the
app.htmlfile that is inside the/client/srcfolder toindex.htmland move it directly inside of the/clientfolder. So its new location will be/client/index.html. - If you are using Kubernetes in your production deployments you can create an
express-web-server.jsfile directly inside the/clientfolder and copy the code from this repo'sexpress-web-server.jsfile to use as a starting point. Or you can configure Nginx as your web server. - You will have to make sure that you have any npm packages installed in your project that apply to the configurations you are using (e.g.
@sveltejs/adapter-static,express,http-proxy-middleware).
- From a terminal window,
- Server-side Setup:
- You will have to install Python on your computer, if it is not already installed.
- Create another folder inside your project folder called
server. - Copy all of the files inside the
serverdirectory in this repo into theserverdirectory of your project. - The
/server/main.pyfile has a lot of comments that might be helpful in understanding things like how requests work in an SPA app.
- Docker setup:
- You will have to install Docker on your computer, if it is not already installed.
- Copy the
Makefile,docker-compose.yml,docker-compose.dev.yml, and.dockerignorefiles from this repo's root folder into your project's root folder. - Copy the
Dockerfile.clientandDockerfile.client.devfiles from this repo'sclientfolder into your project'sclientfolder. - Copy the
Dockerfile.serverandDockerfile.server.devfiles from this repo'sserverfolder into your project'sserverfolder. - The above Docker and Docker Compose files have some comments in them that should help to clarify what the code is doing in those files.
The Makefile inside the project root direct is configured with a list of commands to start building Docker containers and to run Docker Compose in either development or production modes. If you have Make installed on your computer, then you can run commands like this: make dev-build or make dev-run. If you do not have Make installed, then you can run commands like this: docker-compose -f docker-compose.dev.yml build or docker-compose -f docker-compose.dev.yml up.
NOTES:
- Any
makeordocker-composecommands should be run from inside the project root directory. - The
Makefilein this repo is heavily commented, so you should be able to understand the commands that are listed there.
- Build the client-side code for production:
cdinto theclientdirectory and runnpm run build. That will create a new/client/builddirectory that contains bundled and optimized JavaScript, CSS, and image files. - Open the
/server/main.pyfile, scroll to the catch-all route (the last route at the bottom), comment out the return statement that begins withreturn RedirectResponse, and uncomment the return statement that begins withreturn FileResponse. - Run the Uvicorn server:
cdinto theserverdirectory and runuvicorn main:app --host=0.0.0.0 --port=8000. The server should now be serving up the client side code for the/client/builddirectory. Open a browser and navigate tolocalhost:8000. You should see the app in the browser.
The /client/svelte.config.js file is where the configurations are located for proxying frontend requests to the backend server. The main.py file also includes CORS configurations to allow requests from the frontend during development because the frontend code will run on a different port during development.