Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Using Docker for Development

Using Docker for Development

An intro to using docker for development.

Angus Miller

May 16, 2017
Tweet

More Decks by Angus Miller

Other Decks in Programming

Transcript

  1. WHAT IS DOCKER? • "The Docker platform leverages Docker containers

    to enable IT operations teams and Development teams to build, ship and run any application, anywhere." • Lightweight containers. • Better and faster than all those VM’s!
  2. TRADITIONAL VM vs. DOCKER Containers are an abstraction at the

    app layer that packages code and dependencies together. Virtual machines (VMs) are an abstraction of physical hardware turning one server into many servers.
  3. WHY SHOULD YOU CARE? • It's suport fast. • Minimal

    overhead/resource usage. • Easy to run your whole production stack locally. • Ensure your whole team runs the same version of everything.
  4. WHAT WE’VE LEARNED SO FAR? • Container - Lightweight virtualization.

    • Image - Immutable snapshot of a container. • Hub/Index - Central hub for sharing images.
  5. EXAMPLE: POSTGRESQL SERVER • General purpose PostgreSQL instance • Docker

    needs to be installed • We’ll use “data containers” for pg data
  6. STEP 1: CREATE DATA CONTAINER docker create -v /var/lib/postgresql/data --name

    postgres-data busybox • Creates a container named postgres-data • Container has a volume /var/lib/postgresql/data • Container is built from busybox image
  7. STEP 2: CREATE PG CONTAINER docker run --name local-postgres-latest -p

    5432:5432 -e POSTGRES_PASSWORD=vlabs -d --volumes-from postgres-data postgres:latest • Creates a container named local-postgres-latest • Container uses volume from postgres-data • Container specifies env var -e POSTGRES_PASSWORD=vlabs • Container runs in background -d • Container maps port -p 5432:5432
  8. STEP 3: CONNECT TO CONTAINER psql -h localhost -p 5432

    -U postgres • Launch psql client and connect to container
  9. DOCKER COMPOSE • Compose is a tool for defining and

    running multi-container Docker applications • You use a Compose file to configure your application’s services. • Using a single command, you create and start all the services from your configuration.
  10. EXAMPLE: RAILS APP • Let’s create a hello world rails

    app • Docker and Docker Compose need to be installed
  11. STEP 1: CREATE DOCKERFILE • Find a suitable local directory

    • Create file called Dockerfile as follows: FROM ruby:2.3.3 RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs RUN mkdir /myapp WORKDIR /myapp ADD Gemfile /myapp/Gemfile ADD Gemfile.lock /myapp/Gemfile.lock RUN bundle install ADD . /myapp
  12. STEP 2: CREATE RAILS FILES • We need a Gemfile

    and empty Gemfile.lock • Gemfile must have source 'https://rubygems.org' gem 'rails'
  13. STEP 3: CREATE DOCKER-COMPOSE FILE • Create file called docker-compose.yml:

    version: '2' services: db: image: postgres web: build: . command: bundle exec rails s -p 3000 -b '0.0.0.0' volumes: - .:/myapp ports: - "3000:3000" depends_on: - db
  14. STEP 4: BUILD THE PROJECT • Now can provision our

    image/containers docker-compose run web rails new . --force --database=postgresql
  15. STEP 5: UPDATE DB CONFIG • Replace the contents of

    config/database.yml with: development: &default adapter: postgresql encoding: unicode database: myapp_development pool: 5 username: postgres password: host: db test: <<: *default database: myapp_test
  16. STEP 6: BOOT THE APP • Boot the app using:

    docker-compose build docker-compose up
  17. RECAP • Run containers: docker run. • Use Dockerfiles for

    building images. • Use Docker Compose for app services.