a note on how to dockerize NodeJS tutorial source from this link
The docker container is launched based on a docker image, the docker image is created with instructions written in the Dockerfile. So we have to add Dockerfile
to the directory in the application directory.
write these set of commands on your Dockerfile
FROM node:7
# use this directory to store files, run npm, and launch our app
WORKDIR /app
# copy application to /app directory, install dependecies.
# If you add the package.json first and run npm install later, Docker won't have to install the dependencies again if you change package.json
COPY package.json /app
RUN npm install
COPY . /app
# what should be executed when the docker image is launched.
CMD node index.js
# expose port 8081 tok the outside once the container has launched
EXPOSE 8081
go up to the parent directory, then build the docker by running this command: docker build express-crud <the application directory name>
or without going up to the parent directory by running this command: docker build -t express-crud <the application directory name>
Then the docker is build and it takes several minutes, depends on your internet connection and the first time pulling images from dockerhub.
When the docker image is built, we will see that the docker is pulling images from Dockerhub.
Once the building process is completed, we can see list of pulled docker images by running this command : docker images
Now, execute this following command to launch the container and publish it on the host with the port 8081: docker run -p 8081:8081 express-crud <the application directory name>
Then, to make sure that now the container is accessable, open http://localhost:8081/
on your browser. Or, run this command curl -i localhost:8081
on the terminal.
To stop running docker container, open new shell and run this command:
docker ps # to see list of working container, and find the containerid that we want to kill
docker stop 764edda5250c # <container id>
The container process will end and your original shell will be released.
- Create account on Dockerhub, this account will also valid for Docker store.
- Verify our address and add new repository in the Dockerhub
- Run
docker images
to list all images - Tag the
oktavianidewi/docker-express-crud
using Image ID of theexpress-crud
docker tag 3101840d32af oktavianidewi/docker-express-crud:latest
then run again: docker image
and we see oktavianidewi/docker-express-crud
is tagged with the same Image ID as express-crud
5. Push our tagged image to dockerhub
docker push oktavianidewi/docker-express-crud
- Open the Dockerhub website to see the newly-pushed image. Voila, we have our local
express-crud
is now on our Dockerhub repository, which can be pulled anytime with nameoktavianidewi/docker-express-crud
. - Remove all versions of
express-crud
image on our local system.
# <Image ID of `express-crud`>
docker rmi -f 3101840d32af
- Then, when we run
docker run oktavianidewi/docker-express-crud
, it will automatically download/pull the image that is not yet exist on our local system.
tutorial on writing a nodejs with typescript but with express. tutorial source from this link
- Install typescript
npm install -g typescript
- Create lib directory to store all typescript files
- Install the express data types for Typescript
npm install --save @types/express
- Install express and body-parser
npm install --save express body-parser
. Bosy-parser is a middleware to parse all incoming request bodies to JSON as well as outgoing responses. - Install ts-node
npm install --save ts-node
module to check the ts file has error or not, before all files are compiled to javascript. In production we want to compile everything to JavaScript and run it with the node command. - We should run
tsc
to compile all typescript files to javascript. But before that, we have to createtsconfig.json
file. This can be generated by running this commandtsc --init
- Set up the
tsconfig.json
file, for example:
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es5", "es6", "dom"],
"moduleResolution": "node",
"pretty": true,
"sourceMap": true,
"target": "es6",
"outDir": "./dist",
"baseUrl": "./lib"
},
"include": [
"lib/**/*.ts"
],
"exclude": [
"node_modules"
]
}
- Run
tsc
. Then we will see the converten .js files on./dist
directory. - To automate script, open
package.json
and underscript
section, add these lines of code:
"scripts": {
"build": "tsc",
"dev": "ts-node ./lib/server.ts",
"start": "node ./dist/server.js",
"prod": "npm run build && npm start"
}
- We can run the program using
npm run-script prod
to build the typescript files and run the compiled./dist/server.js
TBC