Skip to content

Commit

Permalink
Merge pull request #173 from jiro4989/chore/#70-docker
Browse files Browse the repository at this point in the history
コンテナ化した #70
  • Loading branch information
jiro4989 authored Jul 4, 2020
2 parents a0b2ed7 + 612c9a4 commit a35a854
Show file tree
Hide file tree
Showing 22 changed files with 257 additions and 119 deletions.
32 changes: 0 additions & 32 deletions .github/workflows/debug.yml

This file was deleted.

47 changes: 31 additions & 16 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ on:
release:
types: [published]

env:
registory: 'jiro4989/websh'
image-tag: 'latest'

jobs:
skip:
runs-on: ubuntu-latest
Expand All @@ -27,27 +31,38 @@ jobs:
needs: before
steps:
- uses: actions/checkout@v1
- name: Pull docker image
run: docker pull jiro4989/websh:latest
- uses: jiro4989/setup-nim-action@v1
with:
nim-version: devel
- name: Test server
- name: Setup
run: |
pushd websh_server
echo -e '\nswitch("d", "ci")' > tests/config.nims
nimble test -Y
- name: Build server
run: docker-compose -f docker-compose-ci.yml run server
# FIXME: GitHubActionsでDockerAPIを使用可能にする必要がある
# - name: Test API
# run: ./.github/scripts/api_test.sh
- name: Build front
run: docker-compose -f docker-compose-ci.yml run front
- name: Archive release files
- name: Build image (base)
run: docker build --target base -t base .

- name: Build (front)
run: docker build --target websh_front_builder .

- name: Test (server)
run: docker run --rm -v $PWD/websh_server:/work -t base nimble test -Y

- name: Build image (server runtime)
run: docker build --target websh_server_runtime -t ${{ env.registory }}:server-${{ env.image-tag }} .

- name: Build image (remover runtime)
run: docker build --target websh_remover_runtime -t ${{ env.registory }}:remover-${{ env.image-tag }} .

- name: Login docker
run: echo "${{ secrets.DOCKER_PASS }}" | docker login -u "${{ secrets.DOCKER_USER }}" --password-stdin

- name: Push image to registory
run: |
docker push ${{ env.registory }}:server-${{ env.image-tag }}
docker push ${{ env.registory }}:remover-${{ env.image-tag }}
if: startsWith(github.ref, 'refs/tags/')

- name: Create artifact
run: |
mkdir -p dist/websh
cp -r websh_server/bin dist/websh
cp -r websh_front/public dist/websh
git describe --tags --abbrev=0 > dist/websh/tag.txt
pushd dist
Expand Down
11 changes: 5 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/bin/
websh_server/bin/*
websh_server/tests/twebsh_server
websh_server/tmp/*
!websh_server/tmp/.gitkeep

websh_server/bin/websh_server
websh_front/public/js/index.js
websh_front/tests/*.js

websh_server/tests/twebsh_server

websh_server/bin/websh_remover
websh_server/tools/visor
websh_remover/bin/*
56 changes: 45 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,48 @@
FROM ubuntu:19.10
FROM alpine:3.12 AS base

RUN apt-get update -yqq \
&& apt-get install -y --no-install-recommends \
ca-certificates \
curl \
build-essential \
git \
;
RUN apk add --no-cache \
alpine-sdk

ENV PATH /root/.nimble/bin:$PATH
RUN curl https://nim-lang.org/choosenim/init.sh -sSf > init.sh
RUN sh init.sh -y \
&& choosenim devel

# install nim (#devel) and nim-tools
RUN mkdir -p /nim && \
git clone https://github.com/nim-lang/Nim /nim && \
cd /nim && \
sh build_all.sh && \
ln -s `pwd`/bin/nim /bin/nim && \
nim c koch && \
./koch tools && \
ln -s `pwd`/bin/nimble /bin/nimble && \
ln -s `pwd`/bin/nimsuggest /bin/nimsuggest

RUN mkdir /work
WORKDIR /work

################################################################################
# builder stages
################################################################################

FROM base AS websh_front_builder
COPY websh_front/ /work
RUN nimble build -Y

FROM base AS websh_server_builder
COPY websh_server/ /work
RUN nimble build -Y -d:release

FROM base AS websh_remover_builder
COPY websh_remover/ /work
RUN nimble build -Y -d:release

################################################################################
# runtime stages
################################################################################

FROM alpine:3.12 AS websh_server_runtime
COPY --from=websh_server_builder /work/bin/websh_server /usr/local/bin/
ENTRYPOINT ["/usr/local/bin/websh_server"]

FROM alpine:3.12 AS websh_remover_runtime
COPY --from=websh_remover_builder /work/bin/websh_remover /usr/local/bin/
ENTRYPOINT ["/usr/local/bin/websh_remover"]
19 changes: 7 additions & 12 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,30 +134,25 @@ APIサーバはコンテナの破棄のトリガーを生成するのみに留
Path Description
===================== ========================================
docs READMEの画像ファイルなど
proxy ローカル開発用のnginxの設定
websh_front フロントエンドのソースコード
websh_server バックエンドのソースコード
nginx ローカル開発用のnginxの設定
websh_front フロントエンドのプログラム
websh_server バックエンドのAPIサーバのプログラム
websh_remover バックエンドの後始末を行うプログラム
config.nims タスク定義
Dockerfile ローカル開発でのみ使用する
Dockerfile アプリのDockerイメージ
docker-compose.yml ローカル開発でのみ使用する開発環境設定
docker-compose-ci.yml GitHub Actionsでのみ使用する
===================== ========================================


開発環境の起動方法
------------------

リポジトリのルート直下の `config.nims` にリポジトリ内で使用するタスクを定義して
いる。
以下のコマンドをリポジトリディレクトリ配下で実行する。

.. code-block:: shell
# 最初の一度、あるいはDockerイメージを更新したいときだけ実行
nim --hints:off pullShellgeiBotImage
# 開発環境の起動
nim --hints:off run
docker-compose build
docker-compose up
サーバを起動して待機状態になったら、ブラウザで以下のページにアクセスする。

Expand Down
24 changes: 24 additions & 0 deletions build/compiler_devel/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM ubuntu:19.10

RUN apt-get update -yqq && \
apt-get install -yqq --no-install-recommends \
build-essential \
ca-certificates \
git \
&& \
rm -rf /var/lib/apt/lists/*

RUN git clone --depth 1 https://github.com/nim-lang/Nim && \
cd Nim && \
bash build_all.sh

ENV PATH $PWD/Nim/bin:$PATH

RUN nimble install -Y \
jester \
karax \
regex \
;

RUN nim --version && \
nimble --version
10 changes: 10 additions & 0 deletions build/compiler_stable/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM nimlang/nim:1.0.6-ubuntu

RUN nimble install -Y \
jester \
karax \
regex \
;

RUN nim --version && \
nimble --version
35 changes: 35 additions & 0 deletions build/executor/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
FROM docker:19.03-dind AS base

# Let's start with some basic stuff.
RUN apk add --no-cache alpine-sdk
RUN mkdir -p /nim && \
curl -sL "http://nim-lang.org/download/nim-1.0.6.tar.xz" | tar xJ --strip-components=1 -C /nim && \
cd /nim && \
sh build.sh && \
rm -r c_code tests

ENV PATH=/nim/bin:/root/.nimble/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

WORKDIR /nim
RUN nim c -d:release koch.nim && \
./koch nimble

RUN nim --version && \
nimble --version


FROM base AS builder

COPY nimbot_executor /work
RUN cd /work && nimble install -Y


FROM docker:19.03-dind AS runtime

RUN apk add --update --no-cache tzdata && \
cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime && \
echo "Asia/Tokyo" > /etc/timezone && \
apk del tzdata

COPY --from=builder /root/.nimble/bin/nimbot_executor /usr/local/bin/nimbot_executor
ENTRYPOINT ["/usr/local/bin/nimbot_executor"]
5 changes: 5 additions & 0 deletions build/fluentd/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM fluentd:v1.9-1 AS runtime

USER root
RUN mkdir /var/log/nimbot
COPY conf/fluentd /fluentd/etc
18 changes: 18 additions & 0 deletions build/server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM nimlang/nim:1.0.6-ubuntu AS builder

COPY nimbot_server /work
RUN cd /work && nimble install -Y


FROM nimlang/nim:1.0.6-ubuntu AS runtime

ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update -yqq && \
apt-get install -y tzdata && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
ENV TZ=Asia/Tokyo

COPY --from=builder /root/.nimble/bin/nimbot_server /usr/local/bin/nimbot_server
ENTRYPOINT ["/usr/local/bin/nimbot_server"]

23 changes: 0 additions & 23 deletions docker-compose-ci.yml

This file was deleted.

38 changes: 35 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,44 @@
version: '3'
version: '3.7'

services:
proxy:
nginx:
image: nginx:1.17
volumes:
- "./proxy/conf.d:/etc/nginx/conf.d:ro"
- "./nginx/conf.d:/etc/nginx/conf.d:ro"
- "./websh_front/public:/var/www:ro"
ports:
- "80:80"
network_mode: "host"

websh_front:
build: &app-build
context: .
dockerfile: Dockerfile
target: base
volumes:
- "./websh_front:/work"
entrypoint: nimble
command:
- build
- -Y
- -d:local

websh_server:
build: *app-build
volumes:
- "./websh_server:/work"
environment:
HOST_PWD: "$PWD/websh_server"
entrypoint: ./entrypoint.sh
ports:
- "5000:5000"
network_mode: "host"

websh_remover:
build: *app-build
volumes:
- "./websh_remover:/work"
- "./websh_server/tmp:/dirs"
entrypoint: ./entrypoint.sh
environment:
WEBSH_REMOVER_DIR: "/dirs"
2 changes: 2 additions & 0 deletions proxy/conf.d/nginx.conf → nginx/conf.d/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ server {
return 204;
}

client_max_body_size 8m;

add_header Access-Control-Allow-Origin '*' always;
proxy_set_header X-Forwarded-for $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
Expand Down
6 changes: 6 additions & 0 deletions websh_remover/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

set -eu

nimble build -Y
./bin/websh_remover
Loading

0 comments on commit a35a854

Please sign in to comment.