Skip to content

Instantly share code, notes, and snippets.

@oktavianidewi
Last active September 12, 2019 21:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save oktavianidewi/7e4a2d83c08acdd7e080f1f3cc63f71c to your computer and use it in GitHub Desktop.
Save oktavianidewi/7e4a2d83c08acdd7e080f1f3cc63f71c to your computer and use it in GitHub Desktop.
Dockerize simple API from Node-JS

Dockerize NodeJS

a note on how to dockerize NodeJS tutorial source from this link

Write Dockerfile and Build Docker Locally

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. 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.

Sharing Docker Image through Dockerhub

  1. Create account on Dockerhub, this account will also valid for Docker store.
  2. Verify our address and add new repository in the Dockerhub
  3. Run docker images to list all images
  4. Tag the oktavianidewi/docker-express-crud using Image ID of the express-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
  1. 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 name oktavianidewi/docker-express-crud.
  2. Remove all versions of express-crud image on our local system.
# <Image ID of `express-crud`>
docker rmi -f 3101840d32af 
  1. 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.

a simple API with NodeJS, Express and Typescript

tutorial on writing a nodejs with typescript but with express. tutorial source from this link

  1. Install typescript npm install -g typescript
  2. Create lib directory to store all typescript files
  3. Install the express data types for Typescript npm install --save @types/express
  4. 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.
  5. 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.
  6. We should run tsc to compile all typescript files to javascript. But before that, we have to create tsconfig.json file. This can be generated by running this command tsc --init
  7. 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"
  ]
}
  1. Run tsc. Then we will see the converten .js files on ./dist directory.
  2. To automate script, open package.json and under script 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"
}
  1. We can run the program using npm run-script prod to build the typescript files and run the compiled ./dist/server.js

Deploy API from NodeJS to Docker

TBC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment