2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

docker-composeの書き方を見直してみた...

Last updated at Posted at 2019-04-04

docker-composeのフォルダ構成

現在、一例として以下のようなフォルダ構成にしていました。

docker
│  docker-compose.yml
│
└─build
    └─app
        ├─anaconda3
        │      Dockerfile
        │
        ├─go
        │      Dockerfile
        │
        ├─julia
        │      Dockerfile
        │
        ├─jupyter
        │      Dockerfile
        │
        └─ubuntu_1604
                Dockerfile

しかし、コンテナごとにDockerfileを作っていたため、正直管理がめんどくさくなってきました...(´・ω・)
docker-compose.ymlに設定を書いてからDockerfileにも設定を書くので、それぞれの記述を対応させる必要があって大変です...

docker-compose.ymlの中身

下記の設定を見ていただくとわかると思いますが、作成するコンテナの設定を各Dockerfileを参照しているため、設定を変えようと思ったときに結局Dockerfileの中身も見ないといけないので手間です...

version: '3'
services:
  anaconda3:
    build: ./build/app/anaconda3
    container_name: 'anaconda3'
    tty: true
    ports:
     - "8877:8877"
    volumes:
     - /home/vagrant/shared:/home/shared/program/jupyter-notebook/notebooks
    links:
      - python
      - go
      - julia

  jupyter:
    build: ./build/app/jupyter
    container_name: 'jupyter-server'
    tty: true
    ports:
     - "8884:8884"
    volumes:
     - /home/vagrant/shared:/home/shared/program/jupyter-notebook/notebooks

  python:
    build: ./build/app/anaconda3
    container_name: 'python-server'
    tty: true
    ports:
     - "8885:8885"
    volumes:
     - /home/vagrant/shared:/home/shared/program/jupyter-notebook/notebooks

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

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

しかも、各Dockerfileで「apt-get update」しているのでdocker imageを作ってからコンテナが立ち上がるまでに時間がかかるという...コンテナとしては本末転倒かなってと思い始めてしまいました...(そもそもコンテナの中身自体が新しいはずなので、apt-get updateいらなくない?と思い始めました...)そういうわけでdocker-compose.ymlの書き方を見直すことにしました(´・ω・)

新しく書き換えたdocker-compose.ymlとフォルダ構成

まず、フォルダ構成は以下の通りになりました。

docker
│  docker-compose.yml

docker-compose.ymlにすべてを書きました。記述が多くなりましたが...すべての設定をdocker-compose.ymlで管理できるようになりました(/・ω・)/ docker-compose.ymlの中身は以下の感じになりました。

version: '3'
services:
  anaconda3:
    image: continuumio/anaconda3
    container_name: 'anaconda3'
    tty: true
    command: >
      bash -c "mkdir -p /home/shared/program/python/jupyter-notebook/notebooks &&
      pip install --upgrade pip &&
      pip install --upgrade schedule &&
      conda install -y selenium &&
      conda install -y beautifulsoup4 &&
      conda install -y python-chromedriver-binary &&
      conda update -all -y &&
      jupyter notebook --notebook-dir=/home/shared/program/python/jupyter-notebook/notebooks --ip='0.0.0.0' --port=8877 --no-browser --allow-root"
    ports:
     - "8877:8877"
    volumes:
     - /home/vagrant/shared/program/python/jupyter-notebook/notebooks:/home/shared/program/python/jupyter-notebook/notebooks
     - /etc/localtime:/etc/localtime:ro
    environment:
      TZ: Asia/Tokyo

  jupyter:
    image: jupyter/datascience-notebook
    container_name: 'jupyter-server'
    tty: true
    command: >
      bash -c "mkdir -p /home/shared/program/python/jupyter-notebook/notebooks &&
      pip install --upgrade pip &&
      pip install --upgrade schedule &&
      conda install -y selenium &&
      conda install -y beautifulsoup4 &&
      conda install -y python-chromedriver-binary &&
      conda update --all -y &&
      jupyter notebook --notebook-dir=/home/shared/program/python/jupyter-notebook/notebooks --ip='0.0.0.0' --port=8888 --no-browser --allow-root"
    ports:
     - "8888:8888"
    volumes:
     - /home/vagrant/shared/program/python/jupyter-notebook/notebooks:/home/shared/program/python/jupyter-notebook/notebooks
     - /etc/localtime:/etc/localtime:ro
    environment:
      TZ: Asia/Tokyo

  python:
    image: continuumio/anaconda3
    container_name: 'python-server'
    tty: true
    command: >
      bash -c "mkdir -p /home/shared/program/python &&
      pip install --upgrade pip &&
      pip install --upgrade schedule &&
      conda install -y selenium &&
      conda install -y beautifulsoup4 &&
      conda install -y python-chromedriver-binary &&
      conda update --all -y &&
      bash"
    volumes:
     - /home/vagrant/shared/program/python:/home/shared/program/python
     - /etc/localtime:/etc/localtime:ro
    environment:
      TZ: Asia/Tokyo

  go:
    image: golang
    container_name: 'golang-server'
    tty: true
    command: >
      bash -c "mkdir -p /home/shared/program/go &&
      go get -u -t github.com/PuerkitoBio/goquery &&
      go get -u -t github.com/bitly/go-simplejson &&
      go get -u -t github.com/Code-Hex/pget/cmd/pget &&
      bash"
    volumes:
     - /home/vagrant/shared/program/go:/home/shared/program/go
     - /etc/localtime:/etc/localtime:ro
    environment:
      TZ: Asia/Tokyo

  julia:
    image: julia
    container_name: 'julia-server'
    tty: true
    command: >
      bash -c "mkdir -p /home/shared/program/julia &&
      bash"
    volumes:
     - /home/vagrant/shared/program/julia:/home/shared/program/julia
     - /etc/localtime:/etc/localtime:ro
    environment:
      TZ: Asia/Tokyo

jupyter-notebookを使用するコンテナ以外はプログラムを実行するだけなので、必要なパッケージを入れるだけでいいなーと思い、上記みたいになりました(*‘ω‘ *)
上記の書き方だとdocker-compose buildをしなくていいので、docker-compose upするだけでコンテナが立ち上がるのも利点ですかね???(/・ω・)/

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?