Dockerfile建立服務

codecadet - 我的小服務

codecadet是我在SOA課堂上做的一個小專案,這次的目標就是透過Dockerfile將他建立起來。

開始Dockerfile!

以下是Dockerfile的程式碼

FROM ubuntu:14.04

MAINTAINER ChenLiZhan "https://github.com/chenlizhan"

# Installing basic stuf: wget, git, ruby
# These are dependencies for compiling gems with native extentions like Nokogiri 
RUN apt-get update
RUN apt-get install -y make
RUN apt-get install -y gcc
RUN apt-get install -y libxslt-dev libxml2-dev
RUN apt-get install -y wget git-core

# Installing Ruby 
RUN apt-get -y install build-essential zlib1g-dev libssl-dev libreadline6-dev libyaml-dev
RUN wget http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.5.tar.gz
RUN tar xvfz ruby-2.1.5.tar.gz
WORKDIR /ruby-2.1.5
RUN ./configure
RUN make
RUN sudo make install

# Installing Bundler
RUN gem install bundler

# injecting private key from host into container
RUN mkdir -p /root/.ssh
ADD URL_TO_SSH_KEY /root/.ssh/id_rsa
RUN chmod 700 /root/.ssh/id_rsa
RUN echo "Host github.com\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config

# git clone codecadet
RUN git clone git@github.com:ISS-SOA/codecadet.git /root/codecadet
RUN bundle install --gemfile=/root/codecadet/Gemfile

EXPOSE 4567

WORKDIR /root/codecadet
CMD ["rackup", "-p", "4567"]

其中遇到較大的問題是把git的ssh key注入到container當中,目前我的做法是把我的git ssh key放到一個url,在Dockerfile中使用,然後在container裡面執行
echo "Host github.com\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config
(將GitHub host key checking關掉),努力積極找更安全的做法當中….。

Docker 小筆記

用一行指令 停止/刪除 所有的Docker containers

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

在Dockerfile裡面使用ADD指令,第一個參數若為host machine上的檔案或資料夾,必須為相對路徑