読者です 読者をやめる 読者になる 読者になる

技術のメモ帳

気が向いたときに書いてます

Docker practice (1/2)

Docker

Environment

Mac OSX 10.11.6 (EI Capitan)
VirtualBox 5.1.4
docker 1.11.1
docker-machine 0.7.0
docker-compose 1.7.0

Installation

$ brew cask install virtualbox
$ brew install docker-machine
$ brew cask install dockertoolbox

Boot docker-machine

What is Docker Machine?

Docker Machine is a tool that lets you install Docker Engine on virtual hosts, and manage the hosts with docker-machine commands. You can use Machine to create Docker hosts on your local Mac or Windows box, on your company network, in your data center, or on cloud providers like AWS or Digital Ocean.

Setup

Initialize

$ docker-machine create --driver virtualbox --virtualbox-hostonly-cidr "192.168.99.99/24" default
$ eval "$(docker-machine env default)"

Restart

$ docker-machine start default
$ eval "$(docker-machine env default)"

Stop, Remove

$ docker stop $(docker ps -aq)
$ docker rm $(docker ps -aq)
$ docker rmi $(docker images | awk '/^<none>/ { print $3 }')
$ docker-machine rm default

Examples

All examples have to setup docker-machine.

Ruby

$ docker pull ruby:2.3.1
$ docker run ruby:latest ruby -v
$ docker run -it ruby:latest /bin/bash # -i: Keep STDIN open even if not attached, -t: Allocate a pseudo-TTY

latest is a version of image.

MySQL

$ docker pull mysql:5.6
$ docker run -p 3307:3306 -v ~/docker/mysql/conf.d:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=pass -d mysql:5.6
$ mysql -h$(docker-machine ip default) -P3307 -uroot -p # enter "pass"

Redis

$ docker pull redis:3.2
$ docker run --name some-redis -d redis:3.2
$ docker run -it --link some-redis:redis --rm redis:3.2 redis-cli -h redis -p 6379

Rails

  1. Prepare

    $ docker pull ruby:2.3.1
    $ { \
        echo 'source "https://rubygems.org"'; \
        echo 'gem "rails"'; \
    } > Gemfile
    $ touch Gemfile.lock
    
  2. Create Dockerfile

    FROM ruby:2.3.1
    
    ENV APP_ROOT /usr/src/sample_app
    
    WORKDIR $APP_ROOT
    
    RUN apt-get update && \
        apt-get install -y nodejs \
                           mysql-client \
                           postgresql-client \
                           sqlite3 \
                           --no-install-recommends && \
        rm -rf /var/lib/apt/lists/*
    
    COPY Gemfile $APP_ROOT
    COPY Gemfile.lock $APP_ROOT
    
    RUN \
      echo 'gem: --no-document' >> ~/.gemrc && \
      cp ~/.gemrc /etc/gemrc && \
      chmod uog+r /etc/gemrc && \
      bundle config --global build.nokogiri --use-system-libraries && \
      bundle config --global jobs 4 && \
      bundle install && \
      rm -rf ~/.gem
    

    And execute docker build -t sample_app ..

  3. Create Rails app

    $ docker run --rm -it -v "$PWD":/usr/src/sample_app sample_app rails new . -BT
    
  4. Edit Dockerfile

    Append these lines:

    COPY . $APP_ROOT
    
    EXPOSE 3000
    CMD ["rails", "s", "-b", "0.0.0.0"]
    

    And execute docker build -t sample_app ..

  5. Setup DB

    $ docker run --rm sample_app rails db:create db:migrate
    
  6. Run

    $ docker run -d -p 3000:3000 sample_app
    $ open "http://$(docker-machine ip default):3000"
    

Next Step...

Using docker-compose.

References

ihcomega.hatenadiary.com

qiita.com

docs.docker.com