docker-compose
ログ設定 2021/07/26
ログ設定をしないと起動時からのログを全て保存するので、サイズ設定を行う。 選べる出力先(driver)は json-file, syslog, awslogs, gcplogs などが選択可能 デフォルトは json-file。
json-file でサイズを設定した
services:
server:
logging:
driver: json-file
options:
max-file: '1'
max-size: 3m
syslog
EC2 上で docker-compose で起動するようなケース
logging:
driver: syslog
options:
syslog-facility: daemon
tag: redash/{{.Name}}/{{.ID}}
下記のような出力になる
Jul 26 08:10:07 ip-172-30-1-46 redash/redash_scheduler_1/e4dbe49ee058[1067]: [2021-07-26 08:10:07,916][PID:19][INFO][ForkPoolWorker-2] task_name=redash.tasks.refresh_queries task_id=aecf0215-9884-4254-82d2-1d08f8eb58f9 Refreshing queries...
Jul 26 08:10:07 ip-172-30-1-46 redash/redash_scheduler_1/e4dbe49ee058[1067]: [2021-07-26 08:10:07,986][PID:19][INFO][ForkPoolWorker-2] task_name=redash.tasks.refresh_queries task_id=aecf0215-9884-4254-82d2-1d08f8eb58f9 Done refreshing queries. Found 0 outdated queries: []
Jul 26 08:10:07 ip-172-30-1-46 redash/redash_scheduler_1/e4dbe49ee058[1067]: [2021-07-26 08:10:07,998][PID:19][INFO][ForkPoolWorker-2] Task redash.tasks.refresh_queries[aecf0215-9884-4254-82d2-1d08f8eb58f9] succeeded in 0.081449877s: None
cloudwatch logs などリモートにロギングする場合、ノンブロッキングオプションも検討の余地がある
build して stop/start しても古いイメージから起動してしまう場合
docker-compse で build して、stop/start しても何故か古いイメージから起動される、と思ったらこんな話が
古いイメージを全て削除してから build すると docker-compose も新しいものを利用する。 いちいち docker rmi するのは面倒だ。と思ったら https://stackoverflow.com/questions/37685581/how-to-get-docker-compose-to-use-the-latest-image-from-repository
docker-compose stop web && docker-compose rm web
みたいにすると、該当サービスだけ削除できる。 docker-compose up web -d で新しいイメージで起動した。
#[ WARNING: Found orphan containers ********for this project. ] みたいなエラーが出たら
docker-compose up web --remove-orphans
WARNING: Found orphan containers (****) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
というエラーが出たら
docker-compose up --remove-orphans
のようにして起動すると良い。一度だけ実行すれば良かった。2017/12/06
docker-compose.yml 書き方
簡単に都度コマンドを実行したい
Dockerfiles でイメージをビルドするのではなく、Dockerhub からクローンしたイメージで起動して、起動するたびにキレイな状態でコマンドを実行したい
services:
distcc:
image: gentoo/stage3-amd64
container_name: 'distcc'
command: >
bash -c "/bin/bash"
雛形
※ # <= 下記のうち、コメントは削除する必要がある。
services:
web:
restart: always # 自動起動
build:
context: containers/nginx # docker-compose.yml と同じディレクトリ内に ./containers/nginx/Dockerfiles がある場合
container_name: hoge-nginx # コンテナ名
depends_on: # 事前に起動する必要があるコンテナを記述
- php-fpm
ports:
- "80:80"
volumes: # マウントするディレクトリ
- ./webroot:/var/www
extra_hosts: # コンテナ内のhosts設定
- "api.local.hoge.com:127.0.0.1"
- "www.local.hoge.com:127.0.0.1"
working_dir: /var/www/root
command: # コンテナ起動時に実行したいコマンド
- nginx
- -c
- conf/nginx.conf
- -g
- daemon off;