Code Reading - Learn ❤️ Docker
    Published Thursday, June 15, 2017
  
  Notes from Flatiron School engineering team’s code reading on using Docker with our core codebase.
Prereqs:
- Watch this Youtube playlist (~90 min)
- Clone down existing Rails app (in our case, the Learn codebase)
- Set up account on Docker Hub
Intro
What is Docker?
- Tool for managing and creating containers
Why use Docker?
- Maintain a stable, consistent environment
- Encapsulate all pices of an app
- Uniformity and consistency
- Step towards better deployments and auto-scaling infrastructure (Swarm, Kubernetes)
- Lighter than a VM (no hypervisor)
- One of the more mature tools available
Basics
Docker image:
- Pre-packaged layer
- Can stack layers on each other
- Result of running Dockerfile
- Can create containers off of image
Docker container:
- Running isolated container of your app’s code
Docker networking:
- Built-in
- Allows containers to talk to each other over same network
Docker volumes:
- Take local files and put them into your container
- Edit locally, see changes in container
Docker compose:
- Toolset for coordinating containers
Docker Hub:
- Store and access repositories (images)
Practice
Run an image: docker run -it ubuntu (creates container with interactive terminal)
Build Dockerfile: docker build -t wget . (Note: . means look for Dockerfile in current directory)
FROM ruby:2.3
RUN apt-get update -yqq \
  && apt-get install -yqq --no-install-recommends \
  postgresql-client
WORKDIR /usr/src/app
COPY Gemfile* ./
RUN bundle install
COPY . .
EXPOSE 3000
CMD rails server -b 0.0.0.0
Side note: rails server -b 0.0.0.0 binds to all ports.
Workshop: Running Learn on Docker
- Create Dockerfile in app directory (app === build context)
  FROM ruby:2.2.2cat
  RUN apt-get update -yqq \
    && apt-get install -yqq --no-install-recommends \
    postgresql-client nodejs npm \
    && npm install gulp -g \
    # symlink nodejs -> node
    && ln -s /usr/bin/nodejs /usr/bin/node
  WORKDIR /var/app
  COPY Gemfile* ./
  RUN bundle install
  COPY package.json ./
  RUN npm install
  COPY . .
  EXPOSE 3000
  CMD rails server -b 0.0.0.0
- Optional: add .dockerignore file
- cat .gitignore >> .dockerignore
- Build image: docker build -t ironboard .
- Run into errors? Get image id (docker images), then rundocker run -it <hash> bashto drop into image at that point.
- docker imagesto see image
- docker run -d --name app -p 3000:3000 -v $PWD:/var/app ironboard(port flag:- -p, share volume:- -v)
- docker psto see running containers
- docker logs --followto see stdout