LoginSignup
3
3

More than 5 years have passed since last update.

Vagrant+Dockerでプログラミング環境を構築メモ

Last updated at Posted at 2019-02-18

Dockerは便利...Vagrantも便利...もう組み合わせるしかないですね( ゚Д゚)

Vagrantで構築したVM上にDockerをインストール

VagrantやVirtualBox、Dockerのインストールについては以前、ブログに書きましたので割愛します。

[https://qiita.com/reinsF82/items/7d0ecbb31d77a1a706f0]
[https://qiita.com/reinsF82/items/867a74c2d37d86732b58]

構築するにあたってのVagrantfileの設定と、docker-compose.ymlの設定、Dockerfileの設定について自分用のやり方を書いていきたいと思います(*´ω`)

Vagrantfile

実験環境用と定期処理用で2つVMを用意するために以下のようにしました(*‘ω‘ *)

# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu/xenial64"

  config.vm.define :node1 do |node|
    node.vm.box = config.vm.box
    node.vm.network :private_network, ip:"192.168.56.10"
    node.vm.synced_folder "./wkspace", "/home/vagrant/shared"
    node.vm.provider "virtualbox" do |vb|
      vb.gui = false
      vb.memory = "4096"
      vb.cpus = 4
    end
    node.vm.provision "shell", privileged: false, path: "provision/provision_node1.sh"
  end

  config.vm.define :node2 do |node|
    node.vm.box = config.vm.box
    node.vm.network :private_network, ip:"192.168.56.20"
    node.vm.synced_folder "./wkspace", "/home/vagrant/shared"
    node.vm.provider "virtualbox" do |vb|
      vb.gui = false
      vb.memory = "4096"
      vb.cpus = 4
    end
    node.vm.provision "shell", privileged: false, path: "provision/provision_node2.sh"
  end

end

Vagrantのプロビジョン

Vagrantでは、前処理として事前にパッケージのインストールなどのコマンドをシェルスクリプトに書いておくことができます。
私はdockerを利用するために以下のような記述をしております(*´ω`)

# provision_node1.sh

#!/bin/sh
sudo apt-get update

sudo apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io

sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

docker-compose.yml

docker-composeで複数のコンテナを管理するために利用します。使いそうなプログラミング言語用に設定してみました。

version: '2'
services:
  anaconda3:
    build: ./build/app/anaconda3
    container_name: 'anaconda3'
    tty: true
    ports:
     - "8888:8888"
    volumes:
     - /home/vagrant/shared/program/python/jupyter-notebook/notebooks:/opt/notebooks

  go:
    build: ./build/app/go
    container_name: 'golang'
    tty: true
    ports:
     - "8887:8887"
    volumes:
     - /home/vagrant/shared/program/go:/opt/src/go

  julia:
    build: ./build/app/julia
    container_name: 'julia'
    tty: true
    ports:
     - "8886:8886"
    volumes:
     - /home/vagrant/shared/program/julia:/opt/src/julia

Dokerfile

細かい設定をanacondaのコンテナで行うので、anaconda用のDokerfileを取り上げます。
jupyter-notebookをDocker on VirtualBoxで動作させているので、ホストで利用するために設定が必要になります。

FROM continuumio/anaconda3

SHELL ["/bin/bash", "-c"]
RUN apt update \
    && apt install -y --no-install-recommends \
    git \
    curl \
    wget \
    gnupg \
    unzip \
    fonts-ipaexfont \
    && rm -rf /var/lib/apt/list/* /var/cache/apt/archives/*

RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list
RUN apt update \
    && apt install -y google-chrome-stable

RUN mkdir -p /opt/notebooks

RUN conda install -c conda-forge phantomjs
#RUn conda install -c conda-forge selenium
#RUN conda install -c clinicalgraphics chromedriver
#RUN conda install -c clinicalgraphics selenium-chromedriver
RUN pip install --upgrade pip
RUN pip install --upgrade chromedriver-binary
RUN pip install --upgrade selenium
RUN pip install --upgrade google-api-python-client
RUN pip install --upgrade google-auth google-auth-oauthlib google-auth-httplib2
RUN pip install pytube

RUN conda install jupyter -y --quiet
CMD bash -c "jupyter notebook --notebook-dir=/opt/notebooks --ip='0.0.0.0' --no-browser --allow-root"

以上が自分用の設定になりますね(*´ω`)
詳しい説明については割愛しますが、ポートの設定を調整すればDocker on VirtualBoxでもホストからもjupyter-notebookにアクセスできます(/・ω・)/

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3