Docker 學習筆記 === ###### tags: `恩-前後端筆記` ## 下載 docker & docker-compose on Rpi * 下載 docker ```bash= curl -sSL https://get.docker.com | sh ``` * 把 docker 的權限提高 ```bash= sudo usermod -aG docker pi ``` * 下載一些有的沒的 ```bash= sudo apt-get install libffi-dev libssl-dev ``` * 下載 docker-compose ```bash= sudo pip3 install docker-compose ``` * https://dev.to/rohansawant/installing-docker-and-docker-compose-on-the-raspberry-pi-in-5-simple-steps-3mgl ## 什麼是 dockerfile * Dockerfile 是一個包含指令的文字檔,可以根據這個檔案去呼叫指令以及 assemble image 檔 * Docker 可以根據 dockerfile 自動 build images ## docker 三個名詞 * image * build 好的一個包 * 可以自己用 docker build 做一個自己的 image * 也可以 docker pull 從 docker hub 上載別人整理好的 image * container * 在電腦上跑起來的一個環境,可以同時有多個 container 執行 * volume * 在 container 中的儲存單位,可以和電腦中的資料夾共通 ## dockerfile 格式詳解 * 做一個 dockerfile ```dockerfile= # Use a lighter version of Node as a parent image FROM mhart/alpine-node:8.11.4 # Set the working directory to /client WORKDIR /client # copy package.json into the container at /client COPY package*.json /client/ # install dependencies RUN npm install # Copy the current directory contents into the container at /client COPY . /client/ # Make port 3000 available to the world outside this container EXPOSE 3000 # Run the app when the container launches CMD ["npm", "start"] ``` * COPY 如果要連資料夾一起複製的話 * 後面要再加一次自己 ``` COPY dir something/dir ``` ## docker 指令 ### image 指令 * Dockerfile build 出 image ```bash= docker build -t simple-express-server . ``` * 看有多少個 image ```bash= docker image ls ``` * 把 docker run 起來 ```bash= docker run -d -p 3000:8080 simple-express-server ``` ``` 把 docker 中的 8080 port 打到外面的 3000 port -d -> 在背景中執行 ``` ```bash= docker run -it <image> /bin/bash ``` ``` 進入 image 中 ``` * 看看背景中的 image 訊息 ```bash= docker logs --tail 50 --follow --timestamps <container> ``` * remove docker image ```bash= docker rmi <image_name> ``` ### container 指令 * 看所有正在執行中的 container ```bash= docker ps -a ``` * 中斷 container ```bash= docker stop <container_id> ``` * 啟動 container (在現有的 container 中開) ```bash= docker start <container_id> ``` * 啟動 container (啟動一個新的 container) ```bash= docker run -idt <image:name> ``` * 進入 container 中 ```bash= docker exec -it <image:name> bash # 進入 command line docker exec -it <image:name> <command> # 也可以直接執行對應的指令 ``` * 清除 build 時的 cache ```bash= docker builder prune ``` ## 什麼是 docker-compose * Docker Compose 是一個工具可以讓你可以透過一個指令就可以控制所有專案(project)中所需要的 services * Docker Compose 是用 YAML 檔案格式來描述和定義 project 中 services 運作關係 ## docker-compose 格式詳解 * docker-compose.yml 大概的長像 * .yml 是有縮排的,跟 python 一樣 ```yaml= version: '3' # 目前使用的版本,可以參考官網: services: # services 關鍵字後面列出 web, redis 兩項專案中的服務 web: build: . # Build 在同一資料夾的 Dockerfile(描述 Image 要組成的 yaml 檔案)成 container ports: - "5000:5000" # 外部露出開放的 port 對應到 docker container 的 port volumes: - .:/code # 要從本地資料夾 mount 掛載進去的資料 host: compose file links: - redis # 連結到 redis,讓兩個 container 可以互通網路 redis: image: redis # 從 redis image build 出 container ``` ### version * 一定要放在最上面,告訴 yml 是要使用哪一個版本的 docker-compose ### service * 放 docker image 執行方式的地方 * 服務可命名,也可以放多個服務 ### image * 可以直接放從 docker hub pull 下來的 image * 也可以指定版本的 tag ### build * 如果是要自己 build 一個 image 的話 * 要用 build 而不是 image * context 是告訴 docker 你的 dockerfile 在哪裡 * dockerfile 則是 告訴 docker 你的 dockerfile 叫什麼名字,預設就是 Dockerfile ```yaml= build: context: ./flask dockerfile: Dockerfile ``` ### container_name * 指定 container 的名稱 * 可以在打指令的時候不打 id 打名稱就可以了 ### command * container 啟動後立刻執行的指令 * 如果要一次執行多個指令 ``` command: bash -c "mongod --repair && mongod --auth" ``` ### networks * container 要加入哪個網路 ### restart * 指定如果起動失敗後要執行什麼 * always * 一失敗就執行 * unless-stopped * the containers will start automatically once the Docker Engine is restarted or any error occurs. ### environment * 定義環境變數 * 可以隨便自行定義 * 如果值是布林,要用單引號包起來,如 ' true '、' false '。 ### ports * 格式是 host:container * 把在 container 內的 port 打在主機上的一個 port * 或是只指定 container,這時會隨機挑一個 host port 來用 ### volume * 格式為 host:container * 是一個路徑 * 可以把 container 內的某個資料夾同布到主機上的某上的資料夾 * 在主機上修改時,container 內也同部修改 ### reference * https://tpu.thinkpower.com.tw/tpu/articleDetails/1377 ## docker-compose 指令 * 把 docker-compose run 起來 (且 run 在背景) ```bash= docker-compose up -d ``` * 把 docker-compose 關掉 ```bash= docker-compose down ``` * 觀看 docker-compose process 狀況 ```bash= docker-compose ps ``` ## 使用 nginx 把 react 部署 * docker-compose file ```yaml= version: '3' services: # 服务名称 nginx: # 镜像:版本 image: nginx:latest # 映射容器80端口到本地80端口 ports: - "80:80" # 数据卷 映射本地文件到容器 volumes: # 映射nginx.conf文件到容器的/etc/nginx/conf.d目录并覆盖default.conf文件 - ./nginx.conf:/etc/nginx/conf.d/default.conf # 映射build文件夹到容器的/usr/share/nginx/html文件夹 - ./build:/usr/share/nginx/html # 覆盖容器启动后默认执行的命令。 command: /bin/bash -c "nginx -g 'daemon off;'" ``` * nginx.conf * 為了要讓 react router 有作用要修改一些地方 ```ba server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } ``` * https://segmentfault.com/a/1190000010415158 ## Reference * 簡單的 docker 介紹 * https://larrylu.blog/step-by-step-dockerize-your-app-ecd8940696f4 * flask + mongodb + nginx docker 部署 * https://www.digitalocean.com/community/tutorials/how-to-set-up-flask-with-mongodb-and-docker * docker 指令大全 * https://philipzheng.gitbooks.io/docker_practice/content/ * docker & docker-compose cheat sheet * https://devhints.io/docker-compose ``` :::::::::::::: {.columns} ::: {.column width="50%"} ::: ::: {.column width="50%"} ::: :::::::::::::: ```
×
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