# 第四節 Making real projects with docker ## 1.Project Outline 專案建立大致流程  ## 2.Template  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  ``` docker run -p local_host_port:container_port <image id> ``` 然後連接http://localhost:8080/ 在Web應該可看到hi there 舉例   打到host的5000會連到8080 ## 5.Work Dir工作區域  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的次數  使用多個instances來處理大量的流量,這樣有個問題是每個instance都會分開計算 可能一個網站被分享99次,一個3次 解決方法是只scale up web部分  以下會開始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  ## 2.Docker Compose 兩個container有兩種方法可以讓彼此相通,如下圖  Docker Compose可同時使多個containers 啟動 透過某種形式的網路自動將containers連接   利用yml來完成  ### 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名稱找相符的,然後自動對應   ### Docker Compose Commands  ```bash! # rebuild用--build參數 docker-compose up --build ``` ## 3.automatic restarts restart policies  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  ### container maintenance 用一個會Crashed的程式示範  
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up