WHAT: Run your prisma service as a docker container with the prisma engine and MySQL Database.
WHY: There are 3 services that must work together. Running them in docker required some magic.
WHO: Anyone using Prisma & MySQL
yarn install && yarn start
See Build Scripts for options.
Directory | Description |
---|---|
src/service |
Our service which utilizes Prisma |
src/database |
MySQL |
Purpose | URI | Port |
---|---|---|
Prisma Service | local | 4000 |
Prisma ORM Engine | local | 4466 |
Database | local | 3306 |
These are scripts which are intended to be run from your terminal
-
start
Build docker-compose.all.yml (ie. production version).
Jump to Start Options for more info on what
start
does -
dev
Build docker-compose.db.yml (ie. development version).
Jump to Start Options for more info on what
dev
does -
build
This is the default entry point for rebuilding. By default, it's set to "docker:rebuild:all" because start is set to "start:docker:all". Build and start should match. ...is there a cleaner way to do this? 🤔
-
clean
This removes all containers' with "prisma" in its name (ie. all prisma_starter containers are destroyed).
-
test
This runs docker-compose all & runs ava in --watch mode. Useful when you want to update your tests, but useful when you want to update your tests. Unfortunately, no docker HMR right now so updating source won't work as expected.
Start Options are called by start -- Terminal Scripts
-
start:docker:db
This runs Prisma Engine (:4466) and MySQL (:3306) in docker. Meanwhile, it runs your service locally. Use this option for local development.
-
start:docker:all
This runs Prisma Engine (:4466), MySQL (:3306) and your Node service (:4000) in Docker containers
Build Options are called by build -- Terminal Scripts
-
docker:rebuild:db
This rebuilds database/docker-compose.db.yml.
-
docker:rebuild:all
This rebuilds docker-compose.all.yml
You'll notice in .env that process.env.PRISMA_ENDPOINT is set to http://localhost:4466
. However, in index.js the Prisma server is initialized with http://prisma:4466
This is necessary when using docker-compose to containerize our prisma service (ie. src/service/...) along with the prisma engine (ORM layer which translates between prisma queries and mysql) & mysql database (persistent storage).
Why? Short answer is because I'm not using Linux (I use Mac). The type of network you need to map Docker's localhost to your host's (ie. your laptop's) localhost is only available on Linux. I did find a 3rd party solution but decided to not go that route.
If you'd like to explore more, you can peruse my notes on the issue here
In case you're wondering, the demo below was captured after running
npm run start:docker:db
. However, from the browser, this is functionally identical to runningnpm run start:docker:all
open localhost:4000 && open localhost:4466
More gifs and screenshots available here: demo.md