Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Docker for Node.js developer at VinnytsiaJS 2017

Docker for Node.js developer at VinnytsiaJS 2017

The main talk focus is how to use Docker for your local development environment. Also, we will cover a bit QA and production environment. We will describe Docker best practices in all processes including design, development, testing, debugging and deploy.

VinnytsiaJS site

Nikita Galkin

August 05, 2017
Tweet

More Decks by Nikita Galkin

Other Decks in Programming

Transcript

  1. Nikita Galkin Love and Know: ▰ JavaScript, Node.js, OpenSource, TypeScript,

    Docker, AWS ▰ How to split monolith into microservices Believe that: ▰ Any problem must be solved at the right level ▰ Most of the difficulties are with people, not with technologies ▰ A problem should be highlighted, an idea should be "sold", a solution should be demonstrated Last Talks: ▰ 5 production Node.js stories ▰ Testing in Node.js World ▰ TypeScript for Node.js applications ▰ Spec driven development in Microservices Links: Site GitHub Twitter Facebook 2
  2. Hard ▰ Base FE or Mobile ▰ DataBases ▰ Base

    Linux + DevOps ▰ Protocols (HTTP, bin, WS) ▰ Utils (git, docker, etc) ▰ StackOverflowDD Node.js Developer Requirements Soft ▰ English ▰ Ability to work in a team ▰ Motivation to teach technology ▰ Adequacy 4
  3. ▰ Salary level is similar to Java Developer's ▰ Less

    experience ▰ Often moved from Front-end development ▰ Technology stack is changing very quickly ▰ Lack of academic education Portrait of Ukrainian Node.js Developer 5
  4. ▰ Public storage for Docker images ▰ Contains official and

    unofficial images for any OS, tools, etc ▰ There are free public and paid private accounts ▰ Default registry for Docker ▰ In short: analogy of npmjs.com What is hub.docker.com? 8
  5. OS/Tools ▰ 1. Nginx ▰ 2. Redis ▰ 3. Busybox

    ▰ 4. Ubuntu ▰ 5. Alpine ▰ 6. Registry ▰ 7. MySQL ▰ 8. MongoDB ▰ ... hub.docker.com rating 9 Lagnuages ▰ 12. Node.js ▰ 19. WordPress (PHP) ▰ 20. Ruby ▰ 22. Python ▰ 23. Golang ▰ 24. PHP ▰ 27. Java ▰ ...
  6. ▰ Install Docker for your OS. It’s really easy! ▰

    Choose any useful tool what you want to test. ▰ If native install of the tool is boring or long or will pollute your computer, then it will make good case for docker usage. ▰ Use kitematic or search hub.docker.com ▰ Check the first results! How to Start Working with Docker? 12
  7. 13

  8. This is a sad story. My first minecraft world was

    lost with a docker container. I changed the server to another image and remove the container. I didn’t know about volume. I used latest tag. Minecraft Story 14
  9. ▰ Don’t store data in containers, use volumes ▰ volumes

    are read-write by default, but there's an :ro flag ▰ Use port mappings ▰ Don’t use config for application inside docker, use environment variables ▰ Use kitematic, but know terminal docker commands Use Docker Container Right 15
  10. My Favorite Docker Images 16 ▰ DBs (MySQL, MongoDB, etc)

    ▰ Bus (Redis, RabbitMQ) ▰ Nginx or httpd ▰ Jenkins our CI ▰ Mailhog Web and API based SMTP testing ▰ Verdaccio Lightweight private npm proxy registry ▰ Dredd Abao contract testing
  11. ▰ Containers are an abstraction at the app layer, which

    is running as an isolated process (maybe with children processes). ▰ Images are frozen immutable snapshots of live containers. ▰ Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. ▰ Read in the glossary. Terminology 18
  12. Steps ▰ CLI command docker image ls ▰ CLI tool

    connects to docker daemon throw REST API for remote or throw socket for local ▰ the daemon returns all known images How Does Docker Work? 19
  13. Dockerfile The text file that contains all the commands, in

    order, needed to build a given image. JS analogy: Inheritance Creating Docker images 20 docker commit command This method to create an image is not reproducible and should be completely avoided. JS analogy: memory snapshot
  14. const Docker = require('dockerode'); const docker = new Docker({socketPath: '/var/run/docker.sock'

    }); const container = docker.getContainer( '71501a8ab0f8' ); container.start((err, data) => console.log(data)); How to Use Docker from Node.js 21 Example with dockerode socket&REST API wrapper for Docker
  15. Dockerfile Example for Modern Node.js App FROM node:8.2.1-alpine USER nodejs

    RUN mkdir /microservice WORKDIR /microservice COPY package.json . COPY package-lock.json . RUN npm install --production ADD . /microservice EXPOSE 8080 ENTRYPOINT ["npm", "start"] 23
  16. My first experiment with Dockerfile development was finished in 2

    hours. There was not enough free space left. Docker took the whole space. What did I miss? Docker VS 128GB SSD 24
  17. Layers FROM node:8.2.1-alpine USER nodejs RUN mkdir /microservice WORKDIR /microservice

    COPY package.json . COPY package-lock.json . RUN npm install --production ADD . /microservice EXPOSE 8080 ENTRYPOINT ["npm", "start"] 25 Any new line is an intermediate image, which is called layer.
  18. Which Base Image to Choose? Full Built from Debian Linux.

    There are significant number of tools, including git, g++, imagemagick,etc. Size more 650MB Slim Built from Debian Linux. There are not such tools as in Full image. Size starts from 250MB Alpine Build from Alpine Linux, the distribution that was almost purpose-built for Docker images and other small, container-like uses Size starts from 5MB 26
  19. How to Debug Node.js App Inside Docker? 28 ▰ Don’t

    use --inspect, send SIGUSR1 Example: docker kill --signal="SIGUSR1" <container name or id> ▰ Expouse 9229 port (default debug port) ▰ Setup your debugger tool, I prefer Chrome with NiM
  20. Compose is a tool for defining and running multi-container Docker

    applications in docker-compose.yml docker-compose.yml describes: ▰ services ▰ networks ▰ volumes ▰ configs What is Docker Compose? 30
  21. version: '3' services: web: build: . ports: - "5000:5000" volumes:

    - .:/code - logvolume01:/var/log links: - redis redis: image: redis volumes: logvolume01: {} docker-compose.yml Example 31
  22. docker-compose.yml for Development 32 ▰ All services needed for your

    application are described in docker-compose.yml and started as containers ▰ The application is configured with environment variables ▰ [Advice] Use dotenv-safe package, then ▰ [Advice] Described the list of required for environment variables in .env.example ▰ [Advice] Use native Node.js development with .env file
  23. dotenv-safe Example 33 # Default log level LOG_LEVEL=info # Graceful

    shutdown timeout in milliseconds SHUTDOWN_TIMEOUT=1000 # HTTP Boundary port HTTP_PORT=8000 # Temporary Redis connection string REDIS_URI=redis://127.0.0.1:6379 require('dotenv-safe').load(); const logger = require('./logger'); logger.levels('stdout', process.env.LOG_LEVEL); const settings = { shutdownTimeout: process.env.SHUTDOWN_TIMEOUT, http: { port: process.env.HTTP_PORT, api: { prefix: process.env.HTTP_API_PREFIX, }, }, redis: { uri: process.env.REDIS_URI, }, }; module.exports = settings;
  24. docker-compose.yml for Testing 34 ▰ Change entrypoint in docker-compose from

    npm start to npm test ▰ Use --abort-on-container-exit and --exit-code-from ▰ Example docker build -t $name:$version . || exit 1 docker-compose up --abort-on-container-exit --exit-code-from $name; export TEST_RESULT=$? docker-compose down exit $TEST_RESULT
  25. My Failure with docker-compose.yml 35 ▰ There was an interesting

    project with parsing SPA sites. ▰ I chose docker-compose with Selenium Grid ▰ Customer asked to deploy the solution to his server without docker-compose. The deploy was PITA. ▰ Project was failed. There was no technical expertise in the customer company for supporting and increasing Selenium Grid.
  26. Know your Pipelines Docker isn’t the hype anymore. You can

    use Docker during development. But not every company has processes with modern stack. Know your technical stack, your processes. 36
  27. 37 THANKS! HAPPY CODING WITH DOCKER AND NODE.JS You can

    find me at twitter as @galk_in Slides are available at speakerdeck.com/galkin or at my site galk.in