deploy nodejs on docker
TRANSCRIPT
How many…are using Docker in production?
are using Docker in development?are here to see if Docker is the right tool for them?
#DesertCodeCamp @wfbutton
In this session:• Docker best practices • Deploying node.js • Creating Docker resources • Performance optimizing • Repeatable
#DesertCodeCamp @wfbutton
Our humble Dockerfile
• Actually called “Dockerfile”
• In the same directory as our code
#DesertCodeCamp @wfbutton
…gets a userfrom node:4.4.7
RUN useradd --user-group --create-home --shell /bin/false nodejs
#DesertCodeCamp @wfbutton
…gets and a homefrom node:4.4.7
RUN useradd --user-group --create-home --shell /bin/false nodejs
ENV HOME=/home/nodejs
#DesertCodeCamp @wfbutton
…gets a production envfrom node:4.4.7
RUN useradd --user-group --create-home --shell /bin/false nodejs
ENV HOME=/home/nodejs ENV NODE_ENV=production
#DesertCodeCamp @wfbutton
docker-compose.ymlapp: mem_limit: 300m memswap_limit: 1g
build: .
ports: - '3000:3000'
#DesertCodeCamp @wfbutton
docker-compose.ymlapp: mem_limit: 300m memswap_limit: 1g
build: .
ports: - ‘3000:3000’
volumes: - .:/home/nodejs/app
back to the Dockerfilefrom node:4.4.7
RUN useradd --user-group --create-home --shell /bin/false nodejs
ENV HOME=/home/nodejs ENV NODE_ENV=production
USER nodejs
CMD ["node", "server.js"]
This isn’t bad, and it will work… but we can improve it!
#DesertCodeCamp @wfbutton
As a matter of fact…• it will create the node_modules folder
• thanks to Docker caching and build layers, if package.json and npm-shrinkwrap.json don’t change:
• the layer gets re-used
• Result: faster deploys because you don’t have to wait for npm install to run
#DesertCodeCamp @wfbutton
there is a catch…from node:4.4.7
RUN useradd --user-group --create-home --shell /bin/false nodejs
ENV HOME=/home/nodejs ENV NODE_ENV=production
COPY package.json npm-shrinkwrap.json $HOME/app/ RUN chown -R nodejs:nodejs $HOME/* USER nodejs RUN npm install
CMD ["node", "server.js"]
#DesertCodeCamp @wfbutton
docker-compose.ymlapp: mem_limit: 300m memswap_limit: 1g
build: .
ports: - ‘3000:3000’
volumes: - .:/home/nodejs/app - /home/nodejs/app/node_modules
#DesertCodeCamp @wfbutton
How’d we do?We limited the memory and swap on our container to
prevent it from stealing resources.#DesertCodeCamp @wfbutton
Remember the .:/home?
• The dot is a local reference.
• On a remote server you can’t reference local folders
• Use a file system reference local to the Docker host
#DesertCodeCamp @wfbutton
Going further
• Deploy script reduces deployments to a single command
• While simple to start, they can grow with your needs
• Imagine new developer on-boarding
#DesertCodeCamp @wfbutton
https://www.docker.com/sites/default/files/RA_CI%20with%20Docker_08.25.2015.pdf
or just Google “deploy
Docker with Jenkins”
#DesertCodeCamp @wfbutton
Testing
• Sadly, still an area where Docker needs work
• Bundle test suite (and test data) into image
• Rely on external integration style tests for validation
#DesertCodeCamp @wfbutton
• Goal of using best practices • Pinned version • non-root user • environment variables • memory limits
• Caching layers for performance • Locally vs. Remote Deploys • Single command deployments • Automated deployments • Testing
#DesertCodeCamp @wfbutton