🐳 Template for a simple TypeScript-Node-Docker service with hot reloading.
Intentionally minimalistic, without any framework or additional libraries.
- Use/Pull/Fork this template
- Navigate to the folder
- Run
docker-compose up
Depending on your machine, you might experience a "ts-node" error after a fresh build. Doing step #3 once again solves the issue.
Removing
volumes
section from thedocker-compose.yml
file malfunctions hot reloading.
Automatic (hot) reloading is triggered when a .ts
file is changed locally. The exact flow is visualized below.
Adding packages to the running container does not trigger hot reloading. When adding a new package by using yarn add
, do the following steps instead:
- Stop and remove the existing container
docker-compose stop ts-service && docker-compose rm -f ts-service
In case you changed the service name in the docker-compose.yml file, replace "ts-service" with the changed name
- Run
docker-compose build ts-service
. This will install the newly added package too - Run
docker-compose up ts-service
Or in one line:
docker-compose stop ts-service && docker-compose rm -f ts-service && docker-compose build ts-service && docker-compose up ts-service
The built container in production will only contain files/folders not ignored
by the .dockerignore
file (wanted behaviour).
The docker-compose.yml
file and docker-compose
commands should not be used in production. This file's purpose is to ease the local development, including adding the hot reloading ability.
When deploying the service to production, set yarn start
as the container's "command".
Examples:
- In AWS ECS: Container Definitions -> command
- In GCP Cloud Run: Container command
Alternative:
- Remove
command: yarn dev
line fromdocker-compose.yml
and setNODE_ENV
environment variable (line #9)
...
environment:
NODE_ENV: "development"
- Add this line to the very end of the
Dockerfile
(line #6)
CMD yarn "$(if [ $NODE_ENV = 'production' ] ; then echo 'start' ; else echo 'dev'; fi)"
- When deploying to production, set
NODE_ENV
environment variable toproduction
When choosing the alternative, it is no longer needed to set "command" in your cloud provider. Setting
CMD
in the Dockerfile (step #2) does the job for you
When you encounter an error (or unexpected behaviour) that you cannot solve:
-
Remove the Docker container, image, and valumes
-
Then run
docker-compose build --no-cache ts-service
-
Lastly, run
docker-compose up ts-service
In case this does not fix the error, or if you have any additional questions, feel free to open a GitHub issue