Last active
January 2, 2016 17:39
-
-
Save ka2n/8338712 to your computer and use it in GitHub Desktop.
Dockerを使ってWebアプリケーションをデプロイする ref: http://qiita.com/ka2n/items/9659cb2b083ab7dcd844
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
APP_IMAGE_NAME=ka2n/sentry | |
# アプリケーションを更新 | |
pushd ./app/src | |
git fetch origin | |
git reset --hard origin/master | |
popd | |
# アプリケーションイメージをビルド | |
docker build -t=$APP_IMAGE_NAME ./app | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# baseイメージからスタート | |
FROM ka2n/base | |
###### アプリケーションをセットアップ ##### | |
# アプリケーションのコードをコンテナの/appに追加 | |
ADD /src /app | |
# インストール(`bundle install`とか`npm install`とか) | |
RUN pip install /app | |
# 設定ファイルを追加 | |
ADD /conf/sentry.conf.py /sentry.conf.py | |
##### Docker #### | |
EXPOSE 9000 # Sentryが起動するHTTPサーバのポート | |
ENTRYPOINT ["sentry", "--config=/sentry.conf.py"] | |
CMD ["start"] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
この記事は2014年01月頃に書いた記事のため現在では内容が古く、正確でないかもしれません。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# カーネルを更新 | |
sudo apt-get update | |
sudo apt-get install -y linux-image-generic-lts-raring linux-headers-generic-lts-raring | |
# 再起動 | |
sudo reboot |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http { | |
# … | |
include /home/ka2n/sentry/nginx-*.conf; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo docker build -t=ka2n/base ~/sentry/base |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo docker build -t=ka2n/base ~/sentry/base |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cd ~/sentry | |
sudo ./build.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cd ~/sentry | |
sudo ./run.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo apt-get install -y curl # curlがなければ | |
curl -s https://get.docker.io/ubuntu/ | sudo sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo docker run -i -t ubuntu /bin/bash | |
# ubuntuコンテナで/bin/bashが立ち上がる。`exit`で閉じる |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~/sentry | |
├── state # 永続化するデータを保存するディレクトリ | |
├── nginx-app.conf # 自動生成されるNGINX用の設定ファイル | |
├── app | |
│ ├── Dockerfile | |
│ ├── conf | |
│ │ └── sentry.conf.py # アプリケーション設定 | |
│ └── src # アプリケーション本体 | |
├── base | |
│ └── Dockerfile | |
├── build.sh # ソースコードのアップデート, イメージのビルド | |
└── run.sh # コンテナ実行、nginxへの登録, 古いコンテナの破棄 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http { | |
# … | |
include /home/ka2n/sentry/nginx-*.conf; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~/sentry | |
├── state # 永続化するデータを保存するディレクトリ | |
├── nginx-app.conf # 自動生成されるNGINX用の設定ファイル | |
├── app | |
│ ├── Dockerfile | |
│ ├── conf | |
│ │ └── sentry.conf.py # アプリケーション設定 | |
│ └── src # アプリケーション本体 | |
├── base | |
│ └── Dockerfile | |
├── build.sh # ソースコードのアップデート, イメージのビルド | |
└── run.sh # コンテナ実行、nginxへの登録, 古いコンテナの破棄 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
APP_IMG_NAME=ka2n/sentry | |
APP_VHOST=sentry.example.com | |
# 同じタグで実行中のコンテナをリストアップ | |
CURRENT_CONTAINERS=`docker ps | grep $APP_IMG_NAME | awk '{print $1}'` | |
echo "[Running containers]" | |
echo "$CURRENT_CONTAINERS" | |
# ファイル永続化用のディレクトリを作成 | |
mkdir -p /var/app/sentry | |
# 新しいコンテナを起動する | |
echo "[Launch new container]" | |
docker run \ | |
-d \ | |
-v /var/app/sentry:/sentry/state \ | |
-e SENTRY_DB=/sentry/state/sentry.db \ | |
-e SENTRY_URL_PREFIX="http://$APP_VHOST" \ | |
-e SENTRY_PORT=9000 \ | |
-p 9000 \ | |
$APP_IMG_NAME | |
# コンテナの情報を取得 | |
NEW_ID=`docker ps -l -q` | |
NEW_ADDR=`docker port $NEW_ID 9000` | |
NEW_PORT=${NEW_ADDR#0.0.0.0:} | |
NEW_IP=`docker inspect --format="{{ .NetworkSettings.IPAddress }}" $NEW_ID` | |
echo "[New container info]" | |
echo "CONTAINER ID: ${NEW_ID}" | |
echo "IP: ${NEW_IP}" | |
sleep 1 | |
# NGINX用の設定ファイルを生成 | |
echo "[Write new nginx config]" | |
cat <<EOF > ./nginx-app.conf | |
upstream container-sentry { server 127.0.0.1:$NEW_PORT; } | |
server { | |
listen 80; | |
server_name $APP_VHOST; | |
proxy_set_header Host \$http_host; | |
proxy_set_header X-Real-IP \$remote_addr; | |
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; | |
proxy_set_header X-Forwarded-Proto \$scheme; | |
proxy_redirect off; | |
location / { | |
proxy_set_header Host \$host; | |
proxy_pass http://container-sentry; | |
break; | |
} | |
} | |
EOF | |
# NGINXをリロード | |
service nginx reload | |
# 古いコンテナをシャットダウン | |
echo "[Shutting down old containers]" | |
if [ -n "$CURRENT_CONTAINERS" ]; then | |
docker kill $CURRENT_CONTAINERS | |
fi | |
echo "[Done]" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os.path | |
import os | |
CONF_ROOT = os.path.dirname(__file__) | |
DB_NAME = os.environ.get('SENTRY_DB', os.path.join(CONF_ROOT, 'sentry.db')) | |
DB_USER = os.environ.get('SENTRY_DB_USER', '') | |
DB_PASS = os.environ.get('SENTRY_DB_PASS', '') | |
DB_HOST = os.environ.get('SENTRY_DB_HOST', '127.0.0.1') | |
DB_PORT = os.environ.get('SENTRY_DB_PORT', '') | |
DATABASES = { | |
'default': { | |
'ENGINE': 'django.db.backends.sqlite3', | |
'NAME': DB_NAME, | |
'USER': DB_USER, | |
'PASSWORD': DB_PASS, | |
'HOST': DB_HOST, | |
'PORT': DB_PORT, | |
} | |
} | |
SENTRY_PUBLIC = False | |
SENTRY_ALLOW_REGISTRATION = False | |
################ | |
## Web Server ## | |
################ | |
# You MUST configure the absolute URI root for Sentry: | |
SENTRY_URL_PREFIX = os.environ.get('SENTRY_URL_PREFIX', 'http://sentry.example.com') | |
ALLOWED_HOSTS = ["*"] | |
SENTRY_WEB_HOST = '0.0.0.0' | |
SENTRY_WEB_PORT = os.environ.get('SENTRY_PORT', 9000) | |
SENTRY_WEB_OPTIONS = { | |
'workers': 3, # the number of gunicorn workers | |
'limit_request_line': 0, # required for raven-js | |
'secure_scheme_headers': {'X-FORWARDED-PROTO': 'https'}, | |
} | |
########### | |
## etc. ## | |
########### | |
SECRET_KEY = '<Your Secret Key>' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment