{"id":430,"date":"2019-04-18T11:13:45","date_gmt":"2019-04-18T02:13:45","guid":{"rendered":"http:\/\/localhost:8000\/?p=430"},"modified":"2021-01-23T11:21:30","modified_gmt":"2021-01-23T02:21:30","slug":"docker-compose-tips","status":"publish","type":"post","link":"http:\/\/localhost:8000\/2019\/04\/docker-compose-tips.html","title":{"rendered":"Docker Compose\u306eTips"},"content":{"rendered":"
Docker Compose\u306f\u8907\u6570\u306eDocker\u30b3\u30f3\u30c6\u30ca\u3092\u307e\u3068\u3081\u3066\u7acb\u3061\u4e0a\u3052\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u4fbf\u5229\u306a\u30c4\u30fc\u30eb\u3067\u3042\u308b\u3002
\n\u3044\u304f\u3064\u304b\u306e\u30b1\u30fc\u30b9\u306b\u3064\u3044\u3066\u5b9f\u4f8b\u3092\u3042\u3052\u306a\u304c\u3089\u8a2d\u5b9a\u65b9\u6cd5\u3092\u8a18\u8f09\u3057\u3066\u3044\u3053\u3046\u3068\u601d\u3046\u3002<\/p>\n
1\u3064\u76ee\u306edocker-compose\u3067DB\u3092\u30012\u3064\u76ee\u306edocker-compose\u3067Web\u30a2\u30d7\u30ea\u3092\u7acb\u3061\u4e0a\u3052\u3066\u3001Web\u30a2\u30d7\u30ea\u304b\u3089DB\u3092\u53c2\u7167\u3059\u308b\u30b1\u30fc\u30b9\u3092\u8003\u3048\u308b\u3002 DB\u5074\u306e\u30d5\u30a9\u30eb\u30c0\u3067<\/p>\n Web\u30a2\u30d7\u30ea\u5074\u306e\u30d5\u30a9\u30eb\u30c0\u3067<\/p>\n \u540c\u4e00Docker Compose\u5185\u3067Web\u30a2\u30d7\u30ea\u3068\u30ad\u30e3\u30c3\u30b7\u30e5\u30b5\u30fc\u30d0\u3092\u7acb\u3061\u4e0a\u3052\u308b\u30b1\u30fc\u30b9\u3092\u8003\u3048\u308b\u3002 Docker Compose\u3067\u7acb\u3061\u4e0a\u3052\u305fWeb\u30a2\u30d7\u30ea\u304b\u3089\u3001Docker\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u5916\u306eAPI\u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u3092\u8003\u3048\u308b\u3002 \u4eca\u56de\u306f\u3001 Docker Compose\u306f\u8907\u6570\u306eDocker\u30b3\u30f3\u30c6\u30ca\u3092\u307e\u3068\u3081\u3066\u7acb\u3061\u4e0a\u3052\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u4fbf\u5229\u306a\u30c4\u30fc\u30eb\u3067\u3042\u308b\u3002 \u3044\u304f\u3064\u304b\u306e\u30b1\u30fc\u30b9\u306b\u3064\u3044\u3066\u5b9f\u4f8b\u3092\u3042\u3052\u306a\u304c\u3089\u8a2d\u5b9a\u65b9\u6cd5\u3092\u8a18\u8f09\u3057\u3066\u3044\u3053\u3046\u3068\u601d\u3046\u3002 \u5225docker-compose\u306e\u30b3\u30f3\u30c6\u30ca\u3092\u53c2\u7167\u3059\u308b 1\u3064\u76ee\u306edocker-compose\u3067DB\u3092\u30012\u3064\u76ee\u306edocker-compose\u3067Web\u30a2\u30d7\u30ea\u3092\u7acb\u3061\u4e0a\u3052\u3066\u3001Web\u30a2\u30d7\u30ea\u304b\u3089DB\u3092\u53c2\u7167\u3059\u308b\u30b1\u30fc\u30b9\u3092\u8003\u3048\u308b\u3002 \u57fa\u672c\u7684\u306b\u306f\u3001network\u3067\u540c\u4e00\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306b\u6240\u5c5e\u3059\u308b\u3088\u3046\u306b\u8a2d\u5b9a\u3057\u3001external_links\u3067\u5916\u90e8\u30b3\u30f3\u30c6\u30ca\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u3088\u3046\u306b\u8a2d\u5b9a\u3059\u308c\u3070\u3088\u3044\u3002 DB\u7528docker-compose.yml\u306e\u8a2d\u5b9a version: '3' services: postgres: image: postgres:latest container_name: "database" <\/span>Continue Reading<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[],"_links":{"self":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/430"}],"collection":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/comments?post=430"}],"version-history":[{"count":1,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/430\/revisions"}],"predecessor-version":[{"id":431,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/430\/revisions\/431"}],"wp:attachment":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/media?parent=430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/categories?post=430"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/tags?post=430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
\n\u57fa\u672c\u7684\u306b\u306f\u3001network<\/code>\u3067\u540c\u4e00\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306b\u6240\u5c5e\u3059\u308b\u3088\u3046\u306b\u8a2d\u5b9a\u3057\u3001
external_links<\/code>\u3067\u5916\u90e8\u30b3\u30f3\u30c6\u30ca\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u3088\u3046\u306b\u8a2d\u5b9a\u3059\u308c\u3070\u3088\u3044\u3002<\/p>\n
DB\u7528docker-compose.yml\u306e\u8a2d\u5b9a<\/h3>\n
version: '3'\nservices:\n postgres:\n image: postgres:latest\n container_name: "database"<\/code><\/pre>\n
\n
${\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u540d}_default<\/code>\u306b\u306a\u308b\u3002\u4eca\u56de\u306e\u30b1\u30fc\u30b9\u3067\u306f
database_default<\/code>\u306b\u306a\u308b\u3002\n
\n
database_default<\/code>\u3068\u3044\u3046\u540d\u524d\u304c\u6c17\u6301\u3061\u60aa\u3051\u308c\u3070\u3001
networks<\/code>\u8a2d\u5b9a\u3092\u884c\u306a\u3063\u3066\u3082\u3044\u3044\u3002<\/li>\n<\/ul>\n<\/li>\n
ports<\/code>\u8a2d\u5b9a\u306f\u884c\u306a\u3063\u3066\u3044\u306a\u3044\u3002<\/li>\n<\/ul>\n
Web\u30a2\u30d7\u30ea\u7528docker-compose.yml\u306e\u8a2d\u5b9a<\/h3>\n
version: '3'\nservices:\n web:\n build: .\n image: web-application\n container_name: "web-application"\n ports:\n - "8080:8080"\n environment:\n - DATABASE_URL=jdbc:postgresql:\/\/database:5432\/research # DB\u306eURL\n external_links:\n - database # \u53c2\u7167\u5148\u30b3\u30f3\u30c6\u30ca\u540d\u3092\u6307\u5b9a\u3002\u30a8\u30a4\u30ea\u30a2\u30b9\u3082\u3064\u3051\u3089\u308c\u308b\n networks:\n - database_default\nnetworks:\n database_default:\n external: true<\/code><\/pre>\n
\n
\n
docker network ls<\/code>\u3067\u3082\u78ba\u8a8d\u3067\u304d\u308b\u3002<\/li>\n<\/ul>\n<\/li>\n
external_links<\/code>\u3092\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u3067\u3001\u5225Docker Compose\u3067\u7acb\u3061\u4e0a\u3052\u305f
database<\/code>\u30b3\u30f3\u30c6\u30ca\u306b\u5230\u9054\u53ef\u80fd\u306b\u306a\u3063\u3066\u3044\u308b\u3002<\/li>\n
environment<\/code>\u306e\u8a2d\u5b9a\u3067\u3001DB\u306eURL\u3092\u74b0\u5883\u5909\u6570\u306b\u8a2d\u5b9a\u3057\u3066\u3044\u308b\u304c\u3001\u30db\u30b9\u30c8\u540d\u304cexternal_links\u3067\u6307\u5b9a\u3057\u305f\u30b3\u30f3\u30c6\u30ca\u540d
database<\/code>\u306b\u306a\u3063\u3066\u3044\u308b\u3053\u3068\u306b\u6ce8\u610f\u3002<\/li>\n
Dockerfile<\/code>\u306e\u4f5c\u6210\u3082\u5fc5\u8981\u3060\u304c\u4eca\u56de\u306f\u7701\u7565\u3002<\/li>\n<\/ul>\n
\u5b9f\u884c<\/h3>\n
docker-compose up<\/code><\/pre>\n
docker-compose up --build<\/code><\/pre>\n
\u540c\u4e00Docker Compose\u5185\u3067\u5225\u30b3\u30f3\u30c6\u30ca\uff08\u30b5\u30fc\u30d3\u30b9\uff09\u3092\u7acb\u3061\u4e0a\u3052\u308b<\/h2>\n
\n\u901a\u5e38\u306f\u7279\u306b\u4f55\u3082\u8003\u3048\u305a\u5b9a\u7fa9\u3059\u308c\u3070OK\u3002<\/p>\ndocker-compose.yml\u306e\u8a2d\u5b9a<\/h3>\n
version: '3'\nservices:\n web:\n build: .\n image: web-application\n ports:\n - "8080:8080"\n memcached:\n image: memcached:latest<\/code><\/pre>\n
\n
external_links<\/code>\u3092\u4f7f\u3046\u305f\u3081\u306b\u3001web\u30b3\u30f3\u30c6\u30ca\u3092
database_default<\/code>\u306b\u6240\u5c5e\u3055\u305b\u305f\u308a\u3057\u3066\u3044\u308b\u3068\u3001memcached\u30b3\u30f3\u30c6\u30ca\u3082\u660e\u793a\u7684\u306b\u540c\u3058network\u306b\u6240\u5c5e\u3055\u305b\u308b\u5fc5\u8981\u304c\u3042\u308b\u306e\u3067\u8981\u6ce8\u610f\u3002<\/li>\n<\/ul>\n
\u5b9f\u884c<\/h3>\n
docker-compose up --build<\/code><\/pre>\n
\u5916\u90e8API\u3092\u547c\u3073\u51fa\u3059<\/h2>\n
\n\u672c\u5f53\u306b\u5916\u90e8API\u3092\u547c\u3073\u51fa\u3059\u30d1\u30bf\u30fc\u30f3\u3068\u3001\u30db\u30b9\u30c8\u30de\u30b7\u30f3\uff08localhost\uff09\u3067\u52d5\u304b\u3057\u3066\u3044\u308bAPI\u3092\u547c\u3073\u51fa\u3059\u30d1\u30bf\u30fc\u30f3\u306e2\u30d1\u30bf\u30fc\u30f3\u3092\u691c\u8a3c\u3057\u305f\u3002
\n\u5916\u90e8API\u306fextra_hosts<\/code>\u306e\u8a2d\u5b9a\u304c\u5fc5\u8981\u3067\u3001\u30db\u30b9\u30c8\u30de\u30b7\u30f3\u4e0a\u306eAPI\u306b\u306f
host.docker.internal<\/code>\u3067\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u3002<\/p>\n
dockder-compose.yml\u306e\u8a2d\u5b9a<\/h3>\n
version: '3'\nservices:\n web:\n build: .\n image: web-application\n container_name: "web-application"\n ports:\n - "8080:8080"\n environment:\n - EXTERNAL_API_BASE=https:\/\/hoge.com\/ # \u5916\u90e8API\u306e\u30d9\u30fc\u30b9URL\n - INTERNAL_API_BASE=http:\/\/host.docker.internal:8081\/fuga # \u30db\u30b9\u30c8\u30de\u30b7\u30f3\u4e0aAPI\u306e\u30d9\u30fc\u30b9URL\n extra_hosts:\n - "hoge.com:11.22.33.44"<\/code><\/pre>\n
\n
extra_hosts<\/code>\u306b\u8a2d\u5b9a\u3057\u305f\u5185\u5bb9\u304c\u3001\u30b3\u30f3\u30c6\u30ca\u8d77\u52d5\u6642\u306b
\/etc\/hosts<\/code>\u306b\u66f8\u304d\u8fbc\u307e\u308c\u308b\u3002<\/li>\n
EXTERNAL_API_URL<\/code>\u3068
INTERNAL_API_URL<\/code>\u306e\u4e8c\u3064\u306e\u74b0\u5883\u5909\u6570\u3092\u8aad\u307f\u8fbc\u3093\u3067\u5229\u7528\u3059\u308b\u3002<\/li>\n<\/ul>\n
docker-compose.yml\u5185\u3067\u52d5\u7684\u306b\u5909\u6570\u7f6e\u304d\u63db\u3048\u3059\u308b<\/h2>\n
20190512<\/code>\u306e\u69d8\u306a\u6587\u5b57\u5217\u3092\u5b9f\u884c\u6642\u306b\u52d5\u7684\u306b\u53d6\u5f97\u3057\u3066\u3001\u30b3\u30f3\u30c6\u30ca\u5185\u3067
WORK_DIR<\/code>\u74b0\u5883\u5909\u6570\u3068\u3057\u3066\u5229\u7528\u3059\u308b\u30b1\u30fc\u30b9\u3092\u8003\u3048\u308b\u3002
\n\u7d50\u8ad6\u3068\u3057\u3066\u306f\u3001docker-compose\u5b9f\u884c\u6642\u306e\u74b0\u5883\u5909\u6570\u3092\u8a2d\u5b9a\u3059\u308b\u3057\u304b\u306a\u3044\u3002
\n\u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306f\u3053\u3061\u3089<\/a><\/p>\ndockder-compose.yml\u306e\u8a2d\u5b9a<\/h3>\n
version: '3'\nservices:\n web:\n build: .\n image: web-application\n container_name: "web-application"\n ports:\n - "8080:8080"\n environment:\n - WORK_DIR=${TODAY}<\/code><\/pre>\n
\n
\n
.env<\/code>\uff09\u3067\u6307\u5b9a\u3057\u305f\u74b0\u5883\u5909\u6570<\/li>\n
\u5b9f\u884c<\/h3>\n
TODAY<\/code>\u74b0\u5883\u5909\u6570\u3092\u30b7\u30a7\u30eb\u3067\u8a2d\u5b9a\u3057\u3066\u304b\u3089\u3001
docker-compose up<\/code>\u3092\u5b9f\u884c\u3059\u308b\u3002<\/p>\n
export TODAY=$(date "+%Y%m%d")\ndocker-compose up --build<\/code><\/pre>\n
\u305d\u306e\u4ed6Tips<\/h2>\n
\n
web<\/code>\u3068\u304b\u30e6\u30cb\u30fc\u30af\u6027\u304c\u4f4e\u3044\u8a00\u8449\u306f\u4f7f\u308f\u306a\u3044\u65b9\u304c\u826f\u3044\u3002<\/li>\n
docker-compose down<\/code>\u3092\u8a66\u3057\u3066\u307f\u308b\u3002<\/li>\n
\n
\n
docker-compose up<\/code>\u3067\u7acb\u3061\u4e0a\u3052<\/li>\n
gradle bootRun<\/code>\u3068\u304b
activator run<\/code>\u3068\u304b<\/li>\n
\u53c2\u8003\u30ea\u30f3\u30af<\/h2>\n
\n