This repository is an application template using Docker with Alpine Linux, Node LTS, and Typescript. It starts and exposes a web server on port 5000
that returns a hello world HTML page for all requests.
-
Install Docker, if you have not already done so.
-
Clone or download this repository. If starting a new project, you may want to use degit to clone and strip the git history:
npx degit valtech-sd/ts-docker-starter ./my-new-docker-app
-
From the root of this repository, before running the container for the first time:
- You may need to build its image locally with
docker build .
. - Install the node modules with
npm i
. (Docker currently pulls in the whole local repository folder as a mounted volume; this practice might change in the future for deploying to servers.) - Generate the
.js
files from Typescript by runningnpm run build
.
- You may need to build its image locally with
-
Start Docker with
docker-compose run --rm --service-ports node
--rm
removes the container after it runs, to clean up--service-ports
makes sure that the port-forwarding works (it does not forward by default)- starts the
node
service in thedocker-compose.yml
and runs thenpm start
script
We have a simple Dockerfile
and use docker-compose.yml
to put most command line parameters into YAML configuration instead.
This repository uses the official Node Docker image for LTS on Alpine Linux. There are a few issues associated with this image (it uses a different libc
module — musl libc
instead of glibc
), and the Node team provides a set of best practices that we have read and should follow on projects.
-
✔️ Run as
node
user, instead ofroot
. -
✔️ Use environment variable
NODE_ENV=production
. -
✔️ Put global dependencies in non-root user directory.
-
... Limit how much memory the container consumes.
-
... Run Docker Bench for Security audit script.
-
... Bake startup script directly into Dockerfile with
CMD ["node","index.js"]
(or use
init
).
See docker-compose.yml comments.
Note: If you are using VSCode, there is an official Docker extension to make it easier to manage your containers.
There is a tsconfig.json
in the root directory. Per TypeScript's defaults, we
- keep source
.ts
files in./src
- generate
.js
files in./built
.
To rebuild, type npm run build
, which will
- run
rimraf
on the./built
folder (clears everything with arm -rf
) - build with
tsc
.eslintrc
is the JSON configuration file for ESLint created by running npx eslint --init
. It should be set up with TypeScript formatting, and correct double-quotes to single quotes for consistency.
.prettierrc
is the JSON configuration file for Prettier. Currently, it only enforces single quotes.
Tests are configured for mocha
. Node looks in the standard ./test
folder for tests.
mocha
and the@types/mocha
type definitions are dev dependencies.- There is an
npm run test
script that callsmocha
and registers it withts-node
so that tests can be written in TypeScript.
Note: If you're using VSCode, there is a
.vscode/launch.json
set up for easier debugging.
- Refine this repository as our needs and understanding of Docker grow.
- Determine our best practice for mounting data volumes. (Here is a potential issue to watch out for.)