Commands:

  • Up: dockder-compose up:
    • How it works:
      • Default file name: docker-compose.yml and can be specified by “-f” option
      • Build images: not rebuild any images if already existed
      • Start services with configured networks, volumes, environments, …
    • Available options:
      • --build: Build images before starting containers
      • -d, --detach: Detached mode
  • Down: docker-compose down
    • How it works:
      • Stop and remove all containers and internal networks (defined in yaml file or scope network)
      • Images and volumes are not deleted by default but can do with options bellow
    • Available options:
      • --rmi type: Remove images
      • -v, --volumes: Remove named volumes declared in the volumes

Docker compose for nodejs application

Good Tips

  1. COPY, not ADD: The same functionality but ADD should only use for specically functional that COPY dose not support (such as add from URL, …)
  2. npm/yarn install during build, remember to clear npm cache: To simplify the output image
  3. CMD node, not npm. e.g: CMD [“node”, “.”] is much better than CMD [“npm”, “start”]: because npm is another node, if use it, we have multiple node processes and the main app will be a sub-process in the npm node
  4. Use WORKDIR instead of RUN mkdir: because WORKDIR can create dir if not exist with the correct owner

Permission

  1. Docker always run a new container with the default user is root
  2. WORKDIR always create new folder with user root
  3. Official images of node always provide a user name: node
  4. To change the user to node user, we use USER node -> it will apply to all next commands in yaml and the default user when use EXEC command from outside (e.g: docker container exec xxx; docker-compose exec xxx) to execute with specifying user, we can use flag -u
  5. Should not run node app with the root permission -> to reduce security risks inside the container
  6. Should grant the right permission (owner, …) to the app resources folder -> node user

Be careful with Docker cache layer

  1. Use apt-get in a single line with && operator
  2. Copy package.json alone (maybe with package-lock) and npm install follow -> to create separate cache layers -> optimize the docker caching

LEAVE A REPLY

Please enter your comment!
Please enter your name here