SlideShare a Scribd company logo
Bret Fisher
Docker Captain, DevOps Dude
bretfisher.com/docker
Node.js Rocks in Docker
Who's This Session For?
•You know some Node
•You know some Docker
•You want more Node+Docker awesomesauce
What We Gonna' Learn Bret?
•Node Dockerfile Best Practices
•Make a real-world multi-stage Dockefile
•Build with auditing and sec scans
•Proper Node shutdown
•Node HTTP connection management
Node Dockerfiles
Every Node Sample Dockerfile
Node Base Image Guidelines
•Stick to even numbered major releases
•Don't use :latest tag
•Start with Debian if migrating
•Use stretch (default) not jessie
•Try slim first
•Move to Alpine later, maybe
When to use Alpine Images
•Alpine is "small" and "sec focused"
•But Debian/Ubuntu are smaller now too
•~100MB space savings isn't significant
•Alpine has its own issues
•Alpine CVE scanning fails
•Enterprises may require CentOS or
Ubuntu/Debian
Image Sizes for node/slim/alpine
Image Sizes for node/slim/alpine
node_modules in Images
•Problem: we shouldn't build images with
node_modules from host
•Example: node-gyp
•Solution: add node_modules
to .dockerignore
•copy .gitignore?
Least Privilege: Using node User
•Official node images have a node user
•But it’s not used until you USER node
•Do this after apt/apk and npm i -g
•Do this before npm i
•May cause permissions issues with write
access
•May require chown node:node
0.Dockerfile
1.Dockerfile
Process
Management and
Shutdown
Node Process Management In Containers
•No need for nodemon, forever, or pm2 on server
•We'll use nodemon in dev for file watch later
•Docker manages app start, stop, restart,
healthcheck
•Node multi-thread: Docker manages multiple
“replicas”
•One npm/node problem: They don’t listen for
proper shutdown signal by default
The Truth About The PID 1 Problem
•PID 1 (Process Identifier) is the first
process in a system (or container) (AKA init)
•Init process in a container has two jobs:
• reap zombie processes
• pass signals to sub-processes
•Zombie not a big Node issue
•Focus on proper Node shutdown
Proper CMD for Healthy Shutdown
•Docker uses Linux signals to stop app
(SIGINIT/SIGTERM/SIGKILL)
•SIGINIT/SIGTERM allow graceful stop
•npm doesn't respond to SIGINIT/SIGTERM
•node doesn't respond by default, but can with
code
•Docker provides a init PID 1 replacement
option
Proper Node Shutdown Options
•Temp: Use --init to fix ctrl-c for now
•Workaround: add tini to your image
•Production: your app captures SIGINIT
for proper exit
Example init command
•Run any node app with --init to handle
signals (temp solution)
>docker run --init -d nodeapp
Example tini Dockerfile
•Add tini to your Dockerfile, then use
it in CMD (permanent workaround)
>RUN apk add --no-cache tini
>ENTRYPOINT ["/sbin/tini", "--"]
>CMD ["node", "./bin/www"]
1.Dockerfile
2.Dockerfile
Example SIGINIT Capture
•Used to track HTTP connections and send
them FIN packets when Node shuts down
>https://github.com/hunterloftis/stoppable
Better: Connection Tracking
Multi-stage Builds
•Build multiple images from one file
•Those images can FROM each other
•COPY files between them
•Space + security benefits
•Great for "artifact only"
•Great for dev + test + prod
Avoiding devDependencies In Prod
•Multi-stage can solve this
•prod stages: npm i --only=production
•Dev stage: npm i --only=development
•Optional: Use npm ci to speed up builds
•Ensure NODE_ENV is set
2.Dockerfile
3.Dockerfile
Building A Specific Stage
•To build dev image from dev (last) stage
>docker build -t myapp .
•To build prod image from prod stage
>docker build -t myapp:prod --target prod .
More Multi-stage: test
•Add a test stage that runs npm test
•Have CI build --target test stage
before building prod
•Don’t COPY code into dev stage
•Keep it DRY (for COPY and RUN)
3.Dockerfile
4.Dockerfile
4.Dockerfile
Security Scanning and Audit
•Create audit stage for optional build
•Consider RUN npm audit
•Consider CVE scanner
•Only report at first, no failing (most
images have at least one CVE vuln)
4.Dockerfile
4.Dockerfile
5.Dockerfile
Got Compose?
Compose YAML v2 vs v3
•Myth busting: v3 does not replace v2
•v2 focus: single-node dev/test
•v3 focus: multi-node orchestration
•If not using Swarm/Kubernetes, stick to
v2
Every Node Sample Compose
node_modules in Bind-Mounts
•Problem: we can't just bind-mount
node_modules content from host on
macOS/Windows (different arch)
•Two Potential Solutions
node_modules in Bind-Mounts
•Solution 1, common but less flexible:
•Bind-mount /app which includes modules
•You can't docker-compose up until
you've used docker-compose run
•node_modules on host is now only
usable from container
•Never npm install from host
node_modules in Bind-Mounts
•Solution 2, more complex but flexible:
•Move node_modules up a directory in
Dockerfile
•Use empty volume to hide node_modules
on bind-mount
•node_modules on host doesn't conflict
Bind-Mounting: Performance
•On Linux, bind-mounts are native
•On macOS add delegated write mode
•Slower in Windows, mounting across
Samba/SMB
•Consider file sync if it gets real bad
•Or WSL + Docker
0.docker-compose.yml
1.docker-compose.yml
File Monitoring and Node Auto Restarts
•Use nodemon for compose file monitoring
•webpack-dev-server, etc. work the same
•If Windows, enable polling
•Create a nodemon.json for advanced
workflows (bower, webpack, parcel)
Startup Order and Dependencies
•Problem: Multi-service apps start out
of order, node might exit or cycle
•Multi-container dependencies need:
•Name resolution (DNS)
•Connection failure handling
Dependency Awareness
•depends_on: service A needs service B
•Fixes name resolution issues with
"can't resolve <service_name>"
•Only for compose, not Orch
•compose YAML v2: works with
healthchecks like a "wait for script"
1.docker-compose.yml
2.docker-compose.yml
Production Checklist
•CMD node directly
•Build with .dockerignore
•capture SIGTERM, properly shutdown
•npm ci or npm i --only=production
•Scan/audit/test during builds
•Healthchecks (readiness/liveness)
Thanks!
bretfisher.com/docker
@Bretfisher
bretfisher.com/node
New Docker for Node.js course

More Related Content

What's hot

Introduction to Docker at SF Peninsula Software Development Meetup @Guidewire
Introduction to Docker at SF Peninsula Software Development Meetup @GuidewireIntroduction to Docker at SF Peninsula Software Development Meetup @Guidewire
Introduction to Docker at SF Peninsula Software Development Meetup @Guidewire
dotCloud
 
Dockerize the World - presentation from Hradec Kralove
Dockerize the World - presentation from Hradec KraloveDockerize the World - presentation from Hradec Kralove
Dockerize the World - presentation from Hradec Kralove
damovsky
 
Docker by Example - Basics
Docker by Example - Basics Docker by Example - Basics
Docker by Example - Basics
Ganesh Samarthyam
 
From development environments to production deployments with Docker, Compose,...
From development environments to production deployments with Docker, Compose,...From development environments to production deployments with Docker, Compose,...
From development environments to production deployments with Docker, Compose,...
Jérôme Petazzoni
 
Introduction to dockerfile, SF Peninsula Software Development Meetup @Guidewire
Introduction to dockerfile, SF Peninsula Software Development Meetup @Guidewire Introduction to dockerfile, SF Peninsula Software Development Meetup @Guidewire
Introduction to dockerfile, SF Peninsula Software Development Meetup @Guidewire
dotCloud
 
Dockercon EU 2014
Dockercon EU 2014Dockercon EU 2014
Dockercon EU 2014
Rafe Colton
 
Dockerin10mins
Dockerin10minsDockerin10mins
Dockerin10mins
Dawood M.S
 
Automating Docker Containers with Puppet 2014 10-13
Automating Docker Containers with Puppet 2014 10-13Automating Docker Containers with Puppet 2014 10-13
Automating Docker Containers with Puppet 2014 10-13
kylog
 
Locally it worked! virtualizing docker
Locally it worked! virtualizing dockerLocally it worked! virtualizing docker
Locally it worked! virtualizing docker
Sascha Brinkmann
 
Microservices using relocatable Docker containers
Microservices using relocatable Docker containersMicroservices using relocatable Docker containers
Microservices using relocatable Docker containers
Mauricio Garavaglia
 
Joomla Continuous Delivery with Docker
Joomla Continuous Delivery with DockerJoomla Continuous Delivery with Docker
Joomla Continuous Delivery with Docker
Jirayut Nimsaeng
 
Taking Docker to Production: What You Need to Know and Decide
Taking Docker to Production: What You Need to Know and DecideTaking Docker to Production: What You Need to Know and Decide
Taking Docker to Production: What You Need to Know and Decide
Docker, Inc.
 
Docker worshop @Twitter - How to use your own private registry
Docker worshop @Twitter - How to use your own private registryDocker worshop @Twitter - How to use your own private registry
Docker worshop @Twitter - How to use your own private registry
dotCloud
 
Docker Presentation at the OpenStack Austin Meetup | 2013-09-12
Docker Presentation at the OpenStack Austin Meetup | 2013-09-12Docker Presentation at the OpenStack Austin Meetup | 2013-09-12
Docker Presentation at the OpenStack Austin Meetup | 2013-09-12
dotCloud
 
Basic docker for developer
Basic docker for developerBasic docker for developer
Basic docker for developer
Weerayut Hongsa
 
Docker Continuous Delivery Workshop
Docker Continuous Delivery WorkshopDocker Continuous Delivery Workshop
Docker Continuous Delivery Workshop
Jirayut Nimsaeng
 
Continuous delivery with jenkins, docker and exoscale
Continuous delivery with jenkins, docker and exoscaleContinuous delivery with jenkins, docker and exoscale
Continuous delivery with jenkins, docker and exoscale
Julia Mateo
 
Docker introduction
Docker introductionDocker introduction
Docker introduction
dotCloud
 
Docker for Devs - John Zaccone, IBM
Docker for Devs - John Zaccone, IBMDocker for Devs - John Zaccone, IBM
Docker for Devs - John Zaccone, IBM
Docker, Inc.
 
Dockerfile
Dockerfile Dockerfile
Dockerfile
Jeffrey Ellin
 

What's hot (20)

Introduction to Docker at SF Peninsula Software Development Meetup @Guidewire
Introduction to Docker at SF Peninsula Software Development Meetup @GuidewireIntroduction to Docker at SF Peninsula Software Development Meetup @Guidewire
Introduction to Docker at SF Peninsula Software Development Meetup @Guidewire
 
Dockerize the World - presentation from Hradec Kralove
Dockerize the World - presentation from Hradec KraloveDockerize the World - presentation from Hradec Kralove
Dockerize the World - presentation from Hradec Kralove
 
Docker by Example - Basics
Docker by Example - Basics Docker by Example - Basics
Docker by Example - Basics
 
From development environments to production deployments with Docker, Compose,...
From development environments to production deployments with Docker, Compose,...From development environments to production deployments with Docker, Compose,...
From development environments to production deployments with Docker, Compose,...
 
Introduction to dockerfile, SF Peninsula Software Development Meetup @Guidewire
Introduction to dockerfile, SF Peninsula Software Development Meetup @Guidewire Introduction to dockerfile, SF Peninsula Software Development Meetup @Guidewire
Introduction to dockerfile, SF Peninsula Software Development Meetup @Guidewire
 
Dockercon EU 2014
Dockercon EU 2014Dockercon EU 2014
Dockercon EU 2014
 
Dockerin10mins
Dockerin10minsDockerin10mins
Dockerin10mins
 
Automating Docker Containers with Puppet 2014 10-13
Automating Docker Containers with Puppet 2014 10-13Automating Docker Containers with Puppet 2014 10-13
Automating Docker Containers with Puppet 2014 10-13
 
Locally it worked! virtualizing docker
Locally it worked! virtualizing dockerLocally it worked! virtualizing docker
Locally it worked! virtualizing docker
 
Microservices using relocatable Docker containers
Microservices using relocatable Docker containersMicroservices using relocatable Docker containers
Microservices using relocatable Docker containers
 
Joomla Continuous Delivery with Docker
Joomla Continuous Delivery with DockerJoomla Continuous Delivery with Docker
Joomla Continuous Delivery with Docker
 
Taking Docker to Production: What You Need to Know and Decide
Taking Docker to Production: What You Need to Know and DecideTaking Docker to Production: What You Need to Know and Decide
Taking Docker to Production: What You Need to Know and Decide
 
Docker worshop @Twitter - How to use your own private registry
Docker worshop @Twitter - How to use your own private registryDocker worshop @Twitter - How to use your own private registry
Docker worshop @Twitter - How to use your own private registry
 
Docker Presentation at the OpenStack Austin Meetup | 2013-09-12
Docker Presentation at the OpenStack Austin Meetup | 2013-09-12Docker Presentation at the OpenStack Austin Meetup | 2013-09-12
Docker Presentation at the OpenStack Austin Meetup | 2013-09-12
 
Basic docker for developer
Basic docker for developerBasic docker for developer
Basic docker for developer
 
Docker Continuous Delivery Workshop
Docker Continuous Delivery WorkshopDocker Continuous Delivery Workshop
Docker Continuous Delivery Workshop
 
Continuous delivery with jenkins, docker and exoscale
Continuous delivery with jenkins, docker and exoscaleContinuous delivery with jenkins, docker and exoscale
Continuous delivery with jenkins, docker and exoscale
 
Docker introduction
Docker introductionDocker introduction
Docker introduction
 
Docker for Devs - John Zaccone, IBM
Docker for Devs - John Zaccone, IBMDocker for Devs - John Zaccone, IBM
Docker for Devs - John Zaccone, IBM
 
Dockerfile
Dockerfile Dockerfile
Dockerfile
 

Similar to Node.js Rocks in Docker for Dev and Ops

Docker based-Pipelines with Codefresh
Docker based-Pipelines with CodefreshDocker based-Pipelines with Codefresh
Docker based-Pipelines with Codefresh
Codefresh
 
Docker at MoneyBird
Docker at MoneyBirdDocker at MoneyBird
Docker at MoneyBird
Edwin Vlieg
 
Real-World Docker: 10 Things We've Learned
Real-World Docker: 10 Things We've Learned  Real-World Docker: 10 Things We've Learned
Real-World Docker: 10 Things We've Learned
RightScale
 
Journey to Docker Production: Evolving Your Infrastructure and Processes - Br...
Journey to Docker Production: Evolving Your Infrastructure and Processes - Br...Journey to Docker Production: Evolving Your Infrastructure and Processes - Br...
Journey to Docker Production: Evolving Your Infrastructure and Processes - Br...
Docker, Inc.
 
Docker based-pipelines
Docker based-pipelinesDocker based-pipelines
Docker based-pipelines
DevOps.com
 
DCEU 18: Building Your Development Pipeline
DCEU 18: Building Your Development PipelineDCEU 18: Building Your Development Pipeline
DCEU 18: Building Your Development Pipeline
Docker, Inc.
 
Docker crash course
Docker crash courseDocker crash course
Docker crash course
nispas
 
Unikernel User Summit 2015: Getting started in unikernels using the rump kernel
Unikernel User Summit 2015: Getting started in unikernels using the rump kernelUnikernel User Summit 2015: Getting started in unikernels using the rump kernel
Unikernel User Summit 2015: Getting started in unikernels using the rump kernel
The Linux Foundation
 
Docker for everything
Docker for everythingDocker for everything
Docker for everything
Tim Haak
 
Использование AzureDevOps при разработке микросервисных приложений
Использование AzureDevOps при разработке микросервисных приложенийИспользование AzureDevOps при разработке микросервисных приложений
Использование AzureDevOps при разработке микросервисных приложений
Vitebsk Miniq
 
Dockerize All The Things
Dockerize All The ThingsDockerize All The Things
Dockerize All The Things
Chris Tankersley
 
Docker 101: An Introduction
Docker 101: An IntroductionDocker 101: An Introduction
Docker 101: An Introduction
POSSCON
 
Justin Cormack - The 10 Container Security Tricks That Will Help You Sleep At...
Justin Cormack - The 10 Container Security Tricks That Will Help You Sleep At...Justin Cormack - The 10 Container Security Tricks That Will Help You Sleep At...
Justin Cormack - The 10 Container Security Tricks That Will Help You Sleep At...
Codemotion
 
SenchaCon 2016: Develop, Test & Deploy with Docker - Jonas Schwabe
SenchaCon 2016: Develop, Test & Deploy with Docker - Jonas Schwabe SenchaCon 2016: Develop, Test & Deploy with Docker - Jonas Schwabe
SenchaCon 2016: Develop, Test & Deploy with Docker - Jonas Schwabe
Sencha
 
Using Minikube for Node.js development
Using Minikube for Node.js developmentUsing Minikube for Node.js development
Using Minikube for Node.js development
Troy Connor
 
Consuming Cinder from Docker
Consuming Cinder from DockerConsuming Cinder from Docker
Consuming Cinder from Docker
Tesora
 
Continuous Deployment with Kubernetes, Docker and GitLab CI
Continuous Deployment with Kubernetes, Docker and GitLab CIContinuous Deployment with Kubernetes, Docker and GitLab CI
Continuous Deployment with Kubernetes, Docker and GitLab CI
alexanderkiel
 
vodQA(Pune) 2018 - Docker in Testing
vodQA(Pune) 2018 - Docker in TestingvodQA(Pune) 2018 - Docker in Testing
vodQA(Pune) 2018 - Docker in Testing
vodQA
 
Docker + jenkins in the enterprise (3)
Docker + jenkins in the enterprise (3)Docker + jenkins in the enterprise (3)
Docker + jenkins in the enterprise (3)
Kurt Madel
 
Docker Meetup Rosenheim: Package & deploy Microservices
Docker Meetup Rosenheim: Package & deploy MicroservicesDocker Meetup Rosenheim: Package & deploy Microservices
Docker Meetup Rosenheim: Package & deploy Microservices
Nico Meisenzahl
 

Similar to Node.js Rocks in Docker for Dev and Ops (20)

Docker based-Pipelines with Codefresh
Docker based-Pipelines with CodefreshDocker based-Pipelines with Codefresh
Docker based-Pipelines with Codefresh
 
Docker at MoneyBird
Docker at MoneyBirdDocker at MoneyBird
Docker at MoneyBird
 
Real-World Docker: 10 Things We've Learned
Real-World Docker: 10 Things We've Learned  Real-World Docker: 10 Things We've Learned
Real-World Docker: 10 Things We've Learned
 
Journey to Docker Production: Evolving Your Infrastructure and Processes - Br...
Journey to Docker Production: Evolving Your Infrastructure and Processes - Br...Journey to Docker Production: Evolving Your Infrastructure and Processes - Br...
Journey to Docker Production: Evolving Your Infrastructure and Processes - Br...
 
Docker based-pipelines
Docker based-pipelinesDocker based-pipelines
Docker based-pipelines
 
DCEU 18: Building Your Development Pipeline
DCEU 18: Building Your Development PipelineDCEU 18: Building Your Development Pipeline
DCEU 18: Building Your Development Pipeline
 
Docker crash course
Docker crash courseDocker crash course
Docker crash course
 
Unikernel User Summit 2015: Getting started in unikernels using the rump kernel
Unikernel User Summit 2015: Getting started in unikernels using the rump kernelUnikernel User Summit 2015: Getting started in unikernels using the rump kernel
Unikernel User Summit 2015: Getting started in unikernels using the rump kernel
 
Docker for everything
Docker for everythingDocker for everything
Docker for everything
 
Использование AzureDevOps при разработке микросервисных приложений
Использование AzureDevOps при разработке микросервисных приложенийИспользование AzureDevOps при разработке микросервисных приложений
Использование AzureDevOps при разработке микросервисных приложений
 
Dockerize All The Things
Dockerize All The ThingsDockerize All The Things
Dockerize All The Things
 
Docker 101: An Introduction
Docker 101: An IntroductionDocker 101: An Introduction
Docker 101: An Introduction
 
Justin Cormack - The 10 Container Security Tricks That Will Help You Sleep At...
Justin Cormack - The 10 Container Security Tricks That Will Help You Sleep At...Justin Cormack - The 10 Container Security Tricks That Will Help You Sleep At...
Justin Cormack - The 10 Container Security Tricks That Will Help You Sleep At...
 
SenchaCon 2016: Develop, Test & Deploy with Docker - Jonas Schwabe
SenchaCon 2016: Develop, Test & Deploy with Docker - Jonas Schwabe SenchaCon 2016: Develop, Test & Deploy with Docker - Jonas Schwabe
SenchaCon 2016: Develop, Test & Deploy with Docker - Jonas Schwabe
 
Using Minikube for Node.js development
Using Minikube for Node.js developmentUsing Minikube for Node.js development
Using Minikube for Node.js development
 
Consuming Cinder from Docker
Consuming Cinder from DockerConsuming Cinder from Docker
Consuming Cinder from Docker
 
Continuous Deployment with Kubernetes, Docker and GitLab CI
Continuous Deployment with Kubernetes, Docker and GitLab CIContinuous Deployment with Kubernetes, Docker and GitLab CI
Continuous Deployment with Kubernetes, Docker and GitLab CI
 
vodQA(Pune) 2018 - Docker in Testing
vodQA(Pune) 2018 - Docker in TestingvodQA(Pune) 2018 - Docker in Testing
vodQA(Pune) 2018 - Docker in Testing
 
Docker + jenkins in the enterprise (3)
Docker + jenkins in the enterprise (3)Docker + jenkins in the enterprise (3)
Docker + jenkins in the enterprise (3)
 
Docker Meetup Rosenheim: Package & deploy Microservices
Docker Meetup Rosenheim: Package & deploy MicroservicesDocker Meetup Rosenheim: Package & deploy Microservices
Docker Meetup Rosenheim: Package & deploy Microservices
 

Recently uploaded

Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 

Recently uploaded (20)

Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 

Node.js Rocks in Docker for Dev and Ops

  • 1. Bret Fisher Docker Captain, DevOps Dude bretfisher.com/docker Node.js Rocks in Docker
  • 2. Who's This Session For? •You know some Node •You know some Docker •You want more Node+Docker awesomesauce
  • 3. What We Gonna' Learn Bret? •Node Dockerfile Best Practices •Make a real-world multi-stage Dockefile •Build with auditing and sec scans •Proper Node shutdown •Node HTTP connection management
  • 5. Every Node Sample Dockerfile
  • 6. Node Base Image Guidelines •Stick to even numbered major releases •Don't use :latest tag •Start with Debian if migrating •Use stretch (default) not jessie •Try slim first •Move to Alpine later, maybe
  • 7. When to use Alpine Images •Alpine is "small" and "sec focused" •But Debian/Ubuntu are smaller now too •~100MB space savings isn't significant •Alpine has its own issues •Alpine CVE scanning fails •Enterprises may require CentOS or Ubuntu/Debian
  • 8. Image Sizes for node/slim/alpine
  • 9. Image Sizes for node/slim/alpine
  • 10. node_modules in Images •Problem: we shouldn't build images with node_modules from host •Example: node-gyp •Solution: add node_modules to .dockerignore •copy .gitignore?
  • 11. Least Privilege: Using node User •Official node images have a node user •But it’s not used until you USER node •Do this after apt/apk and npm i -g •Do this before npm i •May cause permissions issues with write access •May require chown node:node
  • 15. Node Process Management In Containers •No need for nodemon, forever, or pm2 on server •We'll use nodemon in dev for file watch later •Docker manages app start, stop, restart, healthcheck •Node multi-thread: Docker manages multiple “replicas” •One npm/node problem: They don’t listen for proper shutdown signal by default
  • 16. The Truth About The PID 1 Problem •PID 1 (Process Identifier) is the first process in a system (or container) (AKA init) •Init process in a container has two jobs: • reap zombie processes • pass signals to sub-processes •Zombie not a big Node issue •Focus on proper Node shutdown
  • 17. Proper CMD for Healthy Shutdown •Docker uses Linux signals to stop app (SIGINIT/SIGTERM/SIGKILL) •SIGINIT/SIGTERM allow graceful stop •npm doesn't respond to SIGINIT/SIGTERM •node doesn't respond by default, but can with code •Docker provides a init PID 1 replacement option
  • 18. Proper Node Shutdown Options •Temp: Use --init to fix ctrl-c for now •Workaround: add tini to your image •Production: your app captures SIGINIT for proper exit
  • 19. Example init command •Run any node app with --init to handle signals (temp solution) >docker run --init -d nodeapp
  • 20. Example tini Dockerfile •Add tini to your Dockerfile, then use it in CMD (permanent workaround) >RUN apk add --no-cache tini >ENTRYPOINT ["/sbin/tini", "--"] >CMD ["node", "./bin/www"]
  • 24.
  • 25. •Used to track HTTP connections and send them FIN packets when Node shuts down >https://github.com/hunterloftis/stoppable Better: Connection Tracking
  • 26. Multi-stage Builds •Build multiple images from one file •Those images can FROM each other •COPY files between them •Space + security benefits •Great for "artifact only" •Great for dev + test + prod
  • 27. Avoiding devDependencies In Prod •Multi-stage can solve this •prod stages: npm i --only=production •Dev stage: npm i --only=development •Optional: Use npm ci to speed up builds •Ensure NODE_ENV is set
  • 30. Building A Specific Stage •To build dev image from dev (last) stage >docker build -t myapp . •To build prod image from prod stage >docker build -t myapp:prod --target prod .
  • 31. More Multi-stage: test •Add a test stage that runs npm test •Have CI build --target test stage before building prod •Don’t COPY code into dev stage •Keep it DRY (for COPY and RUN)
  • 35. Security Scanning and Audit •Create audit stage for optional build •Consider RUN npm audit •Consider CVE scanner •Only report at first, no failing (most images have at least one CVE vuln)
  • 40. Compose YAML v2 vs v3 •Myth busting: v3 does not replace v2 •v2 focus: single-node dev/test •v3 focus: multi-node orchestration •If not using Swarm/Kubernetes, stick to v2
  • 41. Every Node Sample Compose
  • 42. node_modules in Bind-Mounts •Problem: we can't just bind-mount node_modules content from host on macOS/Windows (different arch) •Two Potential Solutions
  • 43. node_modules in Bind-Mounts •Solution 1, common but less flexible: •Bind-mount /app which includes modules •You can't docker-compose up until you've used docker-compose run •node_modules on host is now only usable from container •Never npm install from host
  • 44. node_modules in Bind-Mounts •Solution 2, more complex but flexible: •Move node_modules up a directory in Dockerfile •Use empty volume to hide node_modules on bind-mount •node_modules on host doesn't conflict
  • 45. Bind-Mounting: Performance •On Linux, bind-mounts are native •On macOS add delegated write mode •Slower in Windows, mounting across Samba/SMB •Consider file sync if it gets real bad •Or WSL + Docker
  • 48. File Monitoring and Node Auto Restarts •Use nodemon for compose file monitoring •webpack-dev-server, etc. work the same •If Windows, enable polling •Create a nodemon.json for advanced workflows (bower, webpack, parcel)
  • 49. Startup Order and Dependencies •Problem: Multi-service apps start out of order, node might exit or cycle •Multi-container dependencies need: •Name resolution (DNS) •Connection failure handling
  • 50. Dependency Awareness •depends_on: service A needs service B •Fixes name resolution issues with "can't resolve <service_name>" •Only for compose, not Orch •compose YAML v2: works with healthchecks like a "wait for script"
  • 53. Production Checklist •CMD node directly •Build with .dockerignore •capture SIGTERM, properly shutdown •npm ci or npm i --only=production •Scan/audit/test during builds •Healthchecks (readiness/liveness)