# docker-syncを使ってホストとローカルの同期を超光速にしよう

Docker for Macはホストとコンテナ間の同期(sync)があり得ないくらい遅い、、、 しかも、特に frontのnode_modulesとか、railsのtmp/* , log/*とか。。。 まじありえねぇ〜って感じなんで、どうにかしたいなぁ〜って時は、docker-syncを使うべし!

# ディレクトリ構成

sample-app-docker
  ┣━ .gitignore
  ┣━ docker-compose.yml
  ┣━ docker-sync.yml
  ┣━ elastic-search
  ┃      ┗━ dev.Dockerfile
  ┃
  ┣━ sample-app_server
  ┃      ┣━ app/
  ┃      ┣━ bin/
  ┃      ┣━ config/
  ┃      ┗━ ...
  ┃
  ┗━ sample-app-frontend
         ┣━ components/
         ┣━ assets/
         ┣━ static/
         ┗━ ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

iTerm

$ gem install docker-sync
$ brew install unison # unisonは双方向で同期できる。片方向は、fswatchとか?
$ brew tap eugenmayer/dockersync
$ brew install eugenmayer/dockersync/unox
$ pwd (current-directoriがsample-app-dokcerで�あることを確認)
$ bundle install
1
2
3
4
5
6

docker-compose.yml

version: "3"
services:
  db:
    image: mysql:5.7 #mysql5.7が準備されているimageを指定
    ports:
      - "3306:3306" # <-これがないと外からアクセス出来ない
      - "33060:33060"
    command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci
    environment:
      MYSQL_ROOT_PASSWORD: root #root userのパスワード
      MYSQL_SOCKET: "/var/lib/mysql/mysql.sock"
      MYSQL_HOST: db
  redis:
    image: redis:alpine
    ports:
      - "6379:6379"
  elastic-search:
    container_name: sample-app-elastic-search
    build:
     context: .
     dockerfile: ./elastic-search/dev.Dockerfile
    environment:
      - discovery.type=single-node
      - xpack.security.enabled=false
#    volumes:
#      - sample-app-elastic-search:/usr/share/elasticsearch/data
    ports:
      - "9200:9200"
      - "9300:9300"
  webapp_server:
    build: ./sample-app_alpha/
    restart: always
    container_name: sample-app_server
    environment:
      TZ: "Asia/Tokyo"
      MYSQL_SOCKET: "/var/lib/mysql/mysql.sock"
      MYSQL_PASSWORD: root
      MYSQL_HOST: db
    tty: true #標準出力on
    depends_on:
      - db
      - redis
      - elastic-search
    ports:
      - "4000:3000"
    volumes:
      - server-sync:/home/server
    command: >
              bash -c 'bundle exec rails db:create && 
              bundle exec rails db:migrate &&
              bundle exec sidekiq -C config/sidekiq.yml -d &&
              bundle exec rails server -b 0.0.0.0'
  webapp_frontend:
    build: ./sample-app-frontend/
    restart: always
    container_name: sample-app_frontend
    tty: true
    ports:
      - "3001:3000"
    volumes:
      - frontend-sync:/sample-app_frontend
volumes:
  server-sync:
    external: true
  frontend-sync:
    external: true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

docker-sync.yml

version: '2'
options:
  verbose: true
syncs:
  server-sync:
    src: './sample-app_alpha/'
    notify_terminal: true
    sync_strategy: 'unison'
    sync_userid: '1000'
    sync_excludes: ['converage', '.git', '.gitignore', 'package.json', '*.coffee', '*.scss', '*.sass']
  frontend-sync:
    src: './sample-app-frontend/'
    notify_terminal: true
    sync_strategy: 'unison'
    sync_userid: '2000'
    sync_excludes: ['node_modules', '.git', '.gitignore', '*.coffee']
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

elastic-search/dev.Dockerfile

FROM elasticsearch:5.6.13-alpine
RUN elasticsearch-plugin  install analysis-kuromoji
1
2

.gitignore

sample-app-frontend
sample-app_alpha
.docker-sync/*
1
2
3

sample-app-frontend

FROM node:10-alpine
ARG USER="front"
ARG UID="2000"
ARG GROUP="front"
ARG GID="2000"
ARG WORKSPACE=/sample-app_frontend
ENV LANG C.UTF-8
RUN mkdir -p $WORKSPACE
COPY . /sample-app_frontend
WORKDIR /sample-app_frontend
RUN mkdir -p $WORKSPACE
WORKDIR $WORKSPACE
COPY / ./
# Set environment variables
ENV NODE_ENV development
ENV HOST 0.0.0.0
EXPOSE 3000
# Bundle app source
RUN groupadd -r --gid $GID $GROUP && \
    useradd -r --uid $UID --gid $GID $USER && \
    chown -R $USER:$GROUP $WORKSPACE
RUN yarn global add vue-cli
RUN yarn global add nuxt
RUN yarn install
CMD ["yarn", "dev"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

sample-app-alpha

FROM ruby:2.5
ARG USER="server"
ARG UID="1000"
ARG GROUP="server"
ARG GID="1000"
ARG WORKSPACE=/home/server
ENV LANG C.UTF-8
RUN apt-get update && apt-get install -y vim less
RUN apt-get install -y build-essential libpq-dev && \
    gem install bundler
RUN mkdir -p $WORKSPACE
WORKDIR $WORKSPACE
COPY / ./
RUN bundle install 
RUN groupadd -r --gid $GID $GROUP && \
    useradd -r --uid $UID --gid $GID $USER && \
    chown -R $USER:$GROUP $WORKSPACE
USER $USER
EXPOSE  3000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28