# 第四節 Making real projects with docker ## 1.Project Outline 專案建立大致流程 ![image](https://hackmd.io/_uploads/H1pP-nzTT.png) ## 2.Template ![image](https://hackmd.io/_uploads/r1h9xZfaa.png) node js 的hub https://hub.docker.com/_/node/ Dockerfile如下 ```dockerfile= FROM node:14-alpine RUN npm install CMD ["npm","start"] ``` alpine在容器的世界就表示是極小,很多repo都會提供alpine版本 note ``` cd /mnt/d/python_code/projects/study_project/udemy/docker_and_kubernetes_the_complete_guide/ch4/39-node-server-setup/simpleweb ``` ## 3.docker build with log ``` docker build . --progress=plain --no-cache ``` ```--progress=plain``` option as docker defaults to auto and hides too much output. ```--no-cache``` option is necessary to rebuild the container to show all of the output. ## 4.docker run with port forwarding ![image](https://hackmd.io/_uploads/Skkb8-GaT.png) ``` docker run -p local_host_port:container_port <image id> ``` 然後連接http://localhost:8080/ 在Web應該可看到hi there 舉例 ![image](https://hackmd.io/_uploads/HJmOIZfTa.png) ![image](https://hackmd.io/_uploads/S11KLZfaa.png) 打到host的5000會連到8080 ## 5.Work Dir工作區域 ![image](https://hackmd.io/_uploads/BJxha0Gpa.png) Dockerfile如下 ```dockerfile= FROM node:14-alpine #避免與container內原本目錄混淆 WORKDIR /usr/app COPY ./ ./ RUN npm install CMD ["npm","start"] ``` ## 6.Unneccessary rebuilds 非必要每次rebuild containers Dockerfile如下 ```dockerfile= FROM node:14-alpine WORKDIR /usr/app # 異動到package.json的話才RUN npm install COPY ./package.json ./ RUN npm install # 沒異動到的部分則使用Cache來rebuilds COPY ./ ./ CMD ["npm","start"] ``` # 第五節 Docker Compose with Multiple Local Containers ## 1. 現在有個需求是要有一個網站來記錄網站被visit的次數 ![image](https://hackmd.io/_uploads/Syaba1f6T.png) 使用多個instances來處理大量的流量,這樣有個問題是每個instance都會分開計算 可能一個網站被分享99次,一個3次 解決方法是只scale up web部分 ![image](https://hackmd.io/_uploads/BJPoakzTp.png) 以下會開始Demo,把Dockerfile準備好後 要先把docker destop打開 然後先在windows內mount windows folder ```bash mkdir /mnt/d mount -t drvfs d: ``` 移動到目錄 ``` cd /mnt/d/python_code/projects/study_project/udemy/docker_and_kubernetes_the_complete_guide/ch5/visits-starter ``` 無法連線問題 參考 https://copyprogramming.com/howto/connect-econnrefused-127-0-0-1-27017-mongo-in-docker-wsl-version-2 ### create a redis server ``` docker run redis ``` ### docker run but in background ``` docker run -d ``` ### stop docker container ``` docker stop <container id> ``` or you could just type ```docker-compose down``` to stop multiple containers ![image](https://hackmd.io/_uploads/rkmSWLXTp.png) ## 2.Docker Compose 兩個container有兩種方法可以讓彼此相通,如下圖 ![image](https://hackmd.io/_uploads/S1pzSMQTa.png) Docker Compose可同時使多個containers 啟動 透過某種形式的網路自動將containers連接 ![image](https://hackmd.io/_uploads/BJK2SGXpp.png) ![image](https://hackmd.io/_uploads/BkzruB7aa.png) 利用yml來完成 ![image](https://hackmd.io/_uploads/H1P6IM7p6.png) ### docker-compose.yml檔 ```yml= version: '3' services: redis-server: image: 'redis' node-app: build: . ports: - "4001:8081" ``` ```version```:docker-compose文件版本 ```services```:container ```build```:Dockerfile的位置 ```-```表示array,可以用來表示多組關係 利用host name去連結兩個containers host name叫做redis-server, docker會自動去container名稱找相符的,然後自動對應 ![image](https://hackmd.io/_uploads/Hk4sczXpa.png) ![image](https://hackmd.io/_uploads/SyL7szmTa.png) ### Docker Compose Commands ![image](https://hackmd.io/_uploads/S1uiormaT.png) ```bash! # rebuild用--build參數 docker-compose up --build ``` ## 3.automatic restarts restart policies ![image](https://hackmd.io/_uploads/B1Gd6MQ6a.png) docker-compose可以自動重啟容器 docker-compose.yml檔 修改yml檔,增加restart 若要寫"no",需使用'或"分割 ```yml= version: '3' services: redis-server: image: 'redis' node-app: #重啟container restart: always build: . ports: - "4001:8081" ``` ```on-failure```表示return code非0,則會進行重啟 ### return code ![image](https://hackmd.io/_uploads/HkogN8mTa.png) ### container maintenance 用一個會Crashed的程式示範 ![image](https://hackmd.io/_uploads/S1vhfIQpT.png) ![image](https://hackmd.io/_uploads/B1yoGL7pa.png)