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

Docker for JavaScript Developer at React Kyiv September Meetup

Nikita Galkin
September 29, 2017

Docker for JavaScript Developer at React Kyiv September Meetup

Nikita Galkin

September 29, 2017

More Decks by Nikita Galkin

Other Decks in Programming


  1. Docker for JavaScript Developer by Nikita Galkin Sep 29, 2017

  2. 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: ▰ Contract First ▰ Technical backlog usage instructions ▰ 5 production Node.js stories ▰ Testing in Node.js World ▰ TypeScript for Node.js applications Links: Site GitHub Twitter Facebook 2
  3. 3 Some Statistics from hub.docker.com

  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? 4
  5. OS/Tools ▰ 1. Nginx ▰ 2. Redis ▰ 3. Busybox

    ▰ 4. Ubuntu ▰ 5. Alpine ▰ 6. Registry ▰ 7. MySQL ▰ 8. MongoDB ▰ ... hub.docker.com rating 5 Lagnuages ▰ 12. Node.js ▰ 19. WordPress (PHP) ▰ 20. Ruby ▰ 22. Python ▰ 23. Golang ▰ 24. PHP ▰ 27. Java ▰ ...
  6. Awesome Pair! Docker is an amazing wrapper for single process

    Node.js application. 6
  7. 7 Where to begin? Allow Docker to deliver your first

  8. ▰ 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? 8
  9. 9

  10. 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 10
  11. ▰ 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 11
  12. My Favorite Docker Images 12 ▰ 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
  13. 404 What is Docker?

  14. ▰ 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 14
  15. 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? 15
  16. Dockerfile The text file that contains all the commands, in

    order, needed to build a given image. JS analogy: Inheritance Creating Docker images 16 docker commit command This method to create an image is not reproducible and should be completely avoided. JS analogy: memory snapshot
  17. 17 Development Frontend with Docker

  18. Monolith JS application 18 ▰ Single repository for backend and

    frontend is sucks ▰ package.json is rubbish dump ▰ Frontend is building on startup or install ▰ Don’t use monolith js applications
  19. ▰ Artifacts are ▻ UI bundle for FE ▻ Docker

    image(-s) for BE ▰ Use environment variables for storing your backend host ▰ Use nginx or nginx-like tool for serve FE files ▰ Use native tools on your machine for development ▻ Node.js ▻ Webpack ▻ etc How develop FE without pain? 19
  20. 20 Meet Docker Compose!

  21. 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? 21
  22. version: '3' services: web: build: . ports: - "5000:5000" volumes:

    - .:/code - logvolume01:/var/log links: - redis redis: image: redis volumes: logvolume01: {} docker-compose.yml Example 22
  23. docker-compose.yml for Development 23 ▰ 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
  24. dotenv-safe Example 24 # 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:// 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;
  25. docker-compose.yml for Testing 25 ▰ 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
  26. My Failure with docker-compose.yml 26 ▰ 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.
  27. 27 How to Write Your Dockerfile?

  28. 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"] 28
  29. 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 29
  30. 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"] 30 Any new line is an intermediate image, which is called layer.
  31. 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 31
  32. 32 THANKS! HAPPY CODING WITH DOCKER You can find me

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