Deployment and Docker
Vocab • Development Environment (dev) • The environment where you write your code • Ex. Your laptop • Add features; Find and eliminate bugs • Production environment (prod) • The environment where your app will eventually live • The live server with real end users • Do everything we can to avoid bugs in production
Deployment Headaches • It works on my laptop! • Run your code in production and it's broken • Many causes • Di ff erent version of compiler/interpreter • Dependancies not linked • Hard-coded path • Di ff erent environment variables • etc
Virtual Machines • Simulate an entire machine • Run the the virtual machine (VM) in your development environment for testing • Run an exact copy of the VM on the production server • No more surprise deployment issues • Simulating an entire machine can be ine ffi cient • If you've ran a VM on your laptop you know how slow this can get
Containers • Containers are the new [not so new anymore] hot thing • E ff ectively runs lightweight VMs • Cross platform • And portable
Security • Can't break out of the container • If an attacker compromises the server, they can only access what you put in the container • Can't "rm -f /" your entire machine • Patch the exploited vulnerability and rebuild the image • The attacker can still cause significant damage and steal private data • The just can't destroy your physical server box
Security • Sometimes an app has to allow code injection attacks to function • AutoLab • AWS • Heroku • Digital Ocean • Run user code in their own container
Docker • Docker is software that's used to create containers • Install Docker in your development environment to test containers • Install Docker in your production environment to run containers in the same environment
Dockerfile • To start working with Docker, write a Dockerfile • This file contains all the instructions needed to build a Docker image • Some similarities to a Makefile
Dockerfile FROM ubuntu:18.04 • Let's explore this RUN apt-get update sample Dockerfile # Set the home directory to /root ENV HOME / root # cd into the home directory WORKDIR / root # Install Node • This Dockerfile RUN apt-get update -- fix-missing RUN apt-get install - y nodejs RUN apt-get install - y npm creates an image # Copy all app files into the image COPY . . for a node.js app # Download dependancies RUN npm install # Allow port 8000 to be accessed # from outside the container EXPOSE 8000 # Run the app CMD [ "node" , "ecom_app.js" ]
Dockerfile FROM ubuntu:18.04 • The first line of your Dockerfile RUN apt-get update will specify the base image # Set the home directory to /root ENV HOME / root • This image is downloaded # cd into the home directory and the rest of your Dockerfile WORKDIR / root adds to this image # Install Node RUN apt-get update -- fix-missing • In this example: We start with RUN apt-get install - y nodejs RUN apt-get install - y npm Ubuntu 18.04 # Copy all app files into the image COPY . . • Our Dockerfile can run # Download dependancies Linux commands in RUN npm install Ubunutu # Allow port 8000 to be accessed # from outside the container EXPOSE 8000 # Run the app CMD [ "node" , "ecom_app.js" ]
Dockerfile FROM ubuntu:18.04 • Use the RUN keyword to RUN apt-get update run commands in the # Set the home directory to /root ENV HOME / root base image # cd into the home directory WORKDIR / root • Use this for any setup of # Install Node RUN apt-get update -- fix-missing your OS before setting RUN apt-get install - y nodejs RUN apt-get install - y npm up your app # Copy all app files into the image COPY . . • In this example: Updating # Download dependancies apt-get which is used to RUN npm install # Allow port 8000 to be accessed install software # from outside the container EXPOSE 8000 # Run the app CMD [ "node" , "ecom_app.js" ]
Dockerfile FROM ubuntu:18.04 • Use ENV to set environment RUN apt-get update variables # Set the home directory to /root ENV HOME / root • Setting the home directory # cd into the home directory here WORKDIR / root # Install Node • Can use ENV to setup any RUN apt-get update -- fix-missing RUN apt-get install - y nodejs other variables you need RUN apt-get install - y npm # Copy all app files into the image • Use WORKDIR to change COPY . . your current working # Download dependancies RUN npm install directory # Allow port 8000 to be accessed • Same as "cd" # from outside the container EXPOSE 8000 # Run the app CMD [ "node" , "ecom_app.js" ]
Dockerfile FROM ubuntu:18.04 • Since we're starting with a RUN apt-get update fresh image of Ubuntu # Set the home directory to /root ENV HOME / root • Only the default software # cd into the home directory WORKDIR / root is installed # Install Node • RUN commands to install RUN apt-get update -- fix-missing RUN apt-get install - y nodejs all required software for RUN apt-get install - y npm your app # Copy all app files into the image COPY . . • Typically your # Download dependancies RUN npm install development tools for # Allow port 8000 to be accessed your language of choice # from outside the container EXPOSE 8000 # Run the app CMD [ "node" , "ecom_app.js" ]
Dockerfile FROM ubuntu:18.04 • COPY all your app file into the RUN apt-get update image # Set the home directory to /root • "." denotes the current directory ENV HOME / root # cd into the home directory • Run docker from your apps root WORKDIR / root directory # Install Node RUN apt-get update -- fix-missing • The the first "." will refer to RUN apt-get install - y nodejs RUN apt-get install - y npm your apps directory # Copy all app files into the image • We changed the home and COPY . . # Download dependancies working directory to /root RUN npm install • The second "." refers to /root # Allow port 8000 to be accessed # from outside the container in the image EXPOSE 8000 # Run the app CMD [ "node" , "ecom_app.js" ]
Dockerfile FROM ubuntu:18.04 • Now that your apps files are RUN apt-get update in the image, run all app # Set the home directory to /root ENV HOME / root specific commands # cd into the home directory • Order is important WORKDIR / root # Install Node • Don't depend on your RUN apt-get update -- fix-missing RUN apt-get install - y nodejs app files before copying RUN apt-get install - y npm them into the image # Copy all app files into the image COPY . . • Use RUN to install # Download dependancies RUN npm install dependancies and perform # Allow port 8000 to be accessed any other required setup # from outside the container EXPOSE 8000 # Run the app CMD [ "node" , "ecom_app.js" ]
Dockerfile FROM ubuntu:18.04 • Use EXPOSE to allow RUN apt-get update specific ports to be accessed # Set the home directory to /root ENV HOME / root from outside the container # cd into the home directory • By default, all port are WORKDIR / root blocked # Install Node RUN apt-get update -- fix-missing RUN apt-get install - y nodejs • Container is meant to run RUN apt-get install - y npm in isolation # Copy all app files into the image COPY . . • To run a web app in a # Download dependancies RUN npm install container, expose the port # Allow port 8000 to be accessed that your runs on # from outside the container EXPOSE 8000 # Run the app CMD [ "node" , "ecom_app.js" ]
Dockerfile FROM ubuntu:18.04 • Finally, use CMD to run you RUN apt-get update app # Set the home directory to /root ENV HOME / root • Important: Do not use RUN to # cd into the home directory run your app! WORKDIR / root • RUN will execute the command # Install Node RUN apt-get update -- fix-missing RUN apt-get install - y nodejs when the image is being built RUN apt-get install - y npm • CMD will execute when the # Copy all app files into the image COPY . . container is ran # Download dependancies • We do not want the app to run RUN npm install # Allow port 8000 to be accessed when the image is being built # from outside the container EXPOSE 8000 # Run the app CMD [ "node" , "ecom_app.js" ]
Docker Containers • We can now build a Docker image • From the command line run "docker build -t <image_name> ." • Great, but we wanted a container • An image is use to create containers • Similar to using a class to create objects
Docker Containers docker container run --publish <local_port>:8000 --detach <image_name> • Once you have an image, run this line in the command line to create and run a container where • --publish <local_port>:8000 - maps a port on the host machine to an exposed port in the container • --detach - runs the container in the background • <image_name> matches the image name chosen when you created the image
Docker Containers docker container run --publish <local_port>:8000 --detach <image_name> • After running this command your app should be accessible from <local_port>
Recommend
More recommend