This will build the handson-namer image! FROM ubuntu:12.10! MAINTAINER Your Name, [email protected]! ! # Install dependencies! RUN apt-get update! RUN apt-get install -y curl git \! build-essential ruby2.1.0! RUN gem install rubygems-update! RUN update_rubygems! RUN gem install bundler --no-ri --no-rdoc! ! # Rails uses this port ya know! EXPOSE 3000! ! # Copy the app into the container at build time! ADD namer /opt/namer! WORKDIR /opt/namer! RUN bundle install! ! # Set the startup command! CMD ["rails", "server"]!
Takes minutes the first Qme. • Takes seconds the next Qme. • Only re-‐executes any steps a]er the first one that changed. ! $ docker build -t railsapp .!
Run detached • Name the container “pg” • (The image is named postgres) • Don’t use -p…it would expose port 5432 to the host. ! $ docker run -d -name pg postgres!
Sets up a network bridge to pg • Injects env vars in the railsapp container (pg’s ip addr, port mapping, etc.) ! $ docker run -d -p 80:3000 -link pg \! railsapp!
that depend on a configuraQon file will not be cached if: • that file is part of an ADDed directory, and • the directory tree contains other frequently-‐modified files. ‐︎ e.g., a Gemfile e.g., an app directory ‐︎ e.g., source files ‐︎ ‑︎︎ e.g., bundle install
dependencies (cached)! RUN apt-get install ...! ! # Copy the app into the container! # (DOES NOT use cache when myapp changes)! ADD myapp /opt/myapp! WORKDIR /opt/myapp! RUN bundle install! ! # Set the startup command! CMD ["rails", "server"]! Hands on with Docker 40
dependencies (cached)! RUN apt-get install ...! ! # Copy the Gemfile and Gemfile.lock! # (CACHED unless they changed)! ADD myapp/Gemfile /tmp/Gemfile! ADD myapp/Gemfile.lock /tmp/Gemfile.lock! WORKDIR /tmp! RUN bundle install! ! # Now copy the app into the container! ADD myapp /opt/myapp! WORKDIR /opt/myapp! ! # Set the startup command! CMD ["rails", "server"]! Hands on with Docker 44 Before ADDing the app