###### tags: `docker` [TOC] # docker ## 基本觀念 :::spoiler 參考資料 - [初探 docker](https://askie.today/docker-dockerfile-dockercompose-intro/) ::: - image 映像檔 - docker image is read-only - can build the docker container - 每個 image 有不同的 tag - Container 容器 - 可以被啟動、開始、停止、刪除 - 每個容器互相獨立,不會互相影響 - Repository 倉庫 - 集中存放 image 的地方,內含多個 image - 兩種形式: public and private ## VM vs. Docker  > [參考資料](https://blog.kevinyang.net/2020/07/30/docker-101-note-1/) ### VM - 優點 - VM 各自擁有自己的 OS kernel - 不同的環境可以並存 > 同一台 VM 可以同時安裝 Windows and Linus,但是套件安裝只能統一版本 - 缺點 - VM 複製環境的速度比較慢 > 因為 image = 整台 VM ,所以檔案較大 ### docker - 優點 - 多個 containers 彼此之間共一個 OS kernel - Docker 不能同時安裝 Windows and Linux,只能擇一 (優缺?) > 但是不同的 Container 可以安裝不同的環境,彼此不干擾。 > - ex: Container_1 install python 3.6.9 and Container_2 install python 3.8 > - 執行不會因為 Container 彼此版本不同而衝突 - 相同的硬體資源,Docker 可以執行較多單位 - Docker 複製環境的速度比較快 > 因為每一個 Container 都有自己的 image,若要複製特定一個 image 會比 VM 快。 - 缺點 - 安全行較低 > 不知道為啥 ## [install docker in Linux](https://docs.docker.com/engine/install/ubuntu/) - 三種安裝方式 (請點擊連結操作) 1. set up the repository 2. install docker engine 3. install from a package - Windows 安裝 Docker 需要 Hyper-V  > [參考資料](https://blog.kevinyang.net/2020/07/30/docker-101-note-1/) ## [建立 docker file](https://docs.docker.com/engine/reference/builder/#env) ## [啟動多個 docker container](https://ithelp.ithome.com.tw/articles/10194183) - install ```termainl= cd /usr/bin wget https://github.com/docker/compose/releases/download/1.18.0/docker-compose-Linux-x86_64 mv docker-compose-Linux-x86_64 docker-compose chmod 755 docker-compose ``` - docker-compose.yml 範本內容 ```file= version: <填入數字> service: // container 裡面可以撰寫網頁在運行時,所需要的內容或設定 <container name>: build: volume: image: ports: restart: ... <container name>: build: ... ``` :::spoiler docker-compose 範例內容 ```file= version: '2' services: db: image: mysql environment: MYSQL_ROOT_PASSWORD: 123456 admin: image: adminer ports: - 8080:8080 ``` ::: - 啟動所有的 docker container ```termainl= // -d 參數代表要執行在背景的方式 docker-compose up -d ``` - 切到和 docker-compose.yml 同一層路徑,查看 Docker Container 的執行狀態 ```termainl= docker-compose ps ``` # docker 架站/基礎指令 :::success ### :point_right: [教學影片](https://youtu.be/NsOAdmjyOCo) ::: - [參考資料](https://kejyuntw.gitbooks.io/docker-learning-notes/content/container/container-stop.html) ## 需要條件 1. `requirement.txt` - 檔案中所有需要用到的套件寫成 `requirement.txt` - 寫入後端有安裝的套件名稱以及版本 - 每次更新套件,docker 要重新佈署 ```txt= flask==2.1.2 imageio==2.18.0 numpy==1.22.3 matplotlib==3.5.1 uwsgi ``` 2. `dockerfile` - 引入後端對應的套件 :point_right: [docker image python](https://hub.docker.com/_/python) ```docker= FROM python:3.8 ENV PYTHONUNBUFFERED 1 RUN mkdir /run_data && mkdir /api_data && mkdir /log WORKDIR /api_data COPY ./flask /api_data RUN pip install -r requirements.txt CMD ["uwsgi", "--ini", "run.ini"] ``` :::spoiler 示意圖  ::: 3. `run.ini` - 設定網站跑起來時,如何跑 ```ini= [uwsgi] http = 0.0.0.0:8000 module = app:app master=true processes = 2 threads = 2 vacuum = true logto = /log/service.log die-on-term = true ``` :::spoiler 示意圖  ::: 4. docker-compose.yml (optional) ```docker= version: "3" services: app: restart: unless-stopped image: gei volumes: - ./log:/log networks: - traefik_network labels: - "traefik.enable=true" - "traefik.http.routers.gei.rule=Host(`pyyin-gei.im.ncnu.edu.tw`)" - "traefik.http.routers.gei.entrypoints=websecure" - "traefik.http.routers.gei.service=gei-server" - "traefik.http.services.gei-server.loadbalancer.passhostheader=true" - "traefik.http.services.gei-server.loadbalancer.server.port=8000" - "traefik.docker.network=traefik_network" networks: traefik_network: external: true ``` :::spoiler 示意圖  ::: - 如果服務有更動,3 個地方要改 1. domain name 2. 網址(mask -> gei) 3. docker-compose.yml ## 事先準備 1. 確認上面的需要條件都有建立 2. docker dns check:確認申請的網址是不是 public - [DNS checker](https://dnschecker.org/) ## 建立服務 - `docker build -t <服務名稱> .` 服務建立 - `.`記得要寫到!! 代表選取資料夾下所有的檔案 - `docker-compose up -d` 讓服務跑起來 - `docker ps -a` 確認有沒有跑起來 - `docker logs <docker image name>` 查看 docker 的 log - 跑完要刪除 image (`<none>`):把原本系統的 image (像是檔案格式 .png...)刪除 - `docker image prune` - `docker images` 查看目前的 image  - `docker stop <服務名稱>` 停止服務 ## 之後更新網站上去 - 我們架設網站的地方  ## 可以參考的 docker 教學 - [NCKU SA/NA Course - 03. Docker](https://hackmd.io/@Vincent550102/ry2tg1wRF?fbcl) - [初探Docker Machine](https://peihsinsu.gitbooks.io/docker-note-book/content/docker-machine.html)
×
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